Skip to content

Commit

Permalink
Improved error messaging for enums in code mode
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Jan 14, 2016
1 parent e16770e commit 1387169
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
9 changes: 5 additions & 4 deletions src/js/textmode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}

Expand Down
28 changes: 4 additions & 24 deletions src/js/treemode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1387169

Please sign in to comment.