Skip to content

Commit

Permalink
feat(ecr): make validateRepositoryName errors human readable (#27186)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
scanlonp authored and Hogan Bobertz committed Sep 19, 2023
1 parent 5ac1241 commit b6e8a28
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 6 additions & 2 deletions packages/aws-cdk-lib/aws-ecr/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions packages/aws-cdk-lib/aws-ecr/test/repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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', () => {
Expand Down

0 comments on commit b6e8a28

Please sign in to comment.