From e15d0c057ac9ee5d0a8cbca72f1256bac2f8282a Mon Sep 17 00:00:00 2001 From: Parker Scanlon <69879391+scanlonp@users.noreply.github.com> Date: Tue, 19 Sep 2023 07:38:39 -0700 Subject: [PATCH] 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* --- packages/aws-cdk-lib/aws-ecr/lib/repository.ts | 8 ++++++-- packages/aws-cdk-lib/aws-ecr/test/repository.test.ts | 10 +++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/aws-cdk-lib/aws-ecr/lib/repository.ts b/packages/aws-cdk-lib/aws-ecr/lib/repository.ts index 39bdb24eca355..ef4fa18d809c9 100644 --- a/packages/aws-cdk-lib/aws-ecr/lib/repository.ts +++ b/packages/aws-cdk-lib/aws-ecr/lib/repository.ts @@ -493,7 +493,11 @@ export interface OnImageScanCompletedOptions extends events.OnEventOptions { export interface RepositoryProps { /** - * Name for this repository + * Name for this repository. + * + * The repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, and forward slashes. + * + * > 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. * * @default Automatically generated name. */ @@ -672,7 +676,7 @@ export class Repository extends RepositoryBase { } const isPatternMatch = /^(?:[a-z0-9]+(?:[._-][a-z0-9]+)*\/)*[a-z0-9]+(?:[._-][a-z0-9]+)*$/.test(repositoryName); if (!isPatternMatch) { - errors.push('Repository name must follow the specified pattern: (?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*'); + errors.push('Repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, periods and forward slashes'); } if (errors.length > 0) { diff --git a/packages/aws-cdk-lib/aws-ecr/test/repository.test.ts b/packages/aws-cdk-lib/aws-ecr/test/repository.test.ts index 32c79d8d16978..002c347688457 100644 --- a/packages/aws-cdk-lib/aws-ecr/test/repository.test.ts +++ b/packages/aws-cdk-lib/aws-ecr/test/repository.test.ts @@ -898,7 +898,7 @@ describe('repository', () => { const expectedErrors = [ `Invalid ECR repository name (value: ${repositoryName})`, 'Repository name must be at least 2 and no more than 256 characters', - 'Repository name must follow the specified pattern: (?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*', + 'Repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, periods and forward slashes', ].join(EOL); expect(() => new ecr.Repository(stack, 'Repo', { @@ -923,19 +923,19 @@ describe('repository', () => { expect(() => new ecr.Repository(stack, 'Repo1', { repositoryName: 'aAa', - })).toThrow(/must follow the specified pattern/); + })).toThrow('Repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, periods and forward slashes'); expect(() => new ecr.Repository(stack, 'Repo2', { repositoryName: 'a--a', - })).toThrow(/must follow the specified pattern/); + })).toThrow('Repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, periods and forward slashes'); expect(() => new ecr.Repository(stack, 'Repo3', { repositoryName: 'a./a-a', - })).toThrow(/must follow the specified pattern/); + })).toThrow('Repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, periods and forward slashes'); expect(() => new ecr.Repository(stack, 'Repo4', { repositoryName: 'a//a-a', - })).toThrow(/must follow the specified pattern/); + })).toThrow('Repository name must start with a letter and can only contain lowercase letters, numbers, hyphens, underscores, periods and forward slashes'); }); test('return value addToResourcePolicy', () => {