Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(ecr): make validateRepositoryName errors human readable #27186

Merged
merged 11 commits into from
Sep 19, 2023
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