From 3de9359dc4363c35ccba6d78b24c38a93a0e8197 Mon Sep 17 00:00:00 2001 From: Shiv Lakshminarayan Date: Tue, 14 Jul 2020 19:18:05 -0700 Subject: [PATCH] fix(stepfunctions): Choice state does not allow state input as a condition (#8991) Choice state Conditions are currently restricted to variables that must start with $. or $[, however, the state input of $ is also valid. Conditions evaluate against strings. Currently, we only allow attributes in the state input, but not the entire state input. The state input can be an object or a plain string (i.e. "No messages"). This change relaxes the validation on the Condition to allow the state input '$' to be supplied. This allows a string in a state input to be used in a condition when using Choice states Closes #8990 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/aws-stepfunctions/lib/condition.ts | 4 ++-- packages/@aws-cdk/aws-stepfunctions/test/condition.test.ts | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-stepfunctions/lib/condition.ts b/packages/@aws-cdk/aws-stepfunctions/lib/condition.ts index b447509546014..2f334af06190c 100644 --- a/packages/@aws-cdk/aws-stepfunctions/lib/condition.ts +++ b/packages/@aws-cdk/aws-stepfunctions/lib/condition.ts @@ -177,8 +177,8 @@ enum CompoundOperator { class VariableComparison extends Condition { constructor(private readonly variable: string, private readonly comparisonOperator: ComparisonOperator, private readonly value: any) { super(); - if (!/^\$[.[]/.test(variable)) { - throw new Error(`Variable reference must start with '$.' or '$[', got '${variable}'`); + if (!/^\$|(\$[.[])/.test(variable)) { + throw new Error(`Variable reference must be '$', start with '$.', or start with '$[', got '${variable}'`); } } diff --git a/packages/@aws-cdk/aws-stepfunctions/test/condition.test.ts b/packages/@aws-cdk/aws-stepfunctions/test/condition.test.ts index afde2f815fcde..aef483b4583a5 100644 --- a/packages/@aws-cdk/aws-stepfunctions/test/condition.test.ts +++ b/packages/@aws-cdk/aws-stepfunctions/test/condition.test.ts @@ -11,6 +11,9 @@ describe('Condition Variables', () => { test('Condition variables can start with $[', () => { expect(() => stepfunctions.Condition.stringEquals('$[0]', 'a')).not.toThrow(); }), + test('Condition variables can reference the state input $', () => { + expect(() => stepfunctions.Condition.stringEquals('$', 'a')).not.toThrow(); + }), test('NotConditon must render properly', () => { assertRendersTo(stepfunctions.Condition.not(stepfunctions.Condition.stringEquals('$.a', 'b')), { Not: { Variable: '$.a', StringEquals: 'b' } }); }),