onValidationError to report any kind of error (#861)

* onValidationError to report any kind of error

* lint fixes
This commit is contained in:
Meir Rotstein 2019-12-08 11:35:22 +02:00 committed by Jos de Jong
parent 7888dcf660
commit 84950ac69a
6 changed files with 38 additions and 9 deletions

View File

@ -165,10 +165,11 @@ Constructs a new JSONEditor.
- `{function} onValidationError(errors)` - `{function} onValidationError(errors)`
Set a callback function for validation errors. Available in all modes. Set a callback function for validation and parse errors. Available in all modes.
On validation of the json, if errors were found this callback is invoked with the validation errors data. On validation of the json, if errors of any kind were found this callback is invoked with the errors data.
Between validations, the callback will be invoked only if the validation errors were changed.
On change, the callback will be invoked only if errors were changed.
Example: Example:
@ -178,6 +179,20 @@ Constructs a new JSONEditor.
* @param {Array} errors validation errors * @param {Array} errors validation errors
*/ */
onValidationError: function (errors) { onValidationError: function (errors) {
errors.forEach((error) => {
switch (error.type) {
case 'validation': // schema validation error
...
break;
case 'customValidation': // custom validation error
...
break;
case 'error': // json parse error
...
break;
...
}
});
... ...
} }
} }

View File

@ -131,7 +131,7 @@
onValidationError: function(errors) { onValidationError: function(errors) {
console.error('onValidationError', errors); console.error('onValidationError', errors);
const outputEL = document.getElementById('onValidationOutput') const outputEL = document.getElementById('onValidationOutput')
outputEL.innerHTML = '<code>onValidationError</code> was called with ' + errors.length + ' errors <br> ' + outputEL.innerHTML = '<code>onValidationError</code> was called with ' + errors.length + ' error' + (errors.length > 1 ? 's' : '') + ' <br> ' +
'open the browser console to see the error objects'; 'open the browser console to see the error objects';
}, },
onValidate: function (json) { onValidate: function (json) {

View File

@ -804,7 +804,6 @@ textmode.validate = function () {
console.error('Custom validation function did throw an error', err) console.error('Custom validation function did throw an error', err)
}) })
} catch (err) { } catch (err) {
this.lastSchemaErrors = undefined
if (this.getText()) { if (this.getText()) {
// try to extract the line number from the jsonlint error message // try to extract the line number from the jsonlint error message
const match = /\w*line\s*(\d+)\w*/g.exec(err.message) const match = /\w*line\s*(\d+)\w*/g.exec(err.message)
@ -820,6 +819,13 @@ textmode.validate = function () {
} }
this._renderErrors(parseErrors) this._renderErrors(parseErrors)
if (typeof this.options.onValidationError === 'function') {
if (isValidationErrorChanged(parseErrors, this.lastSchemaErrors)) {
this.options.onValidationError.call(this, parseErrors)
}
this.lastSchemaErrors = parseErrors
}
} }
} }

View File

@ -563,7 +563,8 @@ treemode.validate = function () {
.map(function findNode (error) { .map(function findNode (error) {
return { return {
node: root.findNode(error.dataPath), node: root.findNode(error.dataPath),
error: error error: error,
type: 'validation'
} }
}) })
.filter(function hasNode (entry) { .filter(function hasNode (entry) {
@ -675,7 +676,8 @@ treemode._validateCustom = function (json) {
return { return {
node: node, node: node,
error: error error: error,
type: 'customValidation'
} }
}) })
.filter(entry => entry && entry.node && entry.error && entry.error.message) .filter(entry => entry && entry.node && entry.error && entry.error.message)

View File

@ -1464,7 +1464,12 @@ export function isValidationErrorChanged (currErr, prevErr) {
if (prevErr.length !== currErr.length) { return true } if (prevErr.length !== currErr.length) { return true }
for (let i = 0; i < currErr.length; ++i) { for (let i = 0; i < currErr.length; ++i) {
const pErr = prevErr.find(p => p.dataPath === currErr[i].dataPath && p.schemaPath === currErr[i].schemaPath) let pErr
if (currErr[i].type === 'error') {
pErr = prevErr.find(p => p.line === currErr[i].line)
} else {
pErr = prevErr.find(p => p.dataPath === currErr[i].dataPath && p.schemaPath === currErr[i].schemaPath)
}
if (!pErr) { if (!pErr) {
return true return true
} }

View File

@ -34,7 +34,8 @@ export function validateCustom (json, onValidate) {
.map(error => // change data structure into the structure matching the JSON schema errors .map(error => // change data structure into the structure matching the JSON schema errors
({ ({
dataPath: stringifyPath(error.path), dataPath: stringifyPath(error.path),
message: error.message message: error.message,
type: 'customValidation'
})) }))
} else { } else {
return [] return []