Skip to content

Commit

Permalink
Improved error messaging returned from JSON Schema validations
Browse files Browse the repository at this point in the history
refs #10438
  • Loading branch information
naz committed Feb 14, 2019
1 parent fa42a71 commit 5f7c2b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion core/server/api/v2/utils/validators/utils/json-schema.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const _ = require('lodash');
const Ajv = require('ajv');
const stripKeyword = require('./strip-keyword');
const common = require('../../../../../lib/common');
Expand All @@ -14,8 +15,17 @@ const validate = (schema, definitions, data) => {
validation(data);

if (validation.errors) {
let key;
const dataPath = _.get(validation, 'errors[0].dataPath');

if (dataPath) {
key = dataPath.split('.').pop();
}

return Promise.reject(new common.errors.ValidationError({
message: common.i18n.t('notices.data.validation.index.validationFailed'),
message: common.i18n.t('notices.data.validation.index.schemaValidationFailed', {
key: key
}),
errorDetails: validation.errors
}));
}
Expand Down
1 change: 1 addition & 0 deletions core/server/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@
"valueIsNotInteger": "Value in [{tableName}.{columnKey}] is not an integer.",
"themeCannotBeActivated": "{themeName} cannot be activated because it is not currently installed.",
"validationFailed": "Validation ({validationName}) failed for {key}",
"schemaValidationFailed": "Validation failed for '{key}'",
"validationFailedTypes": {
"isLength": "Value in [{tableName}.{key}] exceeds maximum length of {max} characters."
}
Expand Down

0 comments on commit 5f7c2b4

Please sign in to comment.