Skip to content

Commit

Permalink
feat(sns): add validation of displayName for topic (#30770)
Browse files Browse the repository at this point in the history
### Issue # (if applicable)

--

### Reason for this change

Display errors before deploying

### Description of changes

- Implement length check for displayName (maximum 100 characters long)
    - [AWS::SNS::Topic](https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-sns-topic.html#cfn-sns-topic-displayname) 

### Description of how you validated changes

I added unit tests and confirmed all tests passed.

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
sosomuse committed Jul 29, 2024
1 parent f9fd00c commit da2ec75
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-sns/lib/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export interface TopicProps {
/**
* A developer-defined string that can be used to identify this SNS topic.
*
* The display name must be maximum 100 characters long, including hyphens (-),
* underscores (_), spaces, and tabs.
*
* @default None
*/
readonly displayName?: string;
Expand Down Expand Up @@ -296,6 +299,10 @@ export class Topic extends TopicBase {
throw new Error(`signatureVersion must be "1" or "2", received: "${props.signatureVersion}"`);
}

if (props.displayName && !Token.isUnresolved(props.displayName) && props.displayName.length > 100) {
throw new Error(`displayName must be less than or equal to 100 characters, got ${props.displayName.length}`);
}

const resource = new CfnTopic(this, 'Resource', {
archivePolicy: props.messageRetentionPeriodInDays ? {
MessageRetentionPeriod: props.messageRetentionPeriodInDays,
Expand Down
10 changes: 10 additions & 0 deletions packages/aws-cdk-lib/aws-sns/test/sns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ describe('Topic', () => {
signatureVersion: '3',
})).toThrow(/signatureVersion must be "1" or "2", received: "3"/);
});

test('throw error when displayName is too long', () => {
const stack = new cdk.Stack();

expect(() => {
new sns.Topic(stack, 'MyTopic', {
displayName: 'a'.repeat(101),
});
}).toThrow('displayName must be less than or equal to 100 characters, got 101');
});
});

test('can add a policy to the topic', () => {
Expand Down

0 comments on commit da2ec75

Please sign in to comment.