不要怂,就是干,撸起袖子干!

Commit b0037d67 by Gustav Pursche Committed by Jan Aagaard Meier

Move validator.extend to it's own file

1 parent 1b41977d
'use strict'; 'use strict';
var Validator = require('validator') var validator = require('./utils/validator-extras').validator
, extendModelValidations = require('./utils/validator-extras').extendModelValidations
, Utils = require('./utils') , Utils = require('./utils')
, sequelizeError = require('./errors') , sequelizeError = require('./errors')
, Promise = require('./promise') , Promise = require('./promise')
, DataTypes = require('./data-types') , DataTypes = require('./data-types')
, _ = require('lodash'); , _ = require('lodash');
function noop() {}
// Deprecate this.
Validator.notNull = function() {
throw new Error('Warning "notNull" validation has been deprecated in favor of Schema based "allowNull"');
};
// https://github.com/chriso/validator.js/blob/1.5.0/lib/validators.js
Validator.extend('notEmpty', function(str) {
return !str.match(/^[\s\t\r\n]*$/);
});
Validator.extend('len', function(str, min, max) {
return this.isLength(str, min, max);
});
Validator.extend('isUrl', function(str) {
return this.isURL(str);
});
Validator.extend('isIPv6', function(str) {
return this.isIP(str, 6);
});
Validator.extend('isIPv4', function(str) {
return this.isIP(str, 4);
});
Validator.extend('notIn', function(str, values) {
return !this.isIn(str, values);
});
Validator.extend('regex', function(str, pattern, modifiers) {
str += '';
if (Object.prototype.toString.call(pattern).slice(8, -1) !== 'RegExp') {
pattern = new RegExp(pattern, modifiers);
}
return str.match(pattern);
});
Validator.extend('notRegex', function(str, pattern, modifiers) {
return !this.regex(str, pattern, modifiers);
});
Validator.extend('isDecimal', function(str) {
return str !== '' && str.match(/^(?:-?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/);
});
Validator.extend('min', function(str, val) {
var number = parseFloat(str);
return isNaN(number) || number >= val;
});
Validator.extend('max', function(str, val) {
var number = parseFloat(str);
return isNaN(number) || number <= val;
});
Validator.extend('not', function(str, pattern, modifiers) {
return this.notRegex(str, pattern, modifiers);
});
Validator.extend('contains', function(str, elem) {
return str.indexOf(elem) >= 0 && !!elem;
});
Validator.extend('notContains', function(str, elem) {
return !this.contains(str, elem);
});
Validator.extend('is', function(str, pattern, modifiers) {
return this.regex(str, pattern, modifiers);
});
function extendModelValidations(modelInstance) {
Validator.extend('isImmutable', function(str, param, field) {
return (modelInstance.isNewRecord || modelInstance.dataValues[field] === modelInstance._previousDataValues[field]);
});
}
/** /**
* The Main Instance Validator. * The Main Instance Validator.
* *
...@@ -111,10 +30,10 @@ var InstanceValidator = module.exports = function(modelInstance, options) { ...@@ -111,10 +30,10 @@ var InstanceValidator = module.exports = function(modelInstance, options) {
this.modelInstance = modelInstance; this.modelInstance = modelInstance;
/** /**
* Exposes a reference to validator.js. This allows you to add custom validations using `Validator.extend` * Exposes a reference to validator.js. This allows you to add custom validations using `validator.extend`
* @name Validator * @name validator
*/ */
this.Validator = Validator; this.validator = validator;
/** /**
* All errors will be stored here from the validations. * All errors will be stored here from the validations.
...@@ -223,7 +142,7 @@ InstanceValidator.prototype._customValidators = function() { ...@@ -223,7 +142,7 @@ InstanceValidator.prototype._customValidators = function() {
var valprom = self._invokeCustomValidator(validator, validatorType) var valprom = self._invokeCustomValidator(validator, validatorType)
// errors are handled in settling, stub this // errors are handled in settling, stub this
.catch(noop); .catch(function() {});
validators.push(valprom); validators.push(valprom);
}); });
...@@ -270,7 +189,7 @@ InstanceValidator.prototype._builtinAttrValidate = function(value, field) { ...@@ -270,7 +189,7 @@ InstanceValidator.prototype._builtinAttrValidate = function(value, field) {
var validatorPromise = self._invokeBuiltinValidator(value, test, validatorType, field); var validatorPromise = self._invokeBuiltinValidator(value, test, validatorType, field);
// errors are handled in settling, stub this // errors are handled in settling, stub this
validatorPromise.catch(noop); validatorPromise.catch(function() {});
validators.push(validatorPromise); validators.push(validatorPromise);
}); });
...@@ -329,7 +248,7 @@ InstanceValidator.prototype._invokeCustomValidator = Promise.method(function(val ...@@ -329,7 +248,7 @@ InstanceValidator.prototype._invokeCustomValidator = Promise.method(function(val
*/ */
InstanceValidator.prototype._invokeBuiltinValidator = Promise.method(function(value, test, validatorType, field) { InstanceValidator.prototype._invokeBuiltinValidator = Promise.method(function(value, test, validatorType, field) {
// check if Validator knows that kind of validation test // check if Validator knows that kind of validation test
if (typeof Validator[validatorType] !== 'function') { if (typeof validator[validatorType] !== 'function') {
throw new Error('Invalid validator function: ' + validatorType); throw new Error('Invalid validator function: ' + validatorType);
} }
...@@ -348,7 +267,7 @@ InstanceValidator.prototype._invokeBuiltinValidator = Promise.method(function(va ...@@ -348,7 +267,7 @@ InstanceValidator.prototype._invokeBuiltinValidator = Promise.method(function(va
validatorArgs = validatorArgs.slice(0); validatorArgs = validatorArgs.slice(0);
} }
if (!Validator[validatorType].apply(Validator, [value].concat(validatorArgs))) { if (!validator[validatorType].apply(validator, [value].concat(validatorArgs))) {
// extract the error msg // extract the error msg
throw test.msg || 'Validation ' + validatorType + ' failed'; throw test.msg || 'Validation ' + validatorType + ' failed';
} }
......
'use strict';
var validator = require('validator')
, _ = require('lodash');
var extensions = {
notEmpty: function(str) {
return !str.match(/^[\s\t\r\n]*$/);
},
len: function(str, min, max) {
return this.isLength(str, min, max);
},
isUrl: function(str) {
return this.isURL(str);
},
isIPv6: function(str) {
return this.isIP(str, 6);
},
isIPv4: function(str) {
return this.isIP(str, 4);
},
notIn: function(str, values) {
return !this.isIn(str, values);
},
regex: function(str, pattern, modifiers) {
str += '';
if (Object.prototype.toString.call(pattern).slice(8, -1) !== 'RegExp') {
pattern = new RegExp(pattern, modifiers);
}
return str.match(pattern);
},
notRegex: function(str, pattern, modifiers) {
return !this.regex(str, pattern, modifiers);
},
isDecimal: function(str) {
return str !== '' && str.match(/^(?:-?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/);
},
min: function(str, val) {
var number = parseFloat(str);
return isNaN(number) || number >= val;
},
max: function(str, val) {
var number = parseFloat(str);
return isNaN(number) || number <= val;
},
not: function(str, pattern, modifiers) {
return this.notRegex(str, pattern, modifiers);
},
contains: function(str, elem) {
return str.indexOf(elem) >= 0 && !!elem;
},
notContains: function(str, elem) {
return !this.contains(str, elem);
},
is: function(str, pattern, modifiers) {
return this.regex(str, pattern, modifiers);
},
};
var extendModelValidations = function(modelInstance) {
var extensions = {
isImmutable: function(str, param, field) {
return (modelInstance.isNewRecord || modelInstance.dataValues[field] === modelInstance._previousDataValues[field]);
},
};
_.forEach(extensions, function(extend, key) {
validator.extend(key, extend);
});
};
// Deprecate this.
validator.notNull = function() {
throw new Error('Warning "notNull" validation has been deprecated in favor of Schema based "allowNull"');
};
// https://github.com/chriso/validator.js/blob/1.5.0/lib/validators.js
_.forEach(extensions, function(extend, key) {
validator.extend(key, extend);
});
module.exports = {
extensions: extensions,
extendModelValidations: extendModelValidations,
validator: validator,
};
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!