-
Notifications
You must be signed in to change notification settings - Fork 31
fix(jsonata): add JSONata syntax validation and support #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ChristopheBougere
merged 3 commits into
ChristopheBougere:main
from
aidanprior:feature/jsonata-syntax-validation
May 6, 2025
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/__tests__/definitions/invalid-json-surround-syntax-extra-spaces.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Comment": "JSONata Expressions shouldn't have extra spaces", | ||
| "StartAt": "X", | ||
| "States": { | ||
| "X": { | ||
| "Type": "Pass", | ||
| "Output": "{% \n10 %}", | ||
| "QueryLanguage": "JSONata", | ||
| "End": true | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/__tests__/definitions/invalid-jsonata-surround-syntax-no-close.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Comment": "JSONata Expressions should be surrounded like so: \"{% <JSONata expression> %}\"", | ||
| "StartAt": "X", | ||
| "States": { | ||
| "X": { | ||
| "Type": "Pass", | ||
| "Output": "{% 10 %", | ||
| "QueryLanguage": "JSONata", | ||
| "End": true | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/__tests__/definitions/invalid-jsonata-surround-syntax-no-open.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Comment": "JSONata Expressions should be surrounded like so: \"{% <JSONata expression> %}\"", | ||
| "StartAt": "X", | ||
| "States": { | ||
| "X": { | ||
| "Type": "Pass", | ||
| "Output": "10 %}", | ||
| "QueryLanguage": "JSONata", | ||
| "End": true | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/__tests__/definitions/invalid-jsonata-surround-syntax-no-spaces.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Comment": "JSONata Expressions should be surrounded like so: \"{% <JSONata expression> %}\"", | ||
| "StartAt": "X", | ||
| "States": { | ||
| "X": { | ||
| "Type": "Pass", | ||
| "Output": "{%10%}", | ||
| "QueryLanguage": "JSONata", | ||
| "End": true | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/__tests__/definitions/invalid-jsonata-syntax-arithmetic.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Comment": "JSONata bad arithmetic error", | ||
| "StartAt": "X", | ||
| "States": { | ||
| "X": { | ||
| "Type": "Pass", | ||
| "Output": "{% 10 + * 2 %}", | ||
| "QueryLanguage": "JSONata", | ||
| "End": true | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/__tests__/definitions/invalid-jsonata-syntax-jsonpath-in-jsonata-surround.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Comment": "JSONPath Syntax inside of a JSONata surround should not be valid", | ||
| "StartAt": "X", | ||
| "States": { | ||
| "X": { | ||
| "Type": "Pass", | ||
| "Output": "{% $.store.book[?(@.price < 10)].title %}", | ||
| "QueryLanguage": "JSONata", | ||
| "End": true | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/__tests__/definitions/invalid-jsonata-syntax-mismatched-parens.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Comment": "Missing the closing paren on `sum` function call", | ||
| "StartAt": "X", | ||
| "States": { | ||
| "X": { | ||
| "Type": "Pass", | ||
| "Output": "{% $sum($states.input.items.numbers %", | ||
| "QueryLanguage": "JSONata", | ||
| "End": true | ||
| } | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
src/__tests__/definitions/invalid-jsonata-syntax-variable-reference.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Comment": "JSONata Expressions should be surrounded like so: \"{% <JSONata expression> %}\"", | ||
| "StartAt": "X", | ||
| "States": { | ||
| "X": { | ||
| "Type": "Pass", | ||
| "Output": "{% $sum($states.input.items.numbers %", | ||
| "QueryLanguage": "JSONata", | ||
| "End": true | ||
| } | ||
| } | ||
|
aidanprior marked this conversation as resolved.
Outdated
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Comment": "JSONata Expressions should be surrounded like so: \"{% <JSONata expression> %}\"", | ||
| "StartAt": "X", | ||
| "States": { | ||
| "X": { | ||
| "Type": "Pass", | ||
| "Output": "{% 10 %}", | ||
| "QueryLanguage": "JSONata", | ||
| "End": true | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/__tests__/definitions/valid-jsonata-syntax-function-call.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Comment": "JSONata function calls", | ||
| "StartAt": "X", | ||
| "States": { | ||
| "X": { | ||
| "Type": "Pass", | ||
| "Output": "{% $sum($states.input.numbers) %}", | ||
| "QueryLanguage": "JSONata", | ||
| "End": true | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/__tests__/definitions/valid-jsonata-syntax-path-expression.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Comment": "JSONata path expression", | ||
| "StartAt": "X", | ||
| "States": { | ||
| "X": { | ||
| "Type": "Pass", | ||
| "Output": "{% $states.input.data.items %}", | ||
| "QueryLanguage": "JSONata", | ||
| "End": true | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/__tests__/definitions/valid-jsonata-syntax-simple-arithmetic.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "Comment": "JSONata simple arithmetic", | ||
| "StartAt": "X", | ||
| "States": { | ||
| "X": { | ||
| "Type": "Pass", | ||
| "Output": "{% 1+2 %}", | ||
| "QueryLanguage": "JSONata", | ||
| "End": true | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import jsonata from "jsonata"; | ||
| import { | ||
| StateMachine, | ||
| StateMachineError, | ||
| StateMachineErrorCode, | ||
| } from "../types"; | ||
| import { getStates } from "./get-states"; | ||
|
|
||
| type Value = string | number | boolean | Record<string, unknown>; | ||
|
|
||
| function checkField( | ||
| stateName: string, | ||
| fieldName: string, | ||
| value: Value | ||
| ): StateMachineError[] { | ||
| if (typeof value === "object") | ||
|
aidanprior marked this conversation as resolved.
Outdated
|
||
| if (Array.isArray(value)) | ||
| return value.flatMap((entry, index) => | ||
| checkField(stateName, `${fieldName}[${index}]`, entry as Value) | ||
| ); | ||
| else | ||
| Object.entries(value).flatMap(([innerFieldName, innerValue]) => | ||
| checkField( | ||
| stateName, | ||
| fieldName + "/" + innerFieldName, | ||
| innerValue as Value | ||
| ) | ||
| ); | ||
| else if (typeof value === "string") | ||
| if (value.startsWith("{%") || value.endsWith("%}")) | ||
| if (/\{% .* %\}/.test(value)) { | ||
| try { | ||
| jsonata(value.match(/\{% (.*) %\}/)?.[1] ?? ""); | ||
| return []; | ||
| } catch (error) { | ||
| return [ | ||
| { | ||
| "Error code": StateMachineErrorCode.InvalidJsonataSyntax, | ||
| Message: `Invalid JSONata syntax in ${stateName}/${fieldName}: ${ | ||
| (error as Error).message | ||
| }`, | ||
| }, | ||
| ]; | ||
| } | ||
| } else | ||
| return [ | ||
| { | ||
| "Error code": StateMachineErrorCode.InvalidJsonataSyntax, | ||
| Message: `Invalid JSONata syntax in ${stateName}/${fieldName}: JSONata surround syntax incomplete. Should be: "{% <JSONata expression> %}"`, | ||
| }, | ||
| ]; | ||
| else return []; | ||
| else if (typeof value === "boolean") return []; | ||
| else if (typeof value === "number") return []; | ||
| // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
| else | ||
| throw Error( | ||
| `JSONata Syntax Validation Error: ${stateName}/${fieldName} is of type '${typeof value}' (${value}) which is not expected!` | ||
|
aidanprior marked this conversation as resolved.
|
||
| ); | ||
| return []; | ||
| } | ||
|
|
||
| export function validateJsonataSyntax( | ||
| definition: StateMachine | ||
| ): StateMachineError[] { | ||
| return getStates(definition.States) | ||
| .filter( | ||
| (entry) => | ||
| (entry.state.QueryLanguage ?? | ||
| definition.QueryLanguage ?? | ||
| "JSONPath") === "JSONata" | ||
| ) | ||
| .flatMap(({ state, stateName }) => | ||
| Object.entries(state).flatMap(([fieldName, value]) => | ||
| checkField(stateName, fieldName, value as Value) | ||
| ) | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.