Validate JsonPath syntax without context
I want to validate a user given JsonPath from its syntax aspect. in other words, I want to make sure the user inputs a valid JsonPath structure, no matter what is the Json object itself. (Json object will create in future in the system and it's no available when the user inputs the JsonPath.)
Is that feature available in this library to validate a JsonPath?
@SaeedZhiany
const jp = require('jsonpath')
const expression = "$.foo.bar"
try{
jp.parse(expression)
// expression is valid!
}catch(err){
// ruh-oh! you passed a bad expression...
}
There are cases when the jp.parse says the expression is valid, but jp.paths or nodes throws an error. For example,
jp.parse('$..[?(@.open_issues 10)]'); // parses successfully
jp.nodes(data, '$..[?(@.open_issues 10)]'); // throws an exception
The correct expression should be $..[?(@.open_issues > 10)]
There doesn't appear to be any generic way to validate a path with a filter expression.
This doesn't throw:
jp.paths({}, "$.items[?([‘Foo’, ‘Bar’].includes(@.xyz))].baz");
While this does throw:
jp.paths({ items: [] }, "$.items[?([‘Foo’, ‘Bar’].includes(@.xyz))].baz");
Note that in the above examples the issue is that the ‘ char is used instead of '.
Seems like the filter expression is only evaluated if there's something to filter (the items array in this case).