Fixed #257: Improving error messages for enum errors failed when the schema contains references.

This commit is contained in:
jos 2016-01-16 10:46:43 +01:00
parent 42acb71907
commit ae2f4e923f
6 changed files with 11 additions and 33 deletions

View File

@ -5,6 +5,8 @@ https://github.com/josdejong/jsoneditor
## not yet released, version 5.1.1 ## 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 #255: Removed wrong console warning about the option `search`.
- Fixed error thrown when option `search` is false (see #256). Thanks @MiroHibler. - Fixed error thrown when option `search` is false (see #256). Thanks @MiroHibler.

View File

@ -42,7 +42,7 @@ Constructs a new JSONEditor.
```js ```js
var options = { var options = {
ajv: Ajv({ allErrors: true }) ajv: Ajv({ allErrors: true, verbose: true })
} }
``` ```

View File

@ -276,7 +276,7 @@ JSONEditor.prototype.setSchema = function (schema) {
var ajv; var ajv;
try { try {
// grab ajv from options if provided, else create a new instance // 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) { catch (err) {

View File

@ -405,11 +405,9 @@ textmode.validate = function () {
if (doValidate && this.validateSchema) { if (doValidate && this.validateSchema) {
var valid = this.validateSchema(json); var valid = this.validateSchema(json);
if (!valid) { if (!valid) {
var schema = this.options.schema; errors = this.validateSchema.errors.map(function (error) {
errors = this.validateSchema.errors return util.improveSchemaError(error);
.map(function (error) { });
return util.improveSchemaError(schema, error);
});
} }
} }

View File

@ -372,12 +372,10 @@ treemode.validate = function () {
if (this.validateSchema) { if (this.validateSchema) {
var valid = this.validateSchema(root.getValue()); var valid = this.validateSchema(root.getValue());
if (!valid) { if (!valid) {
var schema = this.options.schema;
// apply all new errors // apply all new errors
schemaErrors = this.validateSchema.errors schemaErrors = this.validateSchema.errors
.map(function (error) { .map(function (error) {
return util.improveSchemaError(schema, error); return util.improveSchemaError(error);
}) })
.map(function findNode (error) { .map(function findNode (error) {
return { return {

View File

@ -660,34 +660,14 @@ exports.parsePath = function parsePath(jsonPath) {
return [prop].concat(parsePath(remainder)) 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 * Improve the error message of a JSON schema error
* @param {Object} schema
* @param {Object} error * @param {Object} error
* @return {Object} The error * @return {Object} The error
*/ */
exports.improveSchemaError = function (schema, error) { exports.improveSchemaError = function (error) {
if (error.keyword === 'enum') { if (error.keyword === 'enum' && Array.isArray(error.schema)) {
var enums = exports.getFromSchema(schema, error.schemaPath); var enums = error.schema;
if (enums) { if (enums) {
enums = enums.map(function (value) { enums = enums.map(function (value) {
return JSON.stringify(value); return JSON.stringify(value);