From 68d60a183430175dbf0230ab3e2b0899b1b8e956 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 15 Jun 2021 11:20:55 +0100 Subject: [PATCH] Removed i18n from validation modules --- core/server/data/validation/password.js | 11 ++++++++--- core/server/data/validation/schema.js | 16 +++++++++++----- core/server/data/validation/validate.js | 22 +++++++++++++++------- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/core/server/data/validation/password.js b/core/server/data/validation/password.js index a12d5af5fb..61b817d95a 100644 --- a/core/server/data/validation/password.js +++ b/core/server/data/validation/password.js @@ -2,10 +2,15 @@ const _ = require('lodash'); const validator = require('./validator'); -const i18n = require('../../../shared/i18n'); +const tpl = require('@tryghost/tpl'); const settingsCache = require('../../services/settings/cache'); const urlUtils = require('../../../shared/url-utils'); +const messages = { + passwordDoesNotComplyLength: 'Your password must be at least {minLength} characters long.', + passwordDoesNotComplySecurity: 'Sorry, you cannot use an insecure password.' +}; + /** * Counts repeated characters in a string. When 50% or more characters are the same, * we return false and therefore invalidate the string. @@ -72,7 +77,7 @@ function validatePassword(password, email, blogTitle) { // password must be longer than 10 characters if (!validator.isLength(password, 10)) { validationResult.isValid = false; - validationResult.message = i18n.t('errors.models.user.passwordDoesNotComplyLength', {minLength: 10}); + validationResult.message = tpl(messages.passwordDoesNotComplyLength, {minLength: 10}); return validationResult; } @@ -113,7 +118,7 @@ function validatePassword(password, email, blogTitle) { // Generic error message for the rules where no dedicated error massage is set if (!validationResult.isValid && !validationResult.message) { - validationResult.message = i18n.t('errors.models.user.passwordDoesNotComplySecurity'); + validationResult.message = tpl(messages.passwordDoesNotComplySecurity); } return validationResult; diff --git a/core/server/data/validation/schema.js b/core/server/data/validation/schema.js index c5546f86c7..38820cfef8 100644 --- a/core/server/data/validation/schema.js +++ b/core/server/data/validation/schema.js @@ -1,13 +1,19 @@ const _ = require('lodash'); const Promise = require('bluebird'); -const i18n = require('../../../shared/i18n'); +const tpl = require('@tryghost/tpl'); const errors = require('@tryghost/errors'); const schema = require('../schema').tables; const validator = require('./validator'); const validate = require('./validate'); +const messages = { + valueCannotBeBlank: 'Value in [{tableName}.{columnKey}] cannot be blank.', + valueMustBeBoolean: 'Value in [{tableName}.{columnKey}] must be one of true, false, 0 or 1.', + valueExceedsMaxLength: 'Value in [{tableName}.{columnKey}] exceeds maximum length of {maxlength} characters.', + valueIsNotInteger: 'Value in [{tableName}.{columnKey}] is not an integer.' +}; /** * Validate model against schema. * @@ -43,7 +49,7 @@ function validateSchema(tableName, model, options) { !Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'defaultTo') ) { if (validator.empty(strVal)) { - message = i18n.t('notices.data.validation.index.valueCannotBeBlank', { + message = tpl(messages.valueCannotBeBlank, { tableName: tableName, columnKey: columnKey }); @@ -58,7 +64,7 @@ function validateSchema(tableName, model, options) { if (Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'type') && schema[tableName][columnKey].type === 'bool') { if (!(validator.isBoolean(strVal) || validator.empty(strVal))) { - message = i18n.t('notices.data.validation.index.valueMustBeBoolean', { + message = tpl(messages.valueMustBeBoolean, { tableName: tableName, columnKey: columnKey }); @@ -79,7 +85,7 @@ function validateSchema(tableName, model, options) { // check length if (Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'maxlength')) { if (!validator.isLength(strVal, 0, schema[tableName][columnKey].maxlength)) { - message = i18n.t('notices.data.validation.index.valueExceedsMaxLength', + message = tpl(messages.valueExceedsMaxLength, { tableName: tableName, columnKey: columnKey, @@ -100,7 +106,7 @@ function validateSchema(tableName, model, options) { // check type if (Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'type')) { if (schema[tableName][columnKey].type === 'integer' && !validator.isInt(strVal)) { - message = i18n.t('notices.data.validation.index.valueIsNotInteger', { + message = tpl(messages.valueIsNotInteger, { tableName: tableName, columnKey: columnKey }); diff --git a/core/server/data/validation/validate.js b/core/server/data/validation/validate.js index 7740ef3d54..b577ce88a4 100644 --- a/core/server/data/validation/validate.js +++ b/core/server/data/validation/validate.js @@ -1,8 +1,16 @@ const _ = require('lodash'); const validator = require('./validator'); -const i18n = require('../../../shared/i18n'); + +const tpl = require('@tryghost/tpl'); const errors = require('@tryghost/errors'); +const messages = { + validationFailed: 'Validation ({validationName}) failed for {key}', + validationFailedTypes: { + isLength: 'Value in [{tableName}.{key}] exceeds maximum length of {max} characters.' + } +}; + /** * Validate keys using the validator module. * Each validation's key is a method name and its value is an array of options @@ -26,7 +34,7 @@ const errors = require('@tryghost/errors'); */ function validate(value, key, validations, tableName) { const validationErrors = []; - let translation; + let message; value = _.toString(value); _.each(validations, function each(validationOptions, validationName) { @@ -43,22 +51,22 @@ function validate(value, key, validations, tableName) { // equivalent of validator.isSomething(option1, option2) if (validator[validationName].apply(validator, validationOptions) !== goodResult) { - // CASE: You can define specific translations for validators e.g. isLength - if (i18n.doesTranslationKeyExist('notices.data.validation.index.validationFailedTypes.' + validationName)) { - translation = i18n.t('notices.data.validation.index.validationFailedTypes.' + validationName, _.merge({ + // CASE: You can define specific messages for validators e.g. isLength + if (_.has(messages.validationFailedTypes, validationName)) { + message = tpl(messages.validationFailedTypes[validationName], _.merge({ validationName: validationName, key: key, tableName: tableName }, validationOptions[1])); } else { - translation = i18n.t('notices.data.validation.index.validationFailed', { + message = tpl(messages.validationFailed, { validationName: validationName, key: key }); } validationErrors.push(new errors.ValidationError({ - message: translation, + message: message, context: `${tableName}.${key}` })); }