josdejong / josdejong/jsoneditor

onError doesn't fire when JSON is in bad state?

Open
#1,277 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12.3k
Forks
2.1k
PR merge metrics
No merged PRs in 30d

Description

As the title says, when the user puts the JSON in a bad state, e.g. delete an object key in Text mode, the jsoneditor's `onError` event doesn't appear to fire. It _does_, however, log a console.error() in that case. This seems to be a deliberate choice, as confirmed by the application doco:

> The [onError] callback is only invoked for errors triggered by a users action, like switching from code mode to tree mode or clicking the Format button whilst the editor doesn't contain valid JSON.

This doesn't seem consistent to me. If putting the JSON in a bad state is an error, then why not fire the `onError` event? And if that's not really an error, then why are we logging it as such in the console?

The use case for this is a downstream project where we have a **Save Config** button, which would post the JSON to an API. Obviously, we don't want the user clicking this button if the JSON is in a bad state. But with the `onError` event not firing in that case, how do we tell what the JSON's state is at any given point? I don't think we can rely on users hitting the **Validate JSON** button.

For the moment, I'm trapping (`try/catch`) the bad JSON error downstream, so I do have a workaround.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.