Skip to content

Commit da2ec75

Browse files
authored
feat(sns): add validation of displayName for topic (#30770)
### 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*
1 parent f9fd00c commit da2ec75

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Diff for: packages/aws-cdk-lib/aws-sns/lib/topic.ts

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export interface TopicProps {
1212
/**
1313
* A developer-defined string that can be used to identify this SNS topic.
1414
*
15+
* The display name must be maximum 100 characters long, including hyphens (-),
16+
* underscores (_), spaces, and tabs.
17+
*
1518
* @default None
1619
*/
1720
readonly displayName?: string;
@@ -296,6 +299,10 @@ export class Topic extends TopicBase {
296299
throw new Error(`signatureVersion must be "1" or "2", received: "${props.signatureVersion}"`);
297300
}
298301

302+
if (props.displayName && !Token.isUnresolved(props.displayName) && props.displayName.length > 100) {
303+
throw new Error(`displayName must be less than or equal to 100 characters, got ${props.displayName.length}`);
304+
}
305+
299306
const resource = new CfnTopic(this, 'Resource', {
300307
archivePolicy: props.messageRetentionPeriodInDays ? {
301308
MessageRetentionPeriod: props.messageRetentionPeriodInDays,

Diff for: packages/aws-cdk-lib/aws-sns/test/sns.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ describe('Topic', () => {
176176
signatureVersion: '3',
177177
})).toThrow(/signatureVersion must be "1" or "2", received: "3"/);
178178
});
179+
180+
test('throw error when displayName is too long', () => {
181+
const stack = new cdk.Stack();
182+
183+
expect(() => {
184+
new sns.Topic(stack, 'MyTopic', {
185+
displayName: 'a'.repeat(101),
186+
});
187+
}).toThrow('displayName must be less than or equal to 100 characters, got 101');
188+
});
179189
});
180190

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

0 commit comments

Comments
 (0)