mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-11 18:35:22 +03:00
Removed i18n from validation modules
This commit is contained in:
parent
8e46288f37
commit
68d60a1834
@ -2,10 +2,15 @@ const _ = require('lodash');
|
|||||||
|
|
||||||
const validator = require('./validator');
|
const validator = require('./validator');
|
||||||
|
|
||||||
const i18n = require('../../../shared/i18n');
|
const tpl = require('@tryghost/tpl');
|
||||||
const settingsCache = require('../../services/settings/cache');
|
const settingsCache = require('../../services/settings/cache');
|
||||||
const urlUtils = require('../../../shared/url-utils');
|
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,
|
* Counts repeated characters in a string. When 50% or more characters are the same,
|
||||||
* we return false and therefore invalidate the string.
|
* we return false and therefore invalidate the string.
|
||||||
@ -72,7 +77,7 @@ function validatePassword(password, email, blogTitle) {
|
|||||||
// password must be longer than 10 characters
|
// password must be longer than 10 characters
|
||||||
if (!validator.isLength(password, 10)) {
|
if (!validator.isLength(password, 10)) {
|
||||||
validationResult.isValid = false;
|
validationResult.isValid = false;
|
||||||
validationResult.message = i18n.t('errors.models.user.passwordDoesNotComplyLength', {minLength: 10});
|
validationResult.message = tpl(messages.passwordDoesNotComplyLength, {minLength: 10});
|
||||||
|
|
||||||
return validationResult;
|
return validationResult;
|
||||||
}
|
}
|
||||||
@ -113,7 +118,7 @@ function validatePassword(password, email, blogTitle) {
|
|||||||
|
|
||||||
// Generic error message for the rules where no dedicated error massage is set
|
// Generic error message for the rules where no dedicated error massage is set
|
||||||
if (!validationResult.isValid && !validationResult.message) {
|
if (!validationResult.isValid && !validationResult.message) {
|
||||||
validationResult.message = i18n.t('errors.models.user.passwordDoesNotComplySecurity');
|
validationResult.message = tpl(messages.passwordDoesNotComplySecurity);
|
||||||
}
|
}
|
||||||
|
|
||||||
return validationResult;
|
return validationResult;
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const Promise = require('bluebird');
|
const Promise = require('bluebird');
|
||||||
|
|
||||||
const i18n = require('../../../shared/i18n');
|
const tpl = require('@tryghost/tpl');
|
||||||
const errors = require('@tryghost/errors');
|
const errors = require('@tryghost/errors');
|
||||||
|
|
||||||
const schema = require('../schema').tables;
|
const schema = require('../schema').tables;
|
||||||
const validator = require('./validator');
|
const validator = require('./validator');
|
||||||
const validate = require('./validate');
|
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.
|
* Validate model against schema.
|
||||||
*
|
*
|
||||||
@ -43,7 +49,7 @@ function validateSchema(tableName, model, options) {
|
|||||||
!Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'defaultTo')
|
!Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'defaultTo')
|
||||||
) {
|
) {
|
||||||
if (validator.empty(strVal)) {
|
if (validator.empty(strVal)) {
|
||||||
message = i18n.t('notices.data.validation.index.valueCannotBeBlank', {
|
message = tpl(messages.valueCannotBeBlank, {
|
||||||
tableName: tableName,
|
tableName: tableName,
|
||||||
columnKey: columnKey
|
columnKey: columnKey
|
||||||
});
|
});
|
||||||
@ -58,7 +64,7 @@ function validateSchema(tableName, model, options) {
|
|||||||
if (Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'type')
|
if (Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'type')
|
||||||
&& schema[tableName][columnKey].type === 'bool') {
|
&& schema[tableName][columnKey].type === 'bool') {
|
||||||
if (!(validator.isBoolean(strVal) || validator.empty(strVal))) {
|
if (!(validator.isBoolean(strVal) || validator.empty(strVal))) {
|
||||||
message = i18n.t('notices.data.validation.index.valueMustBeBoolean', {
|
message = tpl(messages.valueMustBeBoolean, {
|
||||||
tableName: tableName,
|
tableName: tableName,
|
||||||
columnKey: columnKey
|
columnKey: columnKey
|
||||||
});
|
});
|
||||||
@ -79,7 +85,7 @@ function validateSchema(tableName, model, options) {
|
|||||||
// check length
|
// check length
|
||||||
if (Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'maxlength')) {
|
if (Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'maxlength')) {
|
||||||
if (!validator.isLength(strVal, 0, 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,
|
tableName: tableName,
|
||||||
columnKey: columnKey,
|
columnKey: columnKey,
|
||||||
@ -100,7 +106,7 @@ function validateSchema(tableName, model, options) {
|
|||||||
// check type
|
// check type
|
||||||
if (Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'type')) {
|
if (Object.prototype.hasOwnProperty.call(schema[tableName][columnKey], 'type')) {
|
||||||
if (schema[tableName][columnKey].type === 'integer' && !validator.isInt(strVal)) {
|
if (schema[tableName][columnKey].type === 'integer' && !validator.isInt(strVal)) {
|
||||||
message = i18n.t('notices.data.validation.index.valueIsNotInteger', {
|
message = tpl(messages.valueIsNotInteger, {
|
||||||
tableName: tableName,
|
tableName: tableName,
|
||||||
columnKey: columnKey
|
columnKey: columnKey
|
||||||
});
|
});
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const validator = require('./validator');
|
const validator = require('./validator');
|
||||||
const i18n = require('../../../shared/i18n');
|
|
||||||
|
const tpl = require('@tryghost/tpl');
|
||||||
const errors = require('@tryghost/errors');
|
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.
|
* Validate keys using the validator module.
|
||||||
* Each validation's key is a method name and its value is an array of options
|
* 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) {
|
function validate(value, key, validations, tableName) {
|
||||||
const validationErrors = [];
|
const validationErrors = [];
|
||||||
let translation;
|
let message;
|
||||||
value = _.toString(value);
|
value = _.toString(value);
|
||||||
|
|
||||||
_.each(validations, function each(validationOptions, validationName) {
|
_.each(validations, function each(validationOptions, validationName) {
|
||||||
@ -43,22 +51,22 @@ function validate(value, key, validations, tableName) {
|
|||||||
|
|
||||||
// equivalent of validator.isSomething(option1, option2)
|
// equivalent of validator.isSomething(option1, option2)
|
||||||
if (validator[validationName].apply(validator, validationOptions) !== goodResult) {
|
if (validator[validationName].apply(validator, validationOptions) !== goodResult) {
|
||||||
// CASE: You can define specific translations for validators e.g. isLength
|
// CASE: You can define specific messages for validators e.g. isLength
|
||||||
if (i18n.doesTranslationKeyExist('notices.data.validation.index.validationFailedTypes.' + validationName)) {
|
if (_.has(messages.validationFailedTypes, validationName)) {
|
||||||
translation = i18n.t('notices.data.validation.index.validationFailedTypes.' + validationName, _.merge({
|
message = tpl(messages.validationFailedTypes[validationName], _.merge({
|
||||||
validationName: validationName,
|
validationName: validationName,
|
||||||
key: key,
|
key: key,
|
||||||
tableName: tableName
|
tableName: tableName
|
||||||
}, validationOptions[1]));
|
}, validationOptions[1]));
|
||||||
} else {
|
} else {
|
||||||
translation = i18n.t('notices.data.validation.index.validationFailed', {
|
message = tpl(messages.validationFailed, {
|
||||||
validationName: validationName,
|
validationName: validationName,
|
||||||
key: key
|
key: key
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
validationErrors.push(new errors.ValidationError({
|
validationErrors.push(new errors.ValidationError({
|
||||||
message: translation,
|
message: message,
|
||||||
context: `${tableName}.${key}`
|
context: `${tableName}.${key}`
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user