From 4104043f8c3ce556c4a98579991f4ef860c966b6 Mon Sep 17 00:00:00 2001 From: mazyu36 Date: Thu, 1 Aug 2024 04:02:08 +0900 Subject: [PATCH] chore(location): add validation for description property in PlaceIndex class (#30974) ### Issue # (if applicable) N/A ### Reason for this change Add validation for PlaceIndex description, similar to #30648, #30682, and #30711 . ### Description of changes Add validation and unit tests for description. ### Description of how you validated changes Add unit tests. ### 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* --- .../aws-location-alpha/lib/place-index.ts | 7 +++++ .../test/place-index.test.ts | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/packages/@aws-cdk/aws-location-alpha/lib/place-index.ts b/packages/@aws-cdk/aws-location-alpha/lib/place-index.ts index ae62abe343bea..144059b6469a2 100644 --- a/packages/@aws-cdk/aws-location-alpha/lib/place-index.ts +++ b/packages/@aws-cdk/aws-location-alpha/lib/place-index.ts @@ -29,6 +29,9 @@ export interface PlaceIndexProps { /** * A name for the place index * + * Must be between 1 and 100 characters and contain only alphanumeric characters, + * hyphens, periods and underscores. + * * @default - A name is automatically generated */ readonly placeIndexName?: string; @@ -175,6 +178,10 @@ export class PlaceIndex extends PlaceIndexBase { public readonly placeIndexUpdateTime: string; constructor(scope: Construct, id: string, props: PlaceIndexProps = {}) { + if (props.description && !Token.isUnresolved(props.description) && props.description.length > 1000) { + throw new Error(`\`description\` must be between 0 and 1000 characters. Received: ${props.description.length} characters`); + } + if (props.placeIndexName && !Token.isUnresolved(props.placeIndexName) && !/^[-.\w]{1,100}$/.test(props.placeIndexName)) { throw new Error(`Invalid place index name. The place index name must be between 1 and 100 characters and contain only alphanumeric characters, hyphens, periods and underscores. Received: ${props.placeIndexName}`); } diff --git a/packages/@aws-cdk/aws-location-alpha/test/place-index.test.ts b/packages/@aws-cdk/aws-location-alpha/test/place-index.test.ts index 763ff44c573f6..a600705339445 100644 --- a/packages/@aws-cdk/aws-location-alpha/test/place-index.test.ts +++ b/packages/@aws-cdk/aws-location-alpha/test/place-index.test.ts @@ -17,6 +17,34 @@ test('create a place index', () => { }); }); +test('create a place index with description', () => { + new PlaceIndex(stack, 'PlaceIndex', { + description: 'my-description', + }); + + Template.fromStack(stack).hasResourceProperties('AWS::Location::PlaceIndex', { + DataSource: 'Esri', + IndexName: 'PlaceIndex', + Description: 'my-description', + }); +}); + +test('creates a place index with empty description', () => { + new PlaceIndex(stack, 'PlaceIndex', { + description: '', + }); + + Template.fromStack(stack).hasResourceProperties('AWS::Location::PlaceIndex', { + Description: '', + }); +}); + +test('throws with invalid description', () => { + expect(() => new PlaceIndex(stack, 'PlaceIndex', { + description: 'a'.repeat(1001), + })).toThrow('`description` must be between 0 and 1000 characters. Received: 1001 characters'); +}); + test('throws with invalid name', () => { expect(() => new PlaceIndex(stack, 'PlaceIndex', { placeIndexName: 'inv@lid',