jsonpath icon indicating copy to clipboard operation
jsonpath copied to clipboard

Validate JsonPath syntax without context

Open SaeedZhiany opened this issue 6 years ago • 3 comments

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 avatar Nov 13 '19 11:11 SaeedZhiany

@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...
}

movitto avatar May 29 '20 15:05 movitto

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)]

ArfatSalman avatar Sep 07 '20 14:09 ArfatSalman

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).

mcwebb avatar Jun 10 '21 08:06 mcwebb