Skip to content

Commit

Permalink
chore(location): add validation for description property in PlaceInde…
Browse files Browse the repository at this point in the history
…x 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*
  • Loading branch information
mazyu36 authored Jul 31, 2024
1 parent c185194 commit 4104043
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-location-alpha/lib/place-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}`);
}
Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-location-alpha/test/place-index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 4104043

Please sign in to comment.