Skip to content

Commit

Permalink
Improved error messaging for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Jan 14, 2016
1 parent b26eb2e commit e16770e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Supported browsers: Chrome, Firefox, Safari, Opera, Internet Explorer 9+.
- JSON schema validation (powered by [ajv](https://github.com/epoberezkin/ajv)).

### Code editor
- Format and compact JSON.
- Colorized code (powered by [Ace](https://ace.c9.io)).
- Inspect JSON (powered by [Ace](https://ace.c9.io)).
- Format and compact JSON.
- JSON schema validation (powered by [ajv](https://github.com/epoberezkin/ajv)).

### Text editor
Expand Down
6 changes: 5 additions & 1 deletion examples/07_json_schema_validation.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ <h1>JSON schema validation</h1>
"lastName": {
"type": "string"
},
"gender": {
"enum": ["male", "female"]
},
"age": {
"description": "Age in years",
"type": "integer",
Expand All @@ -52,7 +55,8 @@ <h1>JSON schema validation</h1>
var json = {
firstName: 'John',
lastName: 'Doe',
age: 28.5
gender: null,
age: 28
};

var options = {
Expand Down
27 changes: 26 additions & 1 deletion src/js/treemode.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,14 @@ 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 findNode (error) {
return {
node: root.findNode(error.dataPath),
error: error
error: improveErrorMessages(error)
}
})
.filter(function hasNode (entry) {
Expand Down Expand Up @@ -412,6 +414,29 @@ 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.
Expand Down
19 changes: 19 additions & 0 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,25 @@ 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;
};

/**
* Test whether the child rect fits completely inside the parent rect.
* @param {ClientRect} parent
Expand Down

0 comments on commit e16770e

Please sign in to comment.