You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
AFAIK, there is no built-in capability to raise errors from within a JMESpath expression. For example, suppose a JSON document has a status property, and its value is supposed to be up or down. The user wants to map up to 1 and down to 0, which could be done using the following JMESpath expression:
((status == 'up') && `1`) || `0`)
The user may want to detect and raise an error when the input document has a value other than up or down. A workaround is to rely on the fact that some JMESpath expressions are invalid, e.g., to_number('bad') would raise an error.
This works but it's very kludgy. In this specific example, one might argue this issue could be validated using a JSON schema, but sometimes there are no JSON schemas.
Proposal
Add a new error function that takes a string expression. If the error function is evaluated, an error is raised with the specified message.
Problem Statement
AFAIK, there is no built-in capability to raise errors from within a JMESpath expression. For example, suppose a JSON document has a
status
property, and its value is supposed to beup
ordown
. The user wants to mapup
to1
anddown
to0
, which could be done using the following JMESpath expression:The user may want to detect and raise an error when the input document has a value other than
up
ordown
. A workaround is to rely on the fact that some JMESpath expressions are invalid, e.g.,to_number('bad')
would raise an error.This works but it's very kludgy. In this specific example, one might argue this issue could be validated using a JSON schema, but sometimes there are no JSON schemas.
Proposal
Add a new
error
function that takes a string expression. If theerror
function is evaluated, an error is raised with the specified message.Additional Information
The spec states errors are raised when problems are encountered during the evaluation process. Currently, the following errors are defined:
invalid-type
unknown-function
invalid-arity
Related: jmespath/jmespath.site#115 , jmespath/jmespath.site#116
The text was updated successfully, but these errors were encountered: