diff --git a/HISTORY.md b/HISTORY.md index 2f07e48..3fdeb44 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -5,6 +5,8 @@ https://github.com/josdejong/jsoneditor ## not yet released, version 5.1.1 +- Fixed #257: Improving error messages for enum errors failed when the + schema contains references. - Fixed #255: Removed wrong console warning about the option `search`. - Fixed error thrown when option `search` is false (see #256). Thanks @MiroHibler. diff --git a/docs/api.md b/docs/api.md index 226cf75..0295fc4 100644 --- a/docs/api.md +++ b/docs/api.md @@ -42,7 +42,7 @@ Constructs a new JSONEditor. ```js var options = { - ajv: Ajv({ allErrors: true }) + ajv: Ajv({ allErrors: true, verbose: true }) } ``` diff --git a/src/js/JSONEditor.js b/src/js/JSONEditor.js index a09c779..5e68532 100644 --- a/src/js/JSONEditor.js +++ b/src/js/JSONEditor.js @@ -276,7 +276,7 @@ JSONEditor.prototype.setSchema = function (schema) { var ajv; try { // grab ajv from options if provided, else create a new instance - ajv = this.options.ajv || Ajv({ allErrors: true }); + ajv = this.options.ajv || Ajv({ allErrors: true, verbose: true }); } catch (err) { diff --git a/src/js/textmode.js b/src/js/textmode.js index 6a9b061..8e8de00 100644 --- a/src/js/textmode.js +++ b/src/js/textmode.js @@ -405,11 +405,9 @@ textmode.validate = function () { if (doValidate && this.validateSchema) { var valid = this.validateSchema(json); if (!valid) { - var schema = this.options.schema; - errors = this.validateSchema.errors - .map(function (error) { - return util.improveSchemaError(schema, error); - }); + errors = this.validateSchema.errors.map(function (error) { + return util.improveSchemaError(error); + }); } } diff --git a/src/js/treemode.js b/src/js/treemode.js index 0614d8d..3f759fa 100644 --- a/src/js/treemode.js +++ b/src/js/treemode.js @@ -372,12 +372,10 @@ treemode.validate = function () { if (this.validateSchema) { var valid = this.validateSchema(root.getValue()); if (!valid) { - var schema = this.options.schema; - // apply all new errors schemaErrors = this.validateSchema.errors .map(function (error) { - return util.improveSchemaError(schema, error); + return util.improveSchemaError(error); }) .map(function findNode (error) { return { diff --git a/src/js/util.js b/src/js/util.js index a762cd1..fed7030 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -660,34 +660,14 @@ exports.parsePath = function parsePath(jsonPath) { return [prop].concat(parsePath(remainder)) }; -/** - * Retrieve a part of the schema - * @param {Object} schema - * @param {string} schemaPath A path like "#/properties/gender/enum" - * @return {Object} Returns the found part of the schema, or undefined when not found. - */ -exports.getFromSchema = function (schema, schemaPath) { - var path = schemaPath.split('/'); - path.shift(); // remove the first # - - var obj = schema; - var prop; - while (prop = path.shift()) { - obj = obj[prop]; - } - - 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); +exports.improveSchemaError = function (error) { + if (error.keyword === 'enum' && Array.isArray(error.schema)) { + var enums = error.schema; if (enums) { enums = enums.map(function (value) { return JSON.stringify(value);