Improved error messaging for enums in code mode

This commit is contained in:
jos 2016-01-14 21:17:03 +01:00
parent e16770ebc2
commit 1387169948
3 changed files with 35 additions and 28 deletions

View File

@ -403,12 +403,13 @@ textmode.validate = function () {
// only validate the JSON when parsing the JSON succeeded // only validate the JSON when parsing the JSON succeeded
if (doValidate && this.validateSchema) { if (doValidate && this.validateSchema) {
//console.time('validate'); // TODO: clean up time measurement
var valid = this.validateSchema(json); var valid = this.validateSchema(json);
//console.timeEnd('validate');
if (!valid) { if (!valid) {
errors = this.validateSchema.errors; var schema = this.options.schema;
errors = this.validateSchema.errors
.map(function (error) {
return util.improveSchemaError(schema, error);
});
} }
} }

View File

@ -374,10 +374,13 @@ treemode.validate = function () {
// apply all new errors // apply all new errors
schemaErrors = this.validateSchema.errors schemaErrors = this.validateSchema.errors
.map(function (error) {
return util.improveSchemaError(schema, error);
})
.map(function findNode (error) { .map(function findNode (error) {
return { return {
node: root.findNode(error.dataPath), node: root.findNode(error.dataPath),
error: improveErrorMessages(error) error: error
} }
}) })
.filter(function hasNode (entry) { .filter(function hasNode (entry) {
@ -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 * Start autoscrolling when given mouse position is above the top of the
* editor contents, or below the bottom. * editor contents, or below the bottom.

View File

@ -679,6 +679,32 @@ exports.getFromSchema = function (schema, schemaPath) {
return obj; 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. * Test whether the child rect fits completely inside the parent rect.
* @param {ClientRect} parent * @param {ClientRect} parent