loopback-datasource-juggler/lib/validations.js

823 lines
22 KiB
JavaScript
Raw Normal View History

var util = require('util');
var extend = util._extend;
2014-03-12 23:28:46 +00:00
/*!
2013-04-11 23:23:34 +00:00
* Module exports
*/
2013-04-04 15:31:07 +00:00
exports.ValidationError = ValidationError;
2014-03-12 23:28:46 +00:00
exports.Validatable = Validatable;
2011-10-10 13:22:51 +00:00
2012-03-27 14:22:24 +00:00
/**
2014-03-12 23:28:46 +00:00
* Validation mixins for LoopBack models.
2012-03-27 14:22:24 +00:00
*
2014-03-12 23:28:46 +00:00
* This class provides methods that add validation cababilities to models.
* Each of this validations run when `obj.isValid()` method called.
2012-03-27 14:22:24 +00:00
*
* Each configurator can accept *n* params (*n*-1 field names and one config). Config
* is {Object} depends on specific validation, but all of them have a
* `message` member property. It can be just string, when only one situation possible,
* For example: `Post.validatesPresenceOf('title', { message: 'can not be blank' });`
2012-03-27 14:22:24 +00:00
*
* In more complicated cases it can be {Hash} of messages (for each case):
* `User.validatesLengthOf('password', { min: 6, max: 20, message: {min: 'too short', max: 'too long'}});`
2014-03-12 23:28:46 +00:00
* @class Validatable
2012-03-27 14:22:24 +00:00
*/
2013-05-28 05:20:30 +00:00
function Validatable() {
}
2011-10-10 13:22:51 +00:00
2012-03-27 14:22:24 +00:00
/**
* Validate presence of one or more specified properties.
* Requires a model to include a property to be considered valid; fails when validated field is blank.
2012-03-27 14:22:24 +00:00
*
2014-03-12 23:28:46 +00:00
* For example, validate presence of title
2012-03-27 19:48:23 +00:00
* ```
* Post.validatesPresenceOf('title');
* ```
* Validate that model has first, last, and age properties:
* ```
* User.validatesPresenceOf('first', 'last', 'age');
* ```
* Example with custom message
2012-03-27 19:48:23 +00:00
* ```
2014-03-12 23:28:46 +00:00
* Post.validatesPresenceOf('title', {message: 'Cannot be blank'});
2012-03-27 19:48:23 +00:00
* ```
*
* @param {String} propertyName One or more property names.
* @options {Object} errMsg Optional custom error message. Default is "can't be blank"
* @property {String} message Error message to use instead of default.
2012-03-27 14:22:24 +00:00
*/
2011-10-10 13:22:51 +00:00
Validatable.validatesPresenceOf = getConfigurator('presence');
2012-03-27 14:22:24 +00:00
/**
* Validate absence of one or more specified properties.
* A model should not include a property to be considered valid; fails when validated field not blank.
*
* For example, validate absence of reserved
* ```
* Post.validatesAbsenceOf('reserved', { unless: 'special' });
*
* @param {String} propertyName One or more property names.
* @options {Object} errMsg Optional custom error message. Default is "can't be set"
* @property {String} message Error message to use instead of default.
*/
Validatable.validatesAbsenceOf = getConfigurator('absence');
2012-03-27 14:22:24 +00:00
/**
* Validate length. Require a property length to be within a specified range.
* Three kinds of validations: min, max, is.
2012-03-27 14:22:24 +00:00
*
* Default error messages:
*
* - min: too short
* - max: too long
* - is: length is wrong
*
2014-03-12 23:28:46 +00:00
* Example: length validations
2012-03-27 19:48:23 +00:00
* ```
* User.validatesLengthOf('password', {min: 7});
* User.validatesLengthOf('email', {max: 100});
* User.validatesLengthOf('state', {is: 2});
* User.validatesLengthOf('nick', {min: 3, max: 15});
* ```
2014-03-12 23:28:46 +00:00
* Example: length validations with custom error messages
2012-03-27 19:48:23 +00:00
* ```
* User.validatesLengthOf('password', {min: 7, message: {min: 'too weak'}});
* User.validatesLengthOf('state', {is: 2, message: {is: 'is not valid state name'}});
* ```
* @param {String} propertyName Property name to validate.
* @options {Object} Options
* @property {Number} is Value that property must equal to validate.
* @property {Number} min Value that property must be less than to be valid.
* @property {Number} max Value that property must be less than to be valid.
* @property {Object} message Optional Object with string properties for custom error message for each validation: is, min, or max
2012-03-27 14:22:24 +00:00
*/
2011-10-10 13:22:51 +00:00
Validatable.validatesLengthOf = getConfigurator('length');
2012-03-27 14:22:24 +00:00
/**
* Validate numericality. Requires a value for property to be either an integer or number.
2012-03-27 14:22:24 +00:00
*
2014-03-12 23:28:46 +00:00
* Example
2012-03-27 19:48:23 +00:00
* ```
* User.validatesNumericalityOf('age', { message: { number: '...' }});
* User.validatesNumericalityOf('age', {int: true, message: { int: '...' }});
* ```
2012-03-27 14:22:24 +00:00
*
* @param {String} propertyName Property name to validate.
* @options {Object} Options
* @property {Boolean} int If true, then property must be an integer to be valid.
* @property {Object} message Optional object with string properties for 'int' for integer validation. Default error messages:
2012-03-27 14:22:24 +00:00
* - number: is not a number
* - int: is not an integer
*/
2011-10-10 13:22:51 +00:00
Validatable.validatesNumericalityOf = getConfigurator('numericality');
2012-03-27 14:22:24 +00:00
/**
* Validate inclusion in set. Require a value for property to be in the specified array.
2012-03-27 14:22:24 +00:00
*
2014-03-12 23:28:46 +00:00
* Example:
2012-03-27 19:48:23 +00:00
* ```
* User.validatesInclusionOf('gender', {in: ['male', 'female']});
* User.validatesInclusionOf('role', {
* in: ['admin', 'moderator', 'user'], message: 'is not allowed'
* });
* ```
2012-03-27 14:22:24 +00:00
*
* @param {String} propertyName Property name to validate.
* @options {Object} Options
* @property {Array} in Array Property must match one of the values in the array to be valid.
* @property {String} message Optional error message if property is not valid. Default error message: "is not included in the list".
2012-03-27 14:22:24 +00:00
*/
2011-10-10 13:22:51 +00:00
Validatable.validatesInclusionOf = getConfigurator('inclusion');
2012-03-27 14:22:24 +00:00
/**
* Validate exclusion. Require a property value not be in the specified array.
2012-03-27 14:22:24 +00:00
*
2014-03-12 23:28:46 +00:00
* Example: `Company.validatesExclusionOf('domain', {in: ['www', 'admin']});`
2012-03-27 14:22:24 +00:00
*
* @param {String} propertyName Property name to validate.
* @options {Object} Options
* @property {Array} in Array Property must match one of the values in the array to be valid.
* @property {String} message Optional error message if property is not valid. Default error message: "is reserved".
2012-03-27 14:22:24 +00:00
*/
2011-10-10 13:22:51 +00:00
Validatable.validatesExclusionOf = getConfigurator('exclusion');
2012-03-27 14:22:24 +00:00
/**
* Validate format. Require a model to include a property that matches the given format.
*
* Require a model to include a property that matches the given format. Example:
* `User.validatesFormat('name', {with: /\w+/});`
*
* @param {String} propertyName Property name to validate.
* @options {Object} Options
* @property {RegExp} with Regular expression to validate format.
* @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
2012-03-27 14:22:24 +00:00
*/
2011-10-10 17:34:50 +00:00
Validatable.validatesFormatOf = getConfigurator('format');
2012-03-27 14:22:24 +00:00
/**
* Validate using custom validator
*
* Default error message: is invalid
*
2013-02-12 09:05:04 +00:00
* Example:
*
* User.validate('name', customValidator, {message: 'Bad name'});
* function customValidator(err) {
* if (this.name === 'bad') err();
* });
* var user = new User({name: 'Peter'});
* user.isValid(); // true
* user.name = 'bad';
* user.isValid(); // false
*
2012-03-27 14:22:24 +00:00
* @nocode
* @see helper/validateCustom
*/
2011-11-18 18:59:46 +00:00
Validatable.validate = getConfigurator('custom');
2012-03-27 14:22:24 +00:00
/**
* Validate using custom async validator
*
* Default error message: is invalid
*
2013-02-12 09:05:04 +00:00
* Example:
2014-03-13 23:26:29 +00:00
*```js
2013-02-12 09:05:04 +00:00
* User.validateAsync('name', customValidator, {message: 'Bad name'});
* function customValidator(err, done) {
* process.nextTick(function () {
* if (this.name === 'bad') err();
* done();
* });
* });
* var user = new User({name: 'Peter'});
* user.isValid(); // false (because async validation setup)
* user.isValid(function (isValid) {
* isValid; // true
* })
* user.name = 'bad';
* user.isValid(); // false
* user.isValid(function (isValid) {
* isValid; // false
* })
2014-03-13 23:26:29 +00:00
*```
2012-03-27 14:22:24 +00:00
* @async
* @nocode
* @see helper/validateCustom
*/
2011-11-19 05:43:02 +00:00
Validatable.validateAsync = getConfigurator('custom', {async: true});
2012-03-27 14:22:24 +00:00
/**
* Validate uniqueness. Ensure the value for property is unique in the collection of models.
* Not available for all connectors. Currently supported with these connectors:
* - In Memory
* - Oracle
* - MongoDB
*
* ```
* // The login must be unique across all User instances.
* User.validatesUniquenessOf('login');
*
* // Assuming SiteUser.belongsTo(Site)
* // The login must be unique within each Site.
* SiteUser.validateUniquenessOf('login', { scopedTo: ['siteId'] });
* ```
* @param {String} propertyName Property name to validate.
* @options {Object} Options
* @property {RegExp} with Regular expression to validate format.
* @property {Array.<String>} scopedTo List of properties defining the scope.
* @property {String} message Optional error message if property is not valid. Default error message: "is not unique".
2012-03-27 14:22:24 +00:00
*/
2011-11-19 08:58:49 +00:00
Validatable.validatesUniquenessOf = getConfigurator('uniqueness', {async: true});
2011-10-10 13:22:51 +00:00
2011-10-11 19:52:03 +00:00
// implementation of validators
/*!
2012-03-27 14:22:24 +00:00
* Presence validator
*/
function validatePresence(attr, conf, err) {
2014-01-24 17:09:53 +00:00
if (blank(this[attr])) {
err();
}
2012-03-27 14:22:24 +00:00
}
2011-10-11 19:52:03 +00:00
/*!
* Absence validator
*/
function validateAbsence(attr, conf, err) {
if (!blank(this[attr])) {
err();
}
}
/*!
2012-03-27 14:22:24 +00:00
* Length validator
*/
function validateLength(attr, conf, err) {
2014-01-24 17:09:53 +00:00
if (nullCheck.call(this, attr, conf, err)) return;
var len = this[attr].length;
if (conf.min && len < conf.min) {
err('min');
}
if (conf.max && len > conf.max) {
err('max');
}
if (conf.is && len !== conf.is) {
err('is');
}
2012-03-27 14:22:24 +00:00
}
2011-10-11 19:52:03 +00:00
/*!
2012-03-27 14:22:24 +00:00
* Numericality validator
*/
function validateNumericality(attr, conf, err) {
2014-01-24 17:09:53 +00:00
if (nullCheck.call(this, attr, conf, err)) return;
if (typeof this[attr] !== 'number') {
return err('number');
}
if (conf.int && this[attr] !== Math.round(this[attr])) {
return err('int');
}
2012-03-27 14:22:24 +00:00
}
/*!
2012-03-27 14:22:24 +00:00
* Inclusion validator
*/
function validateInclusion(attr, conf, err) {
2014-01-24 17:09:53 +00:00
if (nullCheck.call(this, attr, conf, err)) return;
2012-03-27 14:22:24 +00:00
2014-01-24 17:09:53 +00:00
if (!~conf.in.indexOf(this[attr])) {
err()
}
2012-03-27 14:22:24 +00:00
}
/*!
2012-03-27 14:22:24 +00:00
* Exclusion validator
*/
function validateExclusion(attr, conf, err) {
2014-01-24 17:09:53 +00:00
if (nullCheck.call(this, attr, conf, err)) return;
2012-03-27 14:22:24 +00:00
2014-01-24 17:09:53 +00:00
if (~conf.in.indexOf(this[attr])) {
err()
}
2012-03-27 14:22:24 +00:00
}
/*!
2012-03-27 14:22:24 +00:00
* Format validator
*/
function validateFormat(attr, conf, err) {
2014-01-24 17:09:53 +00:00
if (nullCheck.call(this, attr, conf, err)) return;
2012-03-27 14:22:24 +00:00
2014-01-24 17:09:53 +00:00
if (typeof this[attr] === 'string') {
if (!this[attr].match(conf['with'])) {
err();
2011-10-11 19:52:03 +00:00
}
2014-01-24 17:09:53 +00:00
} else {
err();
}
2012-03-27 14:22:24 +00:00
}
/*!
2012-03-27 14:22:24 +00:00
* Custom validator
*/
function validateCustom(attr, conf, err, done) {
2014-01-24 17:09:53 +00:00
conf.customValidator.call(this, err, done);
2012-03-27 14:22:24 +00:00
}
2011-10-11 19:52:03 +00:00
/*!
2012-03-27 14:22:24 +00:00
* Uniqueness validator
*/
function validateUniqueness(attr, conf, err, done) {
2014-07-11 21:55:15 +00:00
if (blank(this[attr])) {
return process.nextTick(done);
}
2014-01-24 17:09:53 +00:00
var cond = {where: {}};
cond.where[attr] = this[attr];
if (conf && conf.scopedTo) {
conf.scopedTo.forEach(function(k) {
var val = this[k];
if (val !== undefined)
cond.where[k] = this[k];
}, this);
}
2014-01-24 17:09:53 +00:00
this.constructor.find(cond, function (error, found) {
if (error) {
return err();
}
if (found.length > 1) {
err();
} else if (found.length === 1 && (!this.id || !found[0].id || found[0].id.toString() != this.id.toString())) {
err();
}
done();
}.bind(this));
2012-03-27 14:22:24 +00:00
}
var validators = {
2014-01-24 17:09:53 +00:00
presence: validatePresence,
absence: validateAbsence,
2014-01-24 17:09:53 +00:00
length: validateLength,
numericality: validateNumericality,
inclusion: validateInclusion,
exclusion: validateExclusion,
format: validateFormat,
custom: validateCustom,
uniqueness: validateUniqueness
2012-03-27 14:22:24 +00:00
};
2011-10-11 19:52:03 +00:00
2011-11-18 18:59:46 +00:00
function getConfigurator(name, opts) {
2014-01-24 17:09:53 +00:00
return function () {
var args = Array.prototype.slice.call(arguments);
args[1] = args[1] || {};
configure(this, name, args, opts);
2014-01-24 17:09:53 +00:00
};
2011-10-10 13:22:51 +00:00
}
2012-03-27 14:22:24 +00:00
/**
* This method performs validation and triggers validation hooks.
* Before validation the `obj.errors` collection is cleaned.
2012-03-27 14:22:24 +00:00
* Each validation can add errors to `obj.errors` collection.
* If collection is not blank, validation failed.
*
* NOTE: This method can be called as synchronous only when no asynchronous validation is
* configured. It's strongly recommended to run all validations as asyncronous.
2012-03-27 14:22:24 +00:00
*
2014-03-12 23:28:46 +00:00
* Example: ExpressJS controller: render user if valid, show flash otherwise
2012-03-27 14:22:24 +00:00
* ```
* user.isValid(function (valid) {
* if (valid) res.render({user: user});
* else res.flash('error', 'User is not valid'), console.log(user.errors), res.redirect('/users');
* });
* ```
* Another example:
* ```
* user.isValid(function (valid) {
* if (!valid) {
* console.log(user.errors);
* // => hash of errors
* // => {
* // => username: [errmessage, errmessage, ...],
* // => email: ...
* // => }
* }
* });
* ```
2014-03-13 23:26:29 +00:00
* @param {Function} callback called with (valid)
* @returns {Boolean} True if no asynchronouse validation is configured and all properties pass validation.
2012-03-27 14:22:24 +00:00
*/
Validatable.prototype.isValid = function (callback, data) {
2014-01-24 17:09:53 +00:00
var valid = true, inst = this, wait = 0, async = false;
var validations = this.constructor.validations;
2014-01-24 17:09:53 +00:00
// exit with success when no errors
if (typeof validations !== 'object') {
2014-01-24 17:09:53 +00:00
cleanErrors(this);
if (callback) {
this.trigger('validate', function (validationsDone) {
validationsDone.call(inst, function () {
callback(valid);
});
}, data, callback);
2011-10-10 13:22:51 +00:00
}
2014-01-24 17:09:53 +00:00
return valid;
}
Object.defineProperty(this, 'errors', {
enumerable: false,
configurable: true,
value: new Errors
});
this.trigger('validate', function (validationsDone) {
var inst = this,
asyncFail = false;
var attrs = Object.keys(validations || {});
attrs.forEach(function(attr) {
var attrValidations = validations[attr] || [];
attrValidations.forEach(function(v) {
if (v.options && v.options.async) {
async = true;
wait += 1;
process.nextTick(function () {
validationFailed(inst, attr, v, done);
});
} else {
if (validationFailed(inst, attr, v)) {
valid = false;
}
2014-01-24 17:09:53 +00:00
}
});
2011-10-10 13:22:51 +00:00
});
2014-01-24 17:09:53 +00:00
if (!async) {
validationsDone.call(inst, function () {
if (valid) cleanErrors(inst);
if (callback) {
callback(valid);
2011-12-09 15:23:29 +00:00
}
2014-01-24 17:09:53 +00:00
});
}
2011-12-09 15:23:29 +00:00
2014-01-24 17:09:53 +00:00
function done(fail) {
asyncFail = asyncFail || fail;
if (--wait === 0) {
validationsDone.call(inst, function () {
if (valid && !asyncFail) cleanErrors(inst);
if (callback) {
callback(valid && !asyncFail);
}
});
}
}
2011-11-20 08:26:36 +00:00
}, data, callback);
2011-11-19 05:43:02 +00:00
2014-01-24 17:09:53 +00:00
if (async) {
// in case of async validation we should return undefined here,
// because not all validations are finished yet
return;
} else {
return valid;
}
2011-11-19 05:43:02 +00:00
2011-10-10 13:22:51 +00:00
};
2011-10-11 19:52:03 +00:00
function cleanErrors(inst) {
2014-01-24 17:09:53 +00:00
Object.defineProperty(inst, 'errors', {
enumerable: false,
configurable: true,
value: false
});
2011-10-11 19:52:03 +00:00
}
function validationFailed(inst, attr, conf, cb) {
var opts = conf.options || {};
2014-01-24 17:09:53 +00:00
if (typeof attr !== 'string') return false;
// here we should check skip validation conditions (if, unless)
// that can be specified in conf
if (skipValidation(inst, conf, 'if')
|| skipValidation(inst, conf, 'unless')) {
if (cb) cb(false);
return false;
}
2014-01-24 17:09:53 +00:00
var fail = false;
var validator = validators[conf.validation];
var validatorArguments = [];
validatorArguments.push(attr);
validatorArguments.push(conf);
validatorArguments.push(function onerror(kind) {
2014-07-11 21:03:07 +00:00
var message, code = conf.code || conf.validation;
2014-01-24 17:09:53 +00:00
if (conf.message) {
message = conf.message;
}
if (!message && defaultMessages[conf.validation]) {
message = defaultMessages[conf.validation];
2011-11-19 05:43:02 +00:00
}
2014-01-24 17:09:53 +00:00
if (!message) {
message = 'is invalid';
}
if (kind) {
code += '.' + kind;
if (message[kind]) {
// get deeper
message = message[kind];
} else if (defaultMessages.common[kind]) {
message = defaultMessages.common[kind];
} else {
message = 'is invalid';
}
}
if (kind !== false) inst.errors.add(attr, message, code);
2014-01-24 17:09:53 +00:00
fail = true;
});
if (cb) {
validatorArguments.push(function () {
cb(fail);
});
}
validator.apply(inst, validatorArguments);
return fail;
2011-10-10 13:22:51 +00:00
}
2011-10-11 19:52:03 +00:00
function skipValidation(inst, conf, kind) {
2014-01-24 17:09:53 +00:00
var doValidate = true;
if (typeof conf[kind] === 'function') {
doValidate = conf[kind].call(inst);
if (kind === 'unless') doValidate = !doValidate;
} else if (typeof conf[kind] === 'string') {
if (typeof inst[conf[kind]] === 'function') {
doValidate = inst[conf[kind]].call(inst);
if (kind === 'unless') doValidate = !doValidate;
} else if (inst.__data.hasOwnProperty(conf[kind])) {
doValidate = inst[conf[kind]];
if (kind === 'unless') doValidate = !doValidate;
} else {
doValidate = kind === 'if';
2011-10-11 19:52:03 +00:00
}
2014-01-24 17:09:53 +00:00
}
return !doValidate;
2011-10-11 19:52:03 +00:00
}
2011-10-10 13:22:51 +00:00
var defaultMessages = {
2014-01-24 17:09:53 +00:00
presence: 'can\'t be blank',
absence: 'can\'t be set',
2014-01-24 17:09:53 +00:00
length: {
min: 'too short',
max: 'too long',
is: 'length is wrong'
},
common: {
blank: 'is blank',
'null': 'is null'
},
numericality: {
'int': 'is not an integer',
'number': 'is not a number'
},
inclusion: 'is not included in the list',
exclusion: 'is reserved',
uniqueness: 'is not unique'
2011-10-10 13:22:51 +00:00
};
2011-10-10 17:34:50 +00:00
function nullCheck(attr, conf, err) {
if (this[attr] == null) {
2014-01-24 17:09:53 +00:00
if (!conf.allowNull) {
err('null');
}
return true;
} else {
if (blank(this[attr])) {
if (!conf.allowBlank) {
err('blank');
}
return true;
2011-10-10 17:34:50 +00:00
}
2014-01-24 17:09:53 +00:00
}
return false;
2011-10-10 17:34:50 +00:00
}
/*!
2012-03-27 14:22:24 +00:00
* Return true when v is undefined, blank array, null or empty string
* otherwise returns false
*
* @param {Mix} v
2014-03-13 23:26:29 +00:00
* Returns true if `v` is blank.
2012-03-27 14:22:24 +00:00
*/
2011-10-10 13:22:51 +00:00
function blank(v) {
2014-01-24 17:09:53 +00:00
if (typeof v === 'undefined') return true;
if (v instanceof Array && v.length === 0) return true;
if (v === null) return true;
if (typeof v == 'string' && v === '') return true;
return false;
2011-10-10 13:22:51 +00:00
}
2011-11-18 18:59:46 +00:00
function configure(cls, validation, args, opts) {
if (!cls.validations) {
Object.defineProperty(cls, 'validations', {
2014-01-24 17:09:53 +00:00
writable: true,
configurable: true,
enumerable: false,
value: {}
2011-10-10 13:22:51 +00:00
});
2014-01-24 17:09:53 +00:00
}
args = [].slice.call(args);
var conf;
if (typeof args[args.length - 1] === 'object') {
conf = args.pop();
} else {
conf = {};
}
if (validation === 'custom' && typeof args[args.length - 1] === 'function') {
conf.customValidator = args.pop();
}
conf.validation = validation;
args.forEach(function (attr) {
if (typeof attr === 'string') {
var validation = extend({}, conf);
validation.options = opts || {};
cls.validations[attr] = cls.validations[attr] || [];
cls.validations[attr].push(validation);
}
});
2011-10-10 13:22:51 +00:00
}
function Errors() {
2014-01-24 17:09:53 +00:00
Object.defineProperty(this, 'codes', {
enumerable: false,
configurable: true,
value: {}
});
2011-10-10 13:22:51 +00:00
}
2013-04-04 15:31:07 +00:00
Errors.prototype.add = function (field, message, code) {
2014-01-24 17:09:53 +00:00
code = code || 'invalid';
if (!this[field]) {
this[field] = [];
this.codes[field] = [];
}
this[field].push(message);
this.codes[field].push(code);
2013-04-04 15:31:07 +00:00
};
function ErrorCodes(messages) {
2014-01-24 17:09:53 +00:00
var c = this;
Object.keys(messages).forEach(function (field) {
c[field] = messages[field].codes;
});
2013-04-04 15:31:07 +00:00
}
/**
* ValidationError is raised when the application attempts to save an invalid model instance.
* Example:
* ```
* {
* "name": "ValidationError",
* "status": 422,
* "message": "The Model instance is not valid. \
* See `details` property of the error object for more info.",
* "statusCode": 422,
* "details": {
* "context": "user",
* "codes": {
* "password": [
* "presence"
* ],
* "email": [
* "uniqueness"
* ]
* },
* "messages": {
* "password": [
* "can't be blank"
* ],
* "email": [
* "Email already exists"
* ]
* }
* },
* }
* ```
* You might run into situations where you need to raise a validation error yourself, for example in a "before" hook or a
* custom model method.
* ```
* MyModel.prototype.preflight = function(changes, callback) {
* // Update properties, do not save to db
* for (var key in changes) {
* model[key] = changes[key];
* }
*
* if (model.isValid()) {
* return callback(null, { success: true });
* }
*
* // This line shows how to create a ValidationError
* err = new ValidationError(model);
* callback(err);
* }
* ```
*/
2013-04-04 15:31:07 +00:00
function ValidationError(obj) {
2014-01-24 17:09:53 +00:00
if (!(this instanceof ValidationError)) return new ValidationError(obj);
2013-04-04 15:31:07 +00:00
2014-01-24 17:09:53 +00:00
this.name = 'ValidationError';
var context = obj && obj.constructor && obj.constructor.modelName;
this.message = util.format(
'The %s instance is not valid. Details: %s.',
context ? '`' + context + '`' : 'model',
formatErrors(obj.errors, obj.toJSON()) || '(unknown)'
);
2014-01-24 17:09:53 +00:00
this.statusCode = 422;
2014-01-24 17:09:53 +00:00
this.details = {
context: context,
2014-01-24 17:09:53 +00:00
codes: obj.errors && obj.errors.codes,
messages: obj.errors
};
2013-04-04 15:31:07 +00:00
if (Error.captureStackTrace) {
// V8 (Chrome, Opera, Node)
Error.captureStackTrace(this, this.constructor);
} else if (errorHasStackProperty) {
// Firefox
this.stack = (new Error).stack;
}
// Safari and PhantomJS initializes `error.stack` on throw
// Internet Explorer does not support `error.stack`
}
2011-11-18 18:59:46 +00:00
util.inherits(ValidationError, Error);
var errorHasStackProperty = !!(new Error).stack;
ValidationError.maxPropertyStringLength = 32;
function formatErrors(errors, propertyValues) {
var DELIM = '; ';
errors = errors || {};
return Object.getOwnPropertyNames(errors)
.filter(function(propertyName) {
return Array.isArray(errors[propertyName]);
})
.map(function(propertyName) {
var messages = errors[propertyName];
var propertyValue = propertyValues[propertyName];
return messages.map(function(msg) {
return formatPropertyError(propertyName, propertyValue, msg);
}).join(DELIM);
})
.join(DELIM);
}
function formatPropertyError(propertyName, propertyValue, errorMessage) {
var formattedValue;
var valueType = typeof propertyValue;
if (valueType === 'string') {
formattedValue = JSON.stringify(truncatePropertyString(propertyValue));
} else if (propertyValue instanceof Date) {
formattedValue = propertyValue.toISOString();
} else if (valueType === 'object') {
// objects and arrays
formattedValue = util.inspect(propertyValue, {
showHidden: false,
color: false,
// show top-level object properties only
depth: Array.isArray(propertyValue) ? 1 : 0
});
formattedValue = truncatePropertyString(formattedValue);
} else {
formattedValue = truncatePropertyString('' + propertyValue);
}
return '`' + propertyName + '` ' + errorMessage +
' (value: ' + formattedValue + ')';
}
function truncatePropertyString(value) {
var len = ValidationError.maxPropertyStringLength;
if (value.length <= len) return value;
// preserve few last characters like `}` or `]`, but no more than 3
// this way the last `} ]` in the array of objects is included in the message
var tail;
var m = value.match(/([ \t})\]]+)$/);
if (m) {
tail = m[1].slice(-3);
len -= tail.length;
} else {
tail = value.slice(-3);
len -= 3;
}
return value.slice(0, len-4) + '...' + tail;
}