Skip to content
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

[Proposal] add error function #22

Open
sebastien-rosset opened this issue Mar 19, 2023 · 1 comment
Open

[Proposal] add error function #22

sebastien-rosset opened this issue Mar 19, 2023 · 1 comment

Comments

@sebastien-rosset
Copy link

sebastien-rosset commented Mar 19, 2023

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

((status == 'up') && `1`) || ((status == 'down') && `0`) || to_number('bad')

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.

((status == 'up') && `1`) || ((status == 'down') && `0`) || error(join('invalid-value:', [status]))

Additional Information

The spec states errors are raised when problems are encountered during the evaluation process. Currently, the following errors are defined:

  1. invalid-type
  2. unknown-function
  3. invalid-arity

Related: jmespath/jmespath.site#115 , jmespath/jmespath.site#116

@jamesls
Copy link
Member

jamesls commented Mar 30, 2023

Moving over to jmespath.jep repo for consideration.

@jamesls jamesls transferred this issue from jmespath/jmespath.site Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants