2014-02-19 21:32:23 +04:00
|
|
|
|
var schema = require('../schema').tables,
|
|
|
|
|
_ = require('lodash'),
|
|
|
|
|
validator = require('validator'),
|
2014-05-05 17:51:21 +04:00
|
|
|
|
when = require('when'),
|
2014-05-09 14:11:29 +04:00
|
|
|
|
errors = require('../../errors'),
|
2014-02-19 21:32:23 +04:00
|
|
|
|
|
|
|
|
|
validateSchema,
|
|
|
|
|
validateSettings,
|
|
|
|
|
validate;
|
|
|
|
|
|
2014-02-28 10:51:52 +04:00
|
|
|
|
// Provide a few custom validators
|
|
|
|
|
//
|
|
|
|
|
validator.extend('empty', function (str) {
|
|
|
|
|
return _.isEmpty(str);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
validator.extend('notContains', function (str, badString) {
|
|
|
|
|
return !_.contains(str, badString);
|
|
|
|
|
});
|
|
|
|
|
|
2014-02-19 21:32:23 +04:00
|
|
|
|
// Validation validation against schema attributes
|
|
|
|
|
// values are checked against the validation objects
|
|
|
|
|
// form schema.js
|
|
|
|
|
validateSchema = function (tableName, model) {
|
2014-05-05 17:51:21 +04:00
|
|
|
|
var columns = _.keys(schema[tableName]),
|
2014-05-09 14:11:29 +04:00
|
|
|
|
validationErrors = [];
|
2014-02-19 21:32:23 +04:00
|
|
|
|
|
|
|
|
|
_.each(columns, function (columnKey) {
|
2014-05-05 17:51:21 +04:00
|
|
|
|
var message = '';
|
2014-02-19 21:32:23 +04:00
|
|
|
|
// check nullable
|
|
|
|
|
if (model.hasOwnProperty(columnKey) && schema[tableName][columnKey].hasOwnProperty('nullable')
|
|
|
|
|
&& schema[tableName][columnKey].nullable !== true) {
|
2014-02-28 10:51:52 +04:00
|
|
|
|
if (validator.isNull(model[columnKey]) || validator.empty(model[columnKey])) {
|
2014-05-05 17:51:21 +04:00
|
|
|
|
message = 'Value in [' + tableName + '.' + columnKey + '] cannot be blank.';
|
2014-05-09 14:11:29 +04:00
|
|
|
|
validationErrors.push(new errors.ValidationError(message, tableName + '.' + columnKey));
|
2014-02-28 10:51:52 +04:00
|
|
|
|
}
|
2014-02-19 21:32:23 +04:00
|
|
|
|
}
|
|
|
|
|
// TODO: check if mandatory values should be enforced
|
|
|
|
|
if (model[columnKey]) {
|
|
|
|
|
// check length
|
|
|
|
|
if (schema[tableName][columnKey].hasOwnProperty('maxlength')) {
|
2014-02-28 10:51:52 +04:00
|
|
|
|
if (!validator.isLength(model[columnKey], 0, schema[tableName][columnKey].maxlength)) {
|
2014-05-05 17:51:21 +04:00
|
|
|
|
message = 'Value in [' + tableName + '.' + columnKey + '] exceeds maximum length of '
|
|
|
|
|
+ schema[tableName][columnKey].maxlength + ' characters.';
|
2014-05-09 14:11:29 +04:00
|
|
|
|
validationErrors.push(new errors.ValidationError(message, tableName + '.' + columnKey));
|
2014-02-28 10:51:52 +04:00
|
|
|
|
}
|
2014-02-19 21:32:23 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//check validations objects
|
|
|
|
|
if (schema[tableName][columnKey].hasOwnProperty('validations')) {
|
2014-05-09 14:11:29 +04:00
|
|
|
|
validationErrors.concat(validate(model[columnKey], columnKey, schema[tableName][columnKey].validations));
|
2014-02-19 21:32:23 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//check type
|
|
|
|
|
if (schema[tableName][columnKey].hasOwnProperty('type')) {
|
2014-02-28 10:51:52 +04:00
|
|
|
|
if (schema[tableName][columnKey].type === 'integer' && !validator.isInt(model[columnKey])) {
|
2014-05-05 17:51:21 +04:00
|
|
|
|
message = 'Value in [' + tableName + '.' + columnKey + '] is no valid integer.';
|
2014-05-09 14:11:29 +04:00
|
|
|
|
validationErrors.push(new errors.ValidationError(message, tableName + '.' + columnKey));
|
2014-02-19 21:32:23 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-05-05 17:51:21 +04:00
|
|
|
|
|
2014-05-09 14:11:29 +04:00
|
|
|
|
if (validationErrors.length !== 0) {
|
|
|
|
|
return when.reject(validationErrors);
|
2014-05-05 17:51:21 +04:00
|
|
|
|
}
|
2014-02-19 21:32:23 +04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Validation for settings
|
|
|
|
|
// settings are checked against the validation objects
|
|
|
|
|
// form default-settings.json
|
|
|
|
|
validateSettings = function (defaultSettings, model) {
|
|
|
|
|
var values = model.toJSON(),
|
|
|
|
|
matchingDefault = defaultSettings[values.key];
|
|
|
|
|
|
|
|
|
|
if (matchingDefault && matchingDefault.validations) {
|
2014-05-05 17:51:21 +04:00
|
|
|
|
return validate(values.value, values.key, matchingDefault.validations);
|
2014-02-19 21:32:23 +04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-28 10:51:52 +04:00
|
|
|
|
// Validate default settings using the validator module.
|
|
|
|
|
// Each validation's key is a method name and its value is an array of options
|
|
|
|
|
//
|
|
|
|
|
// eg:
|
|
|
|
|
// validations: { isUrl: true, isLength: [20, 40] }
|
|
|
|
|
//
|
|
|
|
|
// will validate that a setting's length is a URL between 20 and 40 chars.
|
|
|
|
|
//
|
|
|
|
|
// If you pass a boolean as the value, it will specify the "good" result. By default
|
|
|
|
|
// the "good" result is assumed to be true.
|
2014-02-19 21:32:23 +04:00
|
|
|
|
//
|
|
|
|
|
// eg:
|
2014-02-28 10:51:52 +04:00
|
|
|
|
// validations: { isNull: false } // means the "good" result would
|
|
|
|
|
// // fail the `isNull` check, so
|
|
|
|
|
// // not null.
|
2014-02-19 21:32:23 +04:00
|
|
|
|
//
|
2014-02-28 10:51:52 +04:00
|
|
|
|
// available validators: https://github.com/chriso/validator.js#validators
|
2014-02-19 21:32:23 +04:00
|
|
|
|
validate = function (value, key, validations) {
|
2014-05-09 14:11:29 +04:00
|
|
|
|
var validationErrors = [];
|
2014-02-19 21:32:23 +04:00
|
|
|
|
_.each(validations, function (validationOptions, validationName) {
|
2014-02-28 10:51:52 +04:00
|
|
|
|
var goodResult = true;
|
2014-02-19 21:32:23 +04:00
|
|
|
|
|
2014-02-28 10:51:52 +04:00
|
|
|
|
if (_.isBoolean(validationOptions)) {
|
|
|
|
|
goodResult = validationOptions;
|
|
|
|
|
validationOptions = [];
|
|
|
|
|
} else if (!_.isArray(validationOptions)) {
|
2014-02-19 21:32:23 +04:00
|
|
|
|
validationOptions = [validationOptions];
|
|
|
|
|
}
|
2014-02-28 10:51:52 +04:00
|
|
|
|
|
|
|
|
|
validationOptions.unshift(value);
|
|
|
|
|
|
|
|
|
|
// equivalent of validator.isSomething(option1, option2)
|
|
|
|
|
if (validator[validationName].apply(validator, validationOptions) !== goodResult) {
|
2014-05-09 14:11:29 +04:00
|
|
|
|
validationErrors.push(new errors.ValidationError('Settings validation (' + validationName + ') failed for ' + key, key));
|
2014-02-28 10:51:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validationOptions.shift();
|
2014-02-19 21:32:23 +04:00
|
|
|
|
}, this);
|
2014-05-05 17:51:21 +04:00
|
|
|
|
|
2014-05-09 14:11:29 +04:00
|
|
|
|
if (validationErrors.length !== 0) {
|
|
|
|
|
return when.reject(validationErrors);
|
2014-05-05 17:51:21 +04:00
|
|
|
|
}
|
2014-02-19 21:32:23 +04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
validateSchema: validateSchema,
|
|
|
|
|
validateSettings: validateSettings
|
2014-02-27 06:44:09 +04:00
|
|
|
|
};
|