From 5f7c2b4d87de512f1d0cdee5d8ef83d6c65ef9a6 Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Thu, 14 Feb 2019 10:52:42 +0000 Subject: [PATCH] Improved error messaging returned from JSON Schema validations refs #10438 --- .../api/v2/utils/validators/utils/json-schema.js | 12 +++++++++++- core/server/translations/en.json | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/core/server/api/v2/utils/validators/utils/json-schema.js b/core/server/api/v2/utils/validators/utils/json-schema.js index 9cf6b29780..d3ba9a32a9 100644 --- a/core/server/api/v2/utils/validators/utils/json-schema.js +++ b/core/server/api/v2/utils/validators/utils/json-schema.js @@ -1,3 +1,4 @@ +const _ = require('lodash'); const Ajv = require('ajv'); const stripKeyword = require('./strip-keyword'); const common = require('../../../../../lib/common'); @@ -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 })); } diff --git a/core/server/translations/en.json b/core/server/translations/en.json index 1f2ff64654..350d6a014d 100644 --- a/core/server/translations/en.json +++ b/core/server/translations/en.json @@ -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." }