Skip to content

Commit

Permalink
fix(core-stage): Fix stage name check to be aligned with stack name
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyokk committed Nov 25, 2022
1 parent cc957d6 commit a7cfdc1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const MY_STACK_CACHE = Symbol.for('@aws-cdk/core.Stack.myStack');

export const STACK_RESOURCE_LIMIT_CONTEXT = '@aws-cdk/core:stackResourceLimit';

const VALID_STACK_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;
export const VALID_STACK_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;

const MAX_RESOURCES = 500;

Expand Down
5 changes: 3 additions & 2 deletions packages/@aws-cdk/core/lib/stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IConstruct, Construct, Node } from 'constructs';
import { Environment } from './environment';
import { PermissionsBoundary } from './permissions-boundary';
import { synthesize } from './private/synthesis';
import { VALID_STACK_NAME_REGEX } from './stack';

const STAGE_SYMBOL = Symbol.for('@aws-cdk/core.Stage');

Expand Down Expand Up @@ -140,8 +141,8 @@ export class Stage extends Construct {
constructor(scope: Construct, id: string, props: StageProps = {}) {
super(scope, id);

if (id !== '' && !/^[a-z][a-z0-9\-\_\.]+$/i.test(id)) {
throw new Error(`invalid stage name "${id}". Stage name must start with a letter and contain only alphanumeric characters, hypens ('-'), underscores ('_') and periods ('.')`);
if (id !== '' && !VALID_STACK_NAME_REGEX.test(id)) {
throw new Error(`invalid stage name "${id}". Stage name must start with a letter and contain only alphanumeric characters and hypens ('-')`);
}

Object.defineProperty(this, STAGE_SYMBOL, { value: true });
Expand Down
7 changes: 3 additions & 4 deletions packages/@aws-cdk/core/test/stage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,15 @@ describe('stage', () => {
const app = new App();

new Stage(app, 'abcd');
new Stage(app, 'abcd123');
new Stage(app, 'Abcd123');
new Stage(app, 'abcd123-588dfjjk');
new Stage(app, 'abcd123-588dfjjk.sss');
new Stage(app, 'abcd123-588dfjjk.sss_ajsid');
new Stage(app, 'Abcd123-588dfjjk-Sss');
new Stage(app, 'f');

expect(() => new Stage(app, 'abcd123-588dfjjk.sss_ajsid ')).toThrow(/invalid stage name "abcd123-588dfjjk.sss_ajsid "/);
expect(() => new Stage(app, 'abcd123-588dfjjk.sss_ajsid/dfo')).toThrow(/invalid stage name "abcd123-588dfjjk.sss_ajsid\/dfo"/);
expect(() => new Stage(app, '&')).toThrow(/invalid stage name "&"/);
expect(() => new Stage(app, '45hello')).toThrow(/invalid stage name "45hello"/);
expect(() => new Stage(app, 'f')).toThrow(/invalid stage name "f"/);
});

test('outdir cannot be specified for nested stages', () => {
Expand Down

0 comments on commit a7cfdc1

Please sign in to comment.