Removed i18n from validation modules

This commit is contained in:
Hannah Wolfe 2021-06-15 11:20:55 +01:00
parent 8e46288f37
commit 68d60a1834
No known key found for this signature in database
GPG Key ID: 9F8C7532D0A6BA55
3 changed files with 34 additions and 15 deletions

View File

@ -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;

View File

@ -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
});

View File

@ -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}`
}));
}