mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-27 22:49:56 -05:00
Cleaned up data validation module
no-issue Removes overly verbose repetition of identifiers making this easier to read
This commit is contained in:
parent
774187ced5
commit
38fce4efc5
1 changed files with 13 additions and 17 deletions
|
@ -8,10 +8,6 @@ const {i18n} = require('../../lib/common');
|
||||||
const errors = require('@tryghost/errors');
|
const errors = require('@tryghost/errors');
|
||||||
const settingsCache = require('../../services/settings/cache');
|
const settingsCache = require('../../services/settings/cache');
|
||||||
const urlUtils = require('../../../shared/url-utils');
|
const urlUtils = require('../../../shared/url-utils');
|
||||||
let validatePassword;
|
|
||||||
let validateSchema;
|
|
||||||
let validateSettings;
|
|
||||||
let validate;
|
|
||||||
|
|
||||||
function assertString(input) {
|
function assertString(input) {
|
||||||
assert(typeof input === 'string', 'Validator js validates strings only');
|
assert(typeof input === 'string', 'Validator js validates strings only');
|
||||||
|
@ -92,7 +88,7 @@ validator.extend('isSlug', function isSlug(str) {
|
||||||
* invalid password: `validationResult: {isValid: false, message: 'Sorry, you cannot use an insecure password.'}`
|
* invalid password: `validationResult: {isValid: false, message: 'Sorry, you cannot use an insecure password.'}`
|
||||||
* valid password: `validationResult: {isValid: true}`
|
* valid password: `validationResult: {isValid: true}`
|
||||||
*/
|
*/
|
||||||
validatePassword = function validatePassword(password, email, blogTitle) {
|
function validatePassword(password, email, blogTitle) {
|
||||||
const validationResult = {isValid: true};
|
const validationResult = {isValid: true};
|
||||||
const disallowedPasswords = ['password', 'ghost', 'passw0rd'];
|
const disallowedPasswords = ['password', 'ghost', 'passw0rd'];
|
||||||
let blogUrl = urlUtils.urlFor('home', true);
|
let blogUrl = urlUtils.urlFor('home', true);
|
||||||
|
@ -159,7 +155,7 @@ validatePassword = function validatePassword(password, email, blogTitle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return validationResult;
|
return validationResult;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate model against schema.
|
* Validate model against schema.
|
||||||
|
@ -176,7 +172,7 @@ validatePassword = function validatePassword(password, email, blogTitle) {
|
||||||
* ## on model add
|
* ## on model add
|
||||||
* - validate everything to catch required fields
|
* - validate everything to catch required fields
|
||||||
*/
|
*/
|
||||||
validateSchema = function validateSchema(tableName, model, options) {
|
function validateSchema(tableName, model, options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
const columns = _.keys(schema[tableName]);
|
const columns = _.keys(schema[tableName]);
|
||||||
|
@ -271,12 +267,12 @@ validateSchema = function validateSchema(tableName, model, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
};
|
}
|
||||||
|
|
||||||
// Validation for settings
|
// Validation for settings
|
||||||
// settings are checked against the validation objects
|
// settings are checked against the validation objects
|
||||||
// form default-settings.json
|
// form default-settings.json
|
||||||
validateSettings = function validateSettings(defaultSettings, model) {
|
function validateSettings(defaultSettings, model) {
|
||||||
const values = model.toJSON();
|
const values = model.toJSON();
|
||||||
let validationErrors = [];
|
let validationErrors = [];
|
||||||
const matchingDefault = defaultSettings[values.key];
|
const matchingDefault = defaultSettings[values.key];
|
||||||
|
@ -290,7 +286,7 @@ validateSettings = function validateSettings(defaultSettings, model) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate keys using the validator module.
|
* Validate keys using the validator module.
|
||||||
|
@ -313,7 +309,7 @@ validateSettings = function validateSettings(defaultSettings, model) {
|
||||||
* @param {String} tableName (optional) the db table of the value to validate, used for error message.
|
* @param {String} tableName (optional) the db table of the value to validate, used for error message.
|
||||||
* @return {Array} returns an Array including the found validation errors (empty if none found);
|
* @return {Array} returns an Array including the found validation errors (empty if none found);
|
||||||
*/
|
*/
|
||||||
validate = function validate(value, key, validations, tableName) {
|
function validate(value, key, validations, tableName) {
|
||||||
const validationErrors = [];
|
const validationErrors = [];
|
||||||
let translation;
|
let translation;
|
||||||
value = _.toString(value);
|
value = _.toString(value);
|
||||||
|
@ -356,12 +352,12 @@ validate = function validate(value, key, validations, tableName) {
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
return validationErrors;
|
return validationErrors;
|
||||||
};
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
validate: validate,
|
validate,
|
||||||
validator: validator,
|
validator,
|
||||||
validatePassword: validatePassword,
|
validatePassword,
|
||||||
validateSchema: validateSchema,
|
validateSchema,
|
||||||
validateSettings: validateSettings
|
validateSettings
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue