2016-04-01 22:25:16 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-07-22 19:26:07 +00:00
|
|
|
var g = require('strong-globalize')();
|
2013-09-09 17:12:12 +00:00
|
|
|
var util = require('util');
|
2014-08-19 13:41:55 +00:00
|
|
|
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
|
|
|
* This class provides methods that add validation cababilities to models.
|
2015-03-05 01:21:25 +00:00
|
|
|
* Each of the validations runs when the `obj.isValid()` method is called.
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
2015-03-05 01:21:25 +00:00
|
|
|
* All of the methods have an options object parameter that has a
|
|
|
|
* `message` property. When there is only a single error message, this property is just a string;
|
|
|
|
* for example: `Post.validatesPresenceOf('title', { message: 'can not be blank' });`
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
2015-03-05 01:21:25 +00:00
|
|
|
* In more complicated cases it can be a set of messages, for each possible error condition; for example:
|
2012-03-27 14:22:24 +00:00
|
|
|
* `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
|
|
|
/**
|
2015-01-12 13:52:54 +00:00
|
|
|
* Validate presence of one or more specified properties.
|
2017-06-01 15:00:44 +00:00
|
|
|
*
|
2014-05-15 01:30:42 +00:00
|
|
|
* 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');
|
|
|
|
* ```
|
2014-05-15 01:30:42 +00:00
|
|
|
* 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
|
|
|
* ```
|
|
|
|
*
|
2014-05-15 01:30:42 +00:00
|
|
|
* @param {String} propertyName One or more property names.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @options {Object} options Configuration parameters; see below.
|
2014-05-15 01:30:42 +00:00
|
|
|
* @property {String} message Error message to use instead of default.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @property {String} if Validate only if `if` exists.
|
|
|
|
* @property {String} unless Validate only if `unless` exists.
|
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
|
|
|
|
2014-07-11 20:56:02 +00:00
|
|
|
/**
|
2015-01-12 13:52:54 +00:00
|
|
|
* Validate absence of one or more specified properties.
|
2017-06-01 15:00:44 +00:00
|
|
|
*
|
|
|
|
* A model should not include a property to be considered valid; fails when validated field is not blank.
|
2014-07-11 20:56:02 +00:00
|
|
|
*
|
|
|
|
* For example, validate absence of reserved
|
|
|
|
* ```
|
|
|
|
* Post.validatesAbsenceOf('reserved', { unless: 'special' });
|
2015-09-04 23:31:54 +00:00
|
|
|
* ```
|
2017-06-01 15:00:44 +00:00
|
|
|
*
|
2014-07-11 20:56:02 +00:00
|
|
|
* @param {String} propertyName One or more property names.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @options {Object} options Configuration parameters; see below.
|
2014-07-11 20:56:02 +00:00
|
|
|
* @property {String} message Error message to use instead of default.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @property {String} if Validate only if `if` exists.
|
|
|
|
* @property {String} unless Validate only if `unless` exists.
|
2014-07-11 20:56:02 +00:00
|
|
|
*/
|
|
|
|
Validatable.validatesAbsenceOf = getConfigurator('absence');
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
2017-06-01 15:00:44 +00:00
|
|
|
* Validate length.
|
|
|
|
*
|
|
|
|
* Require a property length to be within a specified range.
|
|
|
|
*
|
|
|
|
* There are three kinds of validations: min, max, is.
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
|
|
|
* Default error messages:
|
|
|
|
*
|
|
|
|
* - min: too short
|
|
|
|
* - max: too long
|
2017-06-01 15:00:44 +00:00
|
|
|
* - is: length is wrong
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
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'}});
|
|
|
|
* ```
|
2017-06-01 15:00:44 +00:00
|
|
|
*
|
2014-05-15 01:30:42 +00:00
|
|
|
* @param {String} propertyName Property name to validate.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @options {Object} options Configuration parameters; see below.
|
2014-05-15 01:30:42 +00:00
|
|
|
* @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.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
2017-06-01 15:00:44 +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
|
|
|
* ```
|
2017-06-01 15:00:44 +00:00
|
|
|
* User.validatesNumericalityOf('age', { message: { number: 'is not a number' }});
|
|
|
|
* User.validatesNumericalityOf('age', {int: true, message: { int: 'is not an integer' }});
|
2012-03-27 19:48:23 +00:00
|
|
|
* ```
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
2014-05-15 01:30:42 +00:00
|
|
|
* @param {String} propertyName Property name to validate.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @options {Object} options Configuration parameters; see below.
|
2014-05-15 01:30:42 +00:00
|
|
|
* @property {Boolean} int If true, then property must be an integer to be valid.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @property {Boolean} allowBlank Allow property to be blank.
|
|
|
|
* @property {Boolean} allowNull Allow property to be null.
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
2017-06-01 15:00:44 +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
|
|
|
*
|
2014-05-15 01:30:42 +00:00
|
|
|
* @param {String} propertyName Property name to validate.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @options {Object} options Configuration parameters; see below.
|
|
|
|
* @property {Array} in Property must match one of the values in the array to be valid.
|
2016-09-30 02:26:50 +00:00
|
|
|
* @property {String} message Optional error message if property is not valid.
|
2016-09-28 23:43:17 +00:00
|
|
|
* Default error message: "is not included in the list".
|
|
|
|
* @property {Boolean} allowNull Whether null values are allowed.
|
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
|
|
|
|
|
|
|
/**
|
2017-06-01 15:00:44 +00:00
|
|
|
* Validate exclusion in a set.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*
|
2014-05-15 01:30:42 +00:00
|
|
|
* @param {String} propertyName Property name to validate.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @options {Object} options Configuration parameters; see below.
|
2017-03-08 15:24:18 +00:00
|
|
|
* @property {Array} in Property must not match any of the values in the array to be valid.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @property {String} message Optional error message if property is not valid. Default error message: "is reserved".
|
2016-09-30 02:26:50 +00:00
|
|
|
* @property {Boolean} allowNull Whether null values are allowed.
|
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
|
|
|
|
|
|
|
/**
|
2017-06-01 15:00:44 +00:00
|
|
|
* Validate format.
|
2014-05-15 01:30:42 +00:00
|
|
|
*
|
2017-06-01 15:00:44 +00:00
|
|
|
* Require a model to include a property that matches the given format.
|
|
|
|
*
|
|
|
|
* Example: `User.validatesFormatOf('name', {with: /\w+/});`
|
2015-01-12 13:52:54 +00:00
|
|
|
*
|
2014-05-15 01:30:42 +00:00
|
|
|
* @param {String} propertyName Property name to validate.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @options {Object} options Configuration parameters; see below.
|
2014-05-15 01:30:42 +00:00
|
|
|
* @property {RegExp} with Regular expression to validate format.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
|
2016-09-30 02:26:50 +00:00
|
|
|
* @property {Boolean} allowNull Whether null values are allowed.
|
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
|
|
|
|
|
|
|
/**
|
2015-03-05 01:21:25 +00:00
|
|
|
* Validate using custom validation function.
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
2013-02-12 09:05:04 +00:00
|
|
|
* Example:
|
2017-06-01 15:00:44 +00:00
|
|
|
*```javascript
|
2013-02-12 09:05:04 +00:00
|
|
|
* 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
|
2017-06-01 15:00:44 +00:00
|
|
|
* ```
|
2013-02-12 09:05:04 +00:00
|
|
|
*
|
2015-03-04 00:58:19 +00:00
|
|
|
* @param {String} propertyName Property name to validate.
|
|
|
|
* @param {Function} validatorFn Custom validation function.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @options {Object} options Configuration parameters; see below.
|
|
|
|
* @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
|
2016-09-28 23:43:17 +00:00
|
|
|
* @property {Boolean} allowNull Whether null values are allowed.
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2011-11-18 18:59:46 +00:00
|
|
|
Validatable.validate = getConfigurator('custom');
|
2012-03-27 14:22:24 +00:00
|
|
|
|
|
|
|
/**
|
2015-03-05 01:21:25 +00:00
|
|
|
* Validate using custom asynchronous validation function.
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
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
|
|
|
|
* })
|
2017-06-01 15:00:44 +00:00
|
|
|
* ```
|
|
|
|
*
|
2015-03-04 00:58:19 +00:00
|
|
|
* @param {String} propertyName Property name to validate.
|
2015-03-05 01:21:25 +00:00
|
|
|
* @param {Function} validatorFn Custom validation function.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @options {Object} options Configuration parameters; see below.
|
|
|
|
* @property {String} message Optional error message if property is not valid. Default error message: " is invalid".
|
2016-09-30 02:26:50 +00:00
|
|
|
* @property {Boolean} allowNull Whether null values are allowed.
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2016-08-19 17:46:59 +00:00
|
|
|
Validatable.validateAsync = getConfigurator('custom', {async: true});
|
2012-03-27 14:22:24 +00:00
|
|
|
|
|
|
|
/**
|
2017-06-01 15:00:44 +00:00
|
|
|
* Validate uniqueness of the value for a property in the collection of models.
|
|
|
|
*
|
2014-05-15 01:30:42 +00:00
|
|
|
* Not available for all connectors. Currently supported with these connectors:
|
|
|
|
* - In Memory
|
|
|
|
* - Oracle
|
|
|
|
* - MongoDB
|
|
|
|
*
|
2014-05-14 18:38:40 +00:00
|
|
|
* ```
|
|
|
|
* // 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'] });
|
|
|
|
* ```
|
2017-06-01 15:00:44 +00:00
|
|
|
*
|
2014-05-15 01:30:42 +00:00
|
|
|
* @param {String} propertyName Property name to validate.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @options {Object} options Configuration parameters; see below.
|
2014-05-15 01:30:42 +00:00
|
|
|
* @property {RegExp} with Regular expression to validate format.
|
2014-05-14 18:38:40 +00:00
|
|
|
* @property {Array.<String>} scopedTo List of properties defining the scope.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @property {String} message Optional error message if property is not valid. Default error message: "is not unique".
|
2016-09-30 02:26:50 +00:00
|
|
|
* @property {Boolean} allowNull Whether null values are allowed.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @property {String} ignoreCase Make the validation case insensitive.
|
|
|
|
* @property {String} if Validate only if `if` exists.
|
|
|
|
* @property {String} unless Validate only if `unless` exists.
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2016-08-19 17:46:59 +00:00
|
|
|
Validatable.validatesUniquenessOf = getConfigurator('uniqueness', {async: true});
|
2011-10-10 13:22:51 +00:00
|
|
|
|
2017-06-01 15:00:44 +00:00
|
|
|
/**
|
|
|
|
* Validate if a value for a property is a Date.
|
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* ```
|
|
|
|
* User.validatesDateOf('today', {message: 'today is not a date!'});
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param {String} propertyName Property name to validate.
|
|
|
|
* @options {Object} options Configuration parameters; see below.
|
|
|
|
* @property {String} message Error message to use instead of default.
|
|
|
|
*/
|
2017-04-21 18:12:04 +00:00
|
|
|
Validatable.validatesDateOf = getConfigurator('date');
|
2017-06-01 15:00:44 +00:00
|
|
|
|
2011-10-11 19:52:03 +00:00
|
|
|
// implementation of validators
|
|
|
|
|
2014-05-15 01:30:42 +00:00
|
|
|
/*!
|
2012-03-27 14:22:24 +00:00
|
|
|
* Presence validator
|
|
|
|
*/
|
2016-06-24 15:07:25 +00:00
|
|
|
function validatePresence(attr, conf, err, options) {
|
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
|
|
|
|
2014-07-11 20:56:02 +00:00
|
|
|
/*!
|
|
|
|
* Absence validator
|
|
|
|
*/
|
2016-06-24 15:07:25 +00:00
|
|
|
function validateAbsence(attr, conf, err, options) {
|
2014-07-11 20:56:02 +00:00
|
|
|
if (!blank(this[attr])) {
|
|
|
|
err();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:30:42 +00:00
|
|
|
/*!
|
2012-03-27 14:22:24 +00:00
|
|
|
* Length validator
|
|
|
|
*/
|
2016-06-24 15:07:25 +00:00
|
|
|
function validateLength(attr, conf, err, options) {
|
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
|
|
|
|
2014-05-15 01:30:42 +00:00
|
|
|
/*!
|
2012-03-27 14:22:24 +00:00
|
|
|
* Numericality validator
|
|
|
|
*/
|
2016-06-24 15:07:25 +00:00
|
|
|
function validateNumericality(attr, conf, err, options) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (nullCheck.call(this, attr, conf, err)) return;
|
|
|
|
|
2016-09-23 09:26:29 +00:00
|
|
|
if (typeof this[attr] !== 'number' || isNaN(this[attr])) {
|
2014-01-24 17:09:53 +00:00
|
|
|
return err('number');
|
|
|
|
}
|
|
|
|
if (conf.int && this[attr] !== Math.round(this[attr])) {
|
|
|
|
return err('int');
|
|
|
|
}
|
2012-03-27 14:22:24 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:30:42 +00:00
|
|
|
/*!
|
2012-03-27 14:22:24 +00:00
|
|
|
* Inclusion validator
|
|
|
|
*/
|
2016-06-24 15:07:25 +00:00
|
|
|
function validateInclusion(attr, conf, err, options) {
|
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])) {
|
2016-04-01 11:48:17 +00:00
|
|
|
err();
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2012-03-27 14:22:24 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:30:42 +00:00
|
|
|
/*!
|
2012-03-27 14:22:24 +00:00
|
|
|
* Exclusion validator
|
|
|
|
*/
|
2016-06-24 15:07:25 +00:00
|
|
|
function validateExclusion(attr, conf, err, options) {
|
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])) {
|
2016-04-01 11:48:17 +00:00
|
|
|
err();
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2012-03-27 14:22:24 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:30:42 +00:00
|
|
|
/*!
|
2012-03-27 14:22:24 +00:00
|
|
|
* Format validator
|
|
|
|
*/
|
2016-06-24 15:07:25 +00:00
|
|
|
function validateFormat(attr, conf, err, options) {
|
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
|
|
|
}
|
|
|
|
|
2014-05-15 01:30:42 +00:00
|
|
|
/*!
|
2012-03-27 14:22:24 +00:00
|
|
|
* Custom validator
|
|
|
|
*/
|
2016-06-24 15:07:25 +00:00
|
|
|
function validateCustom(attr, conf, err, options, done) {
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
done = options;
|
|
|
|
options = {};
|
|
|
|
}
|
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
|
|
|
|
2015-12-21 12:38:11 +00:00
|
|
|
function escapeStringRegexp(str) {
|
|
|
|
if (typeof str !== 'string') {
|
|
|
|
throw new TypeError('Expected a string');
|
|
|
|
}
|
|
|
|
var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
|
|
|
|
return str.replace(matchOperatorsRe, '\\$&');
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:30:42 +00:00
|
|
|
/*!
|
2012-03-27 14:22:24 +00:00
|
|
|
* Uniqueness validator
|
|
|
|
*/
|
2016-06-24 15:07:25 +00:00
|
|
|
function validateUniqueness(attr, conf, err, options, done) {
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
done = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2014-07-11 21:55:15 +00:00
|
|
|
if (blank(this[attr])) {
|
|
|
|
return process.nextTick(done);
|
|
|
|
}
|
2016-08-19 17:46:59 +00:00
|
|
|
var cond = {where: {}};
|
2015-12-21 12:38:11 +00:00
|
|
|
|
|
|
|
if (conf && conf.ignoreCase) {
|
|
|
|
cond.where[attr] = new RegExp('^' + escapeStringRegexp(this[attr]) + '$', 'i');
|
|
|
|
} else {
|
|
|
|
cond.where[attr] = this[attr];
|
|
|
|
}
|
2014-05-14 18:38:40 +00:00
|
|
|
|
|
|
|
if (conf && conf.scopedTo) {
|
|
|
|
conf.scopedTo.forEach(function(k) {
|
|
|
|
var val = this[k];
|
|
|
|
if (val !== undefined)
|
|
|
|
cond.where[k] = this[k];
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
|
2015-07-13 10:56:25 +00:00
|
|
|
var idName = this.constructor.definition.idName();
|
|
|
|
var isNewRecord = this.isNewRecord();
|
2016-06-24 15:07:25 +00:00
|
|
|
this.constructor.find(cond, options, function(error, found) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (error) {
|
2015-05-28 16:54:01 +00:00
|
|
|
err(error);
|
|
|
|
} else if (found.length > 1) {
|
2014-01-24 17:09:53 +00:00
|
|
|
err();
|
2015-07-13 10:56:25 +00:00
|
|
|
} else if (found.length === 1 && idName === attr && isNewRecord) {
|
|
|
|
err();
|
2016-04-01 13:23:42 +00:00
|
|
|
} else if (found.length === 1 && (
|
|
|
|
!this.id || !found[0].id || found[0].id.toString() != this.id.toString()
|
|
|
|
)) {
|
2014-01-24 17:09:53 +00:00
|
|
|
err();
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
}.bind(this));
|
2012-03-27 14:22:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 18:12:04 +00:00
|
|
|
/*!
|
|
|
|
* Date validator
|
|
|
|
*/
|
|
|
|
function validateDate(attr, conf, err) {
|
|
|
|
if (this[attr] === null || this[attr] === undefined) return;
|
|
|
|
|
|
|
|
var date = new Date(this[attr]);
|
|
|
|
if (isNaN(date.getTime())) return err();
|
|
|
|
}
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
var validators = {
|
2014-01-24 17:09:53 +00:00
|
|
|
presence: validatePresence,
|
2014-07-11 20:56:02 +00:00
|
|
|
absence: validateAbsence,
|
2014-01-24 17:09:53 +00:00
|
|
|
length: validateLength,
|
|
|
|
numericality: validateNumericality,
|
|
|
|
inclusion: validateInclusion,
|
|
|
|
exclusion: validateExclusion,
|
|
|
|
format: validateFormat,
|
|
|
|
custom: validateCustom,
|
2016-04-01 11:48:17 +00:00
|
|
|
uniqueness: validateUniqueness,
|
2017-04-21 18:12:04 +00:00
|
|
|
date: validateDate,
|
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) {
|
2016-04-01 11:48:17 +00:00
|
|
|
return function() {
|
2014-08-26 15:15:07 +00:00
|
|
|
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
|
|
|
/**
|
2014-05-15 01:30:42 +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.
|
|
|
|
*
|
2014-05-15 01:30:42 +00:00
|
|
|
* NOTE: This method can be called as synchronous only when no asynchronous validation is
|
2012-04-09 16:24:35 +00:00
|
|
|
* configured. It's strongly recommended to run all validations as asyncronous.
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
2017-06-01 15:00:44 +00:00
|
|
|
* Example: ExpressJS controller - render user if valid, show flash otherwise
|
|
|
|
* ```javascript
|
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');
|
|
|
|
* });
|
|
|
|
* ```
|
2014-05-15 01:30:42 +00:00
|
|
|
* Another example:
|
2017-06-01 15:00:44 +00:00
|
|
|
* ```javascript
|
2014-05-15 01:30:42 +00:00
|
|
|
* user.isValid(function (valid) {
|
|
|
|
* if (!valid) {
|
|
|
|
* console.log(user.errors);
|
|
|
|
* // => hash of errors
|
|
|
|
* // => {
|
|
|
|
* // => username: [errmessage, errmessage, ...],
|
|
|
|
* // => email: ...
|
|
|
|
* // => }
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* ```
|
2017-06-01 15:00:44 +00:00
|
|
|
* @callback {Function} callback Called with (valid).
|
|
|
|
* @param {Object} data Data to be validated.
|
|
|
|
* @param {Object} options Options to be specified upon validation.
|
2015-03-05 01:21:25 +00:00
|
|
|
* @returns {Boolean} True if no asynchronous validation is configured and all properties pass validation.
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2016-06-24 15:07:25 +00:00
|
|
|
Validatable.prototype.isValid = function(callback, data, options) {
|
|
|
|
options = options || {};
|
2014-01-24 17:09:53 +00:00
|
|
|
var valid = true, inst = this, wait = 0, async = false;
|
2014-08-26 15:51:01 +00:00
|
|
|
var validations = this.constructor.validations;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2016-09-07 18:24:48 +00:00
|
|
|
var reportDiscardedProperties = this.__strict &&
|
2015-04-09 14:57:38 +00:00
|
|
|
this.__unknownProperties && this.__unknownProperties.length;
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// exit with success when no errors
|
2015-04-09 14:57:38 +00:00
|
|
|
if (typeof validations !== 'object' && !reportDiscardedProperties) {
|
2014-01-24 17:09:53 +00:00
|
|
|
cleanErrors(this);
|
|
|
|
if (callback) {
|
2016-04-01 11:48:17 +00:00
|
|
|
this.trigger('validate', function(validationsDone) {
|
|
|
|
validationsDone.call(inst, function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
callback(valid);
|
|
|
|
});
|
2014-07-08 17:54:13 +00:00
|
|
|
}, 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,
|
2016-04-01 11:48:17 +00:00
|
|
|
value: new Errors,
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
this.trigger('validate', function(validationsDone) {
|
2014-01-24 17:09:53 +00:00
|
|
|
var inst = this,
|
|
|
|
asyncFail = false;
|
|
|
|
|
2014-08-26 15:51:01 +00:00
|
|
|
var attrs = Object.keys(validations || {});
|
2015-01-12 13:52:54 +00:00
|
|
|
|
2014-08-26 15:51:01 +00:00
|
|
|
attrs.forEach(function(attr) {
|
|
|
|
var attrValidations = validations[attr] || [];
|
|
|
|
attrValidations.forEach(function(v) {
|
|
|
|
if (v.options && v.options.async) {
|
|
|
|
async = true;
|
|
|
|
wait += 1;
|
2016-04-01 11:48:17 +00:00
|
|
|
process.nextTick(function() {
|
2016-06-24 15:07:25 +00:00
|
|
|
validationFailed(inst, attr, v, options, done);
|
2014-08-26 15:51:01 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (validationFailed(inst, attr, v)) {
|
|
|
|
valid = false;
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2014-08-26 15:51:01 +00:00
|
|
|
});
|
2011-10-10 13:22:51 +00:00
|
|
|
});
|
2015-01-12 13:52:54 +00:00
|
|
|
|
2015-04-09 14:57:38 +00:00
|
|
|
if (reportDiscardedProperties) {
|
|
|
|
for (var ix in inst.__unknownProperties) {
|
|
|
|
var key = inst.__unknownProperties[ix];
|
|
|
|
var code = 'unknown-property';
|
|
|
|
var msg = defaultMessages[code];
|
|
|
|
inst.errors.add(key, msg, code);
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!async) {
|
2016-04-01 11:48:17 +00:00
|
|
|
validationsDone.call(inst, function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
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) {
|
2016-04-01 11:48:17 +00:00
|
|
|
validationsDone.call(inst, function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (valid && !asyncFail) cleanErrors(inst);
|
|
|
|
if (callback) {
|
|
|
|
callback(valid && !asyncFail);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-07-08 17:54:13 +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-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,
|
2016-04-01 11:48:17 +00:00
|
|
|
value: false,
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2011-10-11 19:52:03 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 15:07:25 +00:00
|
|
|
function validationFailed(inst, attr, conf, options, cb) {
|
2014-08-26 15:51:01 +00:00
|
|
|
var opts = conf.options || {};
|
2015-01-12 13:52:54 +00:00
|
|
|
|
2016-06-24 15:07:25 +00:00
|
|
|
if (typeof options === 'function') {
|
|
|
|
cb = options;
|
|
|
|
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
|
2016-04-01 13:23:42 +00:00
|
|
|
if (skipValidation(inst, conf, 'if') ||
|
|
|
|
skipValidation(inst, conf, 'unless')) {
|
2014-11-19 13:50:08 +00:00
|
|
|
if (cb) cb(false);
|
2014-07-11 20:56:02 +00:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|
2014-07-11 20:56:02 +00:00
|
|
|
if (kind !== false) inst.errors.add(attr, message, code);
|
2014-01-24 17:09:53 +00:00
|
|
|
fail = true;
|
|
|
|
});
|
2016-06-24 15:07:25 +00:00
|
|
|
validatorArguments.push(options);
|
2014-01-24 17:09:53 +00:00
|
|
|
if (cb) {
|
2016-04-01 11:48:17 +00:00
|
|
|
validatorArguments.push(function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
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',
|
2014-07-11 20:56:02 +00:00
|
|
|
absence: 'can\'t be set',
|
2015-04-09 14:57:38 +00:00
|
|
|
'unknown-property': 'is not defined in the model',
|
2014-01-24 17:09:53 +00:00
|
|
|
length: {
|
|
|
|
min: 'too short',
|
|
|
|
max: 'too long',
|
2016-04-01 11:48:17 +00:00
|
|
|
is: 'length is wrong',
|
2014-01-24 17:09:53 +00:00
|
|
|
},
|
|
|
|
common: {
|
|
|
|
blank: 'is blank',
|
2016-04-01 11:48:17 +00:00
|
|
|
'null': 'is null',
|
2014-01-24 17:09:53 +00:00
|
|
|
},
|
|
|
|
numericality: {
|
|
|
|
'int': 'is not an integer',
|
2016-04-01 11:48:17 +00:00
|
|
|
'number': 'is not a number',
|
2014-01-24 17:09:53 +00:00
|
|
|
},
|
|
|
|
inclusion: 'is not included in the list',
|
|
|
|
exclusion: 'is reserved',
|
2016-04-01 11:48:17 +00:00
|
|
|
uniqueness: 'is not unique',
|
2017-04-21 18:12:04 +00:00
|
|
|
date: 'is not a valid date',
|
2011-10-10 13:22:51 +00:00
|
|
|
};
|
|
|
|
|
2016-09-23 09:26:29 +00:00
|
|
|
/**
|
|
|
|
* Checks if attribute is undefined or null. Calls err function with 'blank' or 'null'.
|
|
|
|
* See defaultMessages. You can affect this behaviour with conf.allowBlank and conf.allowNull.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @private
|
2016-09-23 09:26:29 +00:00
|
|
|
* @param {String} attr Property name of attribute
|
|
|
|
* @param {Object} conf conf object for validator
|
|
|
|
* @param {Function} err
|
|
|
|
* @return {Boolean} returns true if attribute is null or blank
|
|
|
|
*/
|
2011-10-10 17:34:50 +00:00
|
|
|
function nullCheck(attr, conf, err) {
|
2016-09-23 09:26:29 +00:00
|
|
|
// First determine if attribute is defined
|
2017-07-19 23:26:13 +00:00
|
|
|
if (typeof this[attr] === 'undefined' || this[attr] === '') {
|
2016-09-23 09:26:29 +00:00
|
|
|
if (!conf.allowBlank) {
|
|
|
|
err('blank');
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
2016-09-23 09:26:29 +00:00
|
|
|
// Now check if attribute is null
|
|
|
|
if (this[attr] === null) {
|
|
|
|
if (!conf.allowNull) {
|
|
|
|
err('null');
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-15 01:30:42 +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;
|
2015-04-22 17:57:48 +00:00
|
|
|
if (typeof v === 'number' && isNaN(v)) return true;
|
2014-01-24 17:09:53 +00:00
|
|
|
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) {
|
2014-08-26 15:51:01 +00:00
|
|
|
if (!cls.validations) {
|
|
|
|
Object.defineProperty(cls, 'validations', {
|
2014-01-24 17:09:53 +00:00
|
|
|
writable: true,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
2016-04-01 11:48:17 +00:00
|
|
|
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;
|
2016-04-01 11:48:17 +00:00
|
|
|
args.forEach(function(attr) {
|
2014-09-05 16:00:11 +00:00
|
|
|
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,
|
2016-04-01 11:48:17 +00:00
|
|
|
value: {},
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2011-10-10 13:22:51 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 11:48:17 +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;
|
2016-04-01 11:48:17 +00:00
|
|
|
Object.keys(messages).forEach(function(field) {
|
2014-01-24 17:09:53 +00:00
|
|
|
c[field] = messages[field].codes;
|
|
|
|
});
|
2013-04-04 15:31:07 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:30:42 +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];
|
|
|
|
* }
|
2015-01-12 13:52:54 +00:00
|
|
|
*
|
2014-05-15 01:30:42 +00:00
|
|
|
* if (model.isValid()) {
|
|
|
|
* return callback(null, { success: true });
|
|
|
|
* }
|
2015-01-12 13:52:54 +00:00
|
|
|
*
|
2014-05-15 01:30:42 +00:00
|
|
|
* // This line shows how to create a ValidationError
|
2016-11-29 15:04:53 +00:00
|
|
|
* var err = new MyModel.ValidationError(model);
|
2014-05-15 01:30:42 +00:00
|
|
|
* callback(err);
|
|
|
|
* }
|
|
|
|
* ```
|
2017-06-01 15:00:44 +00:00
|
|
|
*
|
|
|
|
* @private
|
2014-05-15 01:30:42 +00:00
|
|
|
*/
|
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';
|
2014-05-20 08:47:44 +00:00
|
|
|
|
|
|
|
var context = obj && obj.constructor && obj.constructor.modelName;
|
2016-07-22 19:26:07 +00:00
|
|
|
this.message = g.f(
|
2014-05-20 08:47:44 +00:00
|
|
|
'The %s instance is not valid. Details: %s.',
|
|
|
|
context ? '`' + context + '`' : 'model',
|
2015-01-12 13:52:54 +00:00
|
|
|
formatErrors(obj.errors, obj.toJSON()) || '(unknown)'
|
2014-05-20 08:47:44 +00:00
|
|
|
);
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
this.statusCode = 422;
|
2013-11-25 16:20:05 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
this.details = {
|
2014-05-20 08:47:44 +00:00
|
|
|
context: context,
|
2014-01-24 17:09:53 +00:00
|
|
|
codes: obj.errors && obj.errors.codes,
|
2016-04-01 11:48:17 +00:00
|
|
|
messages: obj.errors,
|
2014-01-24 17:09:53 +00:00
|
|
|
};
|
2013-04-04 15:31:07 +00:00
|
|
|
|
2014-06-23 12:39:37 +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`
|
2013-09-09 17:12:12 +00:00
|
|
|
}
|
2011-11-18 18:59:46 +00:00
|
|
|
|
2013-09-09 17:12:12 +00:00
|
|
|
util.inherits(ValidationError, Error);
|
2014-05-20 08:47:44 +00:00
|
|
|
|
2014-06-23 12:39:37 +00:00
|
|
|
var errorHasStackProperty = !!(new Error).stack;
|
|
|
|
|
2015-01-12 13:52:54 +00:00
|
|
|
ValidationError.maxPropertyStringLength = 32;
|
|
|
|
|
|
|
|
function formatErrors(errors, propertyValues) {
|
2014-05-20 08:47:44 +00:00
|
|
|
var DELIM = '; ';
|
|
|
|
errors = errors || {};
|
|
|
|
return Object.getOwnPropertyNames(errors)
|
|
|
|
.filter(function(propertyName) {
|
|
|
|
return Array.isArray(errors[propertyName]);
|
|
|
|
})
|
|
|
|
.map(function(propertyName) {
|
|
|
|
var messages = errors[propertyName];
|
2015-01-12 13:52:54 +00:00
|
|
|
var propertyValue = propertyValues[propertyName];
|
2014-05-20 08:47:44 +00:00
|
|
|
return messages.map(function(msg) {
|
2015-01-12 13:52:54 +00:00
|
|
|
return formatPropertyError(propertyName, propertyValue, msg);
|
2014-05-20 08:47:44 +00:00
|
|
|
}).join(DELIM);
|
|
|
|
})
|
|
|
|
.join(DELIM);
|
|
|
|
}
|
2015-01-12 13:52:54 +00:00
|
|
|
|
|
|
|
function formatPropertyError(propertyName, propertyValue, errorMessage) {
|
|
|
|
var formattedValue;
|
|
|
|
var valueType = typeof propertyValue;
|
|
|
|
if (valueType === 'string') {
|
|
|
|
formattedValue = JSON.stringify(truncatePropertyString(propertyValue));
|
|
|
|
} else if (propertyValue instanceof Date) {
|
2017-04-21 18:12:04 +00:00
|
|
|
formattedValue = isNaN(propertyValue.getTime()) ? propertyValue.toString() : propertyValue.toISOString();
|
2015-01-12 13:52:54 +00:00
|
|
|
} else if (valueType === 'object') {
|
|
|
|
// objects and arrays
|
|
|
|
formattedValue = util.inspect(propertyValue, {
|
|
|
|
showHidden: false,
|
|
|
|
color: false,
|
|
|
|
// show top-level object properties only
|
2016-04-01 11:48:17 +00:00
|
|
|
depth: Array.isArray(propertyValue) ? 1 : 0,
|
2015-01-12 13:52:54 +00:00
|
|
|
});
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
return value.slice(0, len - 4) + '...' + tail;
|
2015-01-12 13:52:54 +00:00
|
|
|
}
|