diff --git a/packages/@aws-cdk/aws-stepfunctions/lib/fields.ts b/packages/@aws-cdk/aws-stepfunctions/lib/fields.ts index 407f93cd3d162..9064fb93d2b6e 100644 --- a/packages/@aws-cdk/aws-stepfunctions/lib/fields.ts +++ b/packages/@aws-cdk/aws-stepfunctions/lib/fields.ts @@ -214,8 +214,13 @@ export class FieldUtils { } function validateJsonPath(path: string) { - if (path !== '$' && !path.startsWith('$.') && path !== '$$' && !path.startsWith('$$.')) { - throw new Error(`JSON path values must be exactly '$', '$$', start with '$.' or start with '$$.' Received: ${path}`); + if (path !== '$' + && !path.startsWith('$.') + && path !== '$$' + && !path.startsWith('$$.') + && !path.startsWith('$[') + ) { + throw new Error(`JSON path values must be exactly '$', '$$', start with '$.', start with '$$.' or start with '$[' Received: ${path}`); } } diff --git a/packages/@aws-cdk/aws-stepfunctions/test/fields.test.ts b/packages/@aws-cdk/aws-stepfunctions/test/fields.test.ts index ef6d15fd86519..1235dc9e5d526 100644 --- a/packages/@aws-cdk/aws-stepfunctions/test/fields.test.ts +++ b/packages/@aws-cdk/aws-stepfunctions/test/fields.test.ts @@ -2,6 +2,8 @@ import '@aws-cdk/assert/jest'; import { FieldUtils, JsonPath } from '../lib'; describe('Fields', () => { + const jsonPathValidationErrorMsg = /exactly '\$', '\$\$', start with '\$.', start with '\$\$.' or start with '\$\['/; + test('deep replace correctly handles fields in arrays', () => { expect( FieldUtils.renderObject({ @@ -70,16 +72,18 @@ describe('Fields', () => { test('datafield path must be correct', () => { expect(JsonPath.stringAt('$')).toBeDefined(); - expect(() => JsonPath.stringAt('$hello')).toThrowError(/exactly '\$', '\$\$', start with '\$.' or start with '\$\$.'/); - - expect(() => JsonPath.stringAt('hello')).toThrowError(/exactly '\$', '\$\$', start with '\$.' or start with '\$\$.'/); + expect(() => JsonPath.stringAt('$hello')).toThrowError(jsonPathValidationErrorMsg); + expect(() => JsonPath.stringAt('hello')).toThrowError(jsonPathValidationErrorMsg); }), test('context path must be correct', () => { expect(JsonPath.stringAt('$$')).toBeDefined(); - expect(() => JsonPath.stringAt('$$hello')).toThrowError(/exactly '\$', '\$\$', start with '\$.' or start with '\$\$.'/); - - expect(() => JsonPath.stringAt('hello')).toThrowError(/exactly '\$', '\$\$', start with '\$.' or start with '\$\$.'/); + expect(() => JsonPath.stringAt('$$hello')).toThrowError(jsonPathValidationErrorMsg); + expect(() => JsonPath.stringAt('hello')).toThrowError(jsonPathValidationErrorMsg); + }), + test('datafield path with array must be correct', () => { + expect(JsonPath.stringAt('$[0]')).toBeDefined(); + expect(JsonPath.stringAt("$['abc']")).toBeDefined(); }), test('test contains task token', () => { expect(true).toEqual(