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 to add 'error' function #27

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions proposals/0013-error-function.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Add 'error' function

|||
|---|---
| **JEP** | 20
| **Author** | Sebastien Rosset
| **Created**| 22-March-2023
| **[SemVer](https://semver.org/spec/v2.0.0.html#summary)** | MINOR

## Abstract

This JEP proposes a new function `error()` that raises a runtime error.
This could be used to detect and raise an error when input JSON documents have unexpected data.


## Motivation

AFAIK, there is no dedicated function to raise a runtime error within a JMESpath expression.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The motivation section should go into more detail as to why JMESPath expressions need the ability to raise a runtime error. The existing errors are intended for cases where the user has made a mistake in their expression. JMESPath is primarily a language for filtering/extracting/reshaping JSON data, and not really about schema validation. This section should make a convincing case for that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a section with concrete use cases and improved the motivation section.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamesls , does this address your concerns?



## Specification

Add a new `error` function that takes a string expression.
If the `error` function is evaluated, an error is raised with the specified string message.

## Rationale

Suppose we have the following JSON documents:
```json
{ "id": "eth0", "status": "up" }
{ "id": "eth1", "status": "down" }
```

In this example, the values of `status` are either `up` or `down`.
Suppose the JMESpath author wants to:
1. Map the values of `status` to `0` and `1`.
1. Raise an error when an expected `status` value is encountered. For example, when the value of `status` is set to `degraded`.

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

### Other options that have been considered

1. When appropriate, use a JSON schema to validate the input. However, this may not always be the right tool:
1. The JSON schema may not be available.
1. Constructing a JSON schema may be difficult when the input data is generated by a 3rd-party component.
1. JSON schema validation may have a significant runtime cost, especially for large documents with a complex schema.
1. Leverage the fact that some JMESpath expressions are invalid, e.g., `to_number('bad')` would raise an error. However, this is a kludgy workaround.

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

## Implementation Notes

Are there any details that implementers should try to follow?