Hi There,
Is it possible to check if the JSON currently in the editor is valid against the schema?
I note that with the get() function I can tell if the JSON is valid JSON. But its not apparent how how I might check if valid JSON is valid when compared to the schema provided.
There is currently no solution to programmatically check whether JSON is valid according to a schema. Internally, JSONEditor uses ajv for this. Makes sense to expose ajv from JSONEditor.
You can of course load ajv and use that directly.
Hi Jos,
Yes exposing ajv from the json editor would be great. Saves us the hassle of including the library twice into our projects (once as part of jsoneditor, and again within our own package).
I think it might also be helpful to have simple boolean getters for "validJSON" and "validJSONAgainstSchema". Then, unless the developer wants to take a look at errors, this would be good enough to decide the validity of input (if that is something they'd like to check before saving for instance).
If the bytes count, you could use the minimalist bundle of JSONEditor and provide Ace (if needed) and ajv yourself. That prevents having to bundle ajv twice.
It must indeed be configurable whether the developer wants to accept valid JSON with or without schema. Maybe a method getErrors() would be useful: when the JSON is invalid it returns the parse error, and when the JSON is not valid against the schema it would return the schema errors.
Hi Jos,
do you have a plan for the method getErrors()?
thx.
function hasSchemaError() {
var errorCls = ["jsoneditor-validation-error-icon", "jsoneditor-schema-error"];
var hasSchemaError = false;
for (var i = 0; i < errorCls.length && !hasSchemaError; i++) {
var es = document.getElementsByClassName(errorCls[i]);
if (es != null && es.length > 0) {
for (var j = 0; j < es.length; i++) {
var e = es[j];
if (e.style.display != 'none') {
hasSchemaError = true;
break;
}
}
}
}
return hasSchemaError;
}
@houkx it's on the wishlist but I will not have time for it myself short term.
See #612
Closing this issue in favor of #612
Most helpful comment
If the bytes count, you could use the minimalist bundle of JSONEditor and provide Ace (if needed) and ajv yourself. That prevents having to bundle ajv twice.
It must indeed be configurable whether the developer wants to accept valid JSON with or without schema. Maybe a method
getErrors()would be useful: when the JSON is invalid it returns the parse error, and when the JSON is not valid against the schema it would return the schema errors.