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
- 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.

View File

@ -42,7 +42,7 @@ Constructs a new JSONEditor.
```js
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;
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) {

View File

@ -405,10 +405,8 @@ 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);
});
}
}

View File

@ -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 {

View File

@ -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);