Skip to content

feat(step-functions): add Jsonata class with helper methods for typed JSONata expressions#36879

Draft
iliapolo wants to merge 2 commits intomainfrom
fix-34228
Draft

feat(step-functions): add Jsonata class with helper methods for typed JSONata expressions#36879
iliapolo wants to merge 2 commits intomainfrom
fix-34228

Conversation

@iliapolo
Copy link
Contributor

@iliapolo iliapolo commented Feb 4, 2026

Issue # (if applicable)

Closes #34228.

Reason for this change

When using JSONata with Step Functions tasks like BatchSubmitJob, properties typed as number (such as arraySize) cannot accept JSONata expression strings directly, resulting in TypeScript compilation errors. While JsonPath.numberAt() exists for JSONPath expressions, there was no equivalent helper for JSONata expressions.

This forces users to use @ts-expect-error workarounds, which bypasses type safety and provides a poor developer experience.

Description of changes

Added a new public Jsonata class to the aws-stepfunctions module with static helper methods that mirror the existing JsonPath class pattern:

  • Jsonata.numberAt(expression) - Returns a number token for JSONata expressions where numbers are expected
  • Jsonata.stringAt(expression) - Returns a string token for JSONata expressions where strings are expected
  • Jsonata.listAt(expression) - Returns a list token for JSONata expressions where lists are expected
  • Jsonata.objectAt(expression) - Returns an IResolvable for JSONata expressions where objects are expected

Implementation details:

  • Added JsonataToken class (internal) that implements IResolvable and wraps expressions with {% ... %} delimiters on resolution
  • Added validateJsonataExpression() function that validates expressions are non-empty and don't include delimiters (which are added automatically)
  • Uses CDK's Token system to allow JSONata expressions to satisfy TypeScript type constraints

Usage example:

import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';

// Before (TypeScript error)
tasks.BatchSubmitJob.jsonata(this, 'Job', {
  jobDefinitionArn: 'arn:aws:batch:...',
  jobName: 'myJob',
  jobQueueArn: 'arn:aws:batch:...',
  arraySize: '{% $states.input.batchSize %}',  // ❌ Type 'string' is not assignable to type 'number'
});

// After (works correctly)
tasks.BatchSubmitJob.jsonata(this, 'Job', {
  jobDefinitionArn: 'arn:aws:batch:...',
  jobName: 'myJob',
  jobQueueArn: 'arn:aws:batch:...',
  arraySize: sfn.Jsonata.numberAt('$states.input.batchSize'),  // ✅ Works!
});

Describe any new or updated permissions being added

N/A - No IAM permission changes. This is a CDK type system enhancement only.

Description of how you validated changes

  • Unit tests: Added 18 new unit tests in fields.test.ts covering:
    • All four helper methods (numberAt, stringAt, listAt, objectAt)
    • Token resolution to correct {% ... %} wrapped expressions
    • Validation error cases (empty expressions, expressions with delimiters)
    • Complex JSONata expressions (function calls, arithmetic, string concatenation)
  • Integration tests: Added test in submit-job.test.ts verifying BatchSubmitJob.jsonata() correctly accepts Jsonata.numberAt() for arraySize property

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license


Additional Context

Related Issues

Dependencies

No new dependencies added.

Documentation Updates

  • JSDoc comments added to all public methods with usage examples
  • Links to AWS documentation for JSONata in Step Functions

Follow-up Work

This pattern can be applied to other Step Functions task properties that need JSONata support for typed values.

Review Guidelines

Focus Areas

  1. API Design: Verify the Jsonata class API mirrors JsonPath appropriately
  2. Token Resolution: Ensure JsonataToken.resolve() correctly wraps expressions with {% ... %} delimiters
  3. Validation: Confirm validation catches common user errors (empty expressions, included delimiters)
  4. JSII Compatibility: All public APIs use JSII-compatible types

Testing Notes

  • All 70 tests in fields.test.ts pass
  • Integration test in submit-job.test.ts verifies end-to-end usage with BatchSubmitJob
  • No snapshot changes to existing tests (purely additive change)

Risk Assessment

  • Low Risk: Purely additive change with no modifications to existing code paths
  • No Breaking Changes: Existing behavior is completely unchanged
  • Well-Tested: Comprehensive unit test coverage for all new functionality

@aws-cdk-automation aws-cdk-automation requested a review from a team February 4, 2026 17:38
@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p2 labels Feb 4, 2026
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Feb 4, 2026
@iliapolo
Copy link
Contributor Author

iliapolo commented Feb 4, 2026

This PR was created with Kiro and is here for evaluation purposes. We have not yet decided whether we want to introduce this into the CDK.

Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter fails with the following errors:

❌ Features must contain a change to a README file.
❌ Features must contain a change to an integration test file and the resulting snapshot.

If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/medium Medium work item – several days of effort p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

aws_stepfunction_tasks: BatchSubmitJob task doesn't support JSONata expression for arraySize

2 participants