Skip to content

Commit e15d0c0

Browse files
authored
feat(ecr): make validateRepositoryName errors human readable (#27186)
When a user specifies an invalid repository name, the error message will now describe why the name is invalid instead of providing the regex used to validate the name. The message comes from the console when defining a new repository. The docstring of `repositoryName` now reflects this and mirrors the docstring of the underlying CFN resource. Closes #26715. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent b7eeda6 commit e15d0c0

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

packages/aws-cdk-lib/aws-ecr/lib/repository.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,11 @@ export interface OnImageScanCompletedOptions extends events.OnEventOptions {
493493

494494
export interface RepositoryProps {
495495
/**
496-
* Name for this repository
496+
* Name for this repository.
497+
*
498+
* The repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, and forward slashes.
499+
*
500+
* > If you specify a name, you cannot perform updates that require replacement of this resource. You can perform updates that require no or some interruption. If you must replace the resource, specify a new name.
497501
*
498502
* @default Automatically generated name.
499503
*/
@@ -672,7 +676,7 @@ export class Repository extends RepositoryBase {
672676
}
673677
const isPatternMatch = /^(?:[a-z0-9]+(?:[._-][a-z0-9]+)*\/)*[a-z0-9]+(?:[._-][a-z0-9]+)*$/.test(repositoryName);
674678
if (!isPatternMatch) {
675-
errors.push('Repository name must follow the specified pattern: (?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*');
679+
errors.push('Repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, periods and forward slashes');
676680
}
677681

678682
if (errors.length > 0) {

packages/aws-cdk-lib/aws-ecr/test/repository.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ describe('repository', () => {
898898
const expectedErrors = [
899899
`Invalid ECR repository name (value: ${repositoryName})`,
900900
'Repository name must be at least 2 and no more than 256 characters',
901-
'Repository name must follow the specified pattern: (?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*',
901+
'Repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, periods and forward slashes',
902902
].join(EOL);
903903

904904
expect(() => new ecr.Repository(stack, 'Repo', {
@@ -923,19 +923,19 @@ describe('repository', () => {
923923

924924
expect(() => new ecr.Repository(stack, 'Repo1', {
925925
repositoryName: 'aAa',
926-
})).toThrow(/must follow the specified pattern/);
926+
})).toThrow('Repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, periods and forward slashes');
927927

928928
expect(() => new ecr.Repository(stack, 'Repo2', {
929929
repositoryName: 'a--a',
930-
})).toThrow(/must follow the specified pattern/);
930+
})).toThrow('Repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, periods and forward slashes');
931931

932932
expect(() => new ecr.Repository(stack, 'Repo3', {
933933
repositoryName: 'a./a-a',
934-
})).toThrow(/must follow the specified pattern/);
934+
})).toThrow('Repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, periods and forward slashes');
935935

936936
expect(() => new ecr.Repository(stack, 'Repo4', {
937937
repositoryName: 'a//a-a',
938-
})).toThrow(/must follow the specified pattern/);
938+
})).toThrow('Repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, periods and forward slashes');
939939
});
940940

941941
test('return value addToResourcePolicy', () => {

0 commit comments

Comments
 (0)