diff --git a/src/js/textmode.js b/src/js/textmode.js index 2e09695..6a9b061 100644 --- a/src/js/textmode.js +++ b/src/js/textmode.js @@ -403,12 +403,13 @@ textmode.validate = function () { // only validate the JSON when parsing the JSON succeeded if (doValidate && this.validateSchema) { - //console.time('validate'); // TODO: clean up time measurement var valid = this.validateSchema(json); - //console.timeEnd('validate'); - if (!valid) { - errors = this.validateSchema.errors; + var schema = this.options.schema; + errors = this.validateSchema.errors + .map(function (error) { + return util.improveSchemaError(schema, error); + }); } } diff --git a/src/js/treemode.js b/src/js/treemode.js index dc2e28d..2c6e221 100644 --- a/src/js/treemode.js +++ b/src/js/treemode.js @@ -374,10 +374,13 @@ treemode.validate = function () { // apply all new errors schemaErrors = this.validateSchema.errors + .map(function (error) { + return util.improveSchemaError(schema, error); + }) .map(function findNode (error) { return { node: root.findNode(error.dataPath), - error: improveErrorMessages(error) + error: error } }) .filter(function hasNode (entry) { @@ -414,29 +417,6 @@ treemode.validate = function () { }); }; -/** - * Improve the error messages of JSON schema errors - * @param {Object} error - * @return {Object} The error - */ -function improveErrorMessages (error) { - if (error.keyword === 'enum') { - var enums = util.getFromSchema(schema, error.schemaPath); - if (enums) { - enums = enums.map(function (value) { - return JSON.stringify(value); - }); - - if (enums.length > 5) { - enums = enums.slice(0, 5).concat(['(' + (enums.length - 5) + ' more...)']); - } - error.message = 'should be equal to one of: ' + enums.join(', '); - } - } - - return error; -} - /** * Start autoscrolling when given mouse position is above the top of the * editor contents, or below the bottom. diff --git a/src/js/util.js b/src/js/util.js index 738ca26..a762cd1 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -679,6 +679,32 @@ exports.getFromSchema = function (schema, schemaPath) { return obj; }; +/** + * Improve the error message of a JSON schema error + * @param {Object} schema + * @param {Object} error + * @return {Object} The error + */ +exports.improveSchemaError = function (schema, error) { + if (error.keyword === 'enum') { + var enums = exports.getFromSchema(schema, error.schemaPath); + if (enums) { + enums = enums.map(function (value) { + return JSON.stringify(value); + }); + + if (enums.length > 5) { + var more = ['(' + (enums.length - 5) + ' more...)']; + enums = enums.slice(0, 5); + enums.push(more); + } + error.message = 'should be equal to one of: ' + enums.join(', '); + } + } + + return error; +}; + /** * Test whether the child rect fits completely inside the parent rect. * @param {ClientRect} parent