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

Commit a98be920 by Felix Becker Committed by Mick Hansen

ES6 refactor of validator-extras.js (#6082)

let, const, method shorthands, arrow functions, export
1 parent 2a5773a4
Showing with 34 additions and 35 deletions
'use strict'; 'use strict';
var validator = require('validator') const validator = require('validator');
, _ = require('lodash'); const _ = require('lodash');
var extensions = { const extensions = {
extend: function(name, fn) { extend(name, fn) {
this[name] = fn; this[name] = fn;
return this; return this;
}, },
notEmpty: function(str) { notEmpty(str) {
return !str.match(/^[\s\t\r\n]*$/); return !str.match(/^[\s\t\r\n]*$/);
}, },
len: function(str, min, max) { len(str, min, max) {
return this.isLength(str, min, max); return this.isLength(str, min, max);
}, },
isUrl: function(str) { isUrl(str) {
return this.isURL(str); return this.isURL(str);
}, },
isIPv6: function(str) { isIPv6(str) {
return this.isIP(str, 6); return this.isIP(str, 6);
}, },
isIPv4: function(str) { isIPv4(str) {
return this.isIP(str, 4); return this.isIP(str, 4);
}, },
notIn: function(str, values) { notIn(str, values) {
return !this.isIn(str, values); return !this.isIn(str, values);
}, },
regex: function(str, pattern, modifiers) { regex(str, pattern, modifiers) {
str += ''; str += '';
if (Object.prototype.toString.call(pattern).slice(8, -1) !== 'RegExp') { if (Object.prototype.toString.call(pattern).slice(8, -1) !== 'RegExp') {
pattern = new RegExp(pattern, modifiers); pattern = new RegExp(pattern, modifiers);
} }
return str.match(pattern); return str.match(pattern);
}, },
notRegex: function(str, pattern, modifiers) { notRegex(str, pattern, modifiers) {
return !this.regex(str, pattern, modifiers); return !this.regex(str, pattern, modifiers);
}, },
isDecimal: function(str) { isDecimal(str) {
return str !== '' && str.match(/^(?:-?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/); return str !== '' && str.match(/^(?:-?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/);
}, },
min: function(str, val) { min(str, val) {
var number = parseFloat(str); const number = parseFloat(str);
return isNaN(number) || number >= val; return isNaN(number) || number >= val;
}, },
max: function(str, val) { max(str, val) {
var number = parseFloat(str); const number = parseFloat(str);
return isNaN(number) || number <= val; return isNaN(number) || number <= val;
}, },
not: function(str, pattern, modifiers) { not(str, pattern, modifiers) {
return this.notRegex(str, pattern, modifiers); return this.notRegex(str, pattern, modifiers);
}, },
contains: function(str, elem) { contains(str, elem) {
return str.indexOf(elem) >= 0 && !!elem; return str.indexOf(elem) >= 0 && !!elem;
}, },
notContains: function(str, elem) { notContains(str, elem) {
return !this.contains(str, elem); return !this.contains(str, elem);
}, },
is: function(str, pattern, modifiers) { is(str, pattern, modifiers) {
return this.regex(str, pattern, modifiers); return this.regex(str, pattern, modifiers);
}, },
}; };
var extendModelValidations = function(modelInstance) { exports.extensions = extensions;
var extensions = {
isImmutable: function(str, param, field) {
return (modelInstance.isNewRecord || modelInstance.dataValues[field] === modelInstance._previousDataValues[field]);
},
};
_.forEach(extensions, function(extend, key) { function extendModelValidations(modelInstance) {
const extensions = {
isImmutable(str, param, field) {
return (modelInstance.isNewRecord || modelInstance.dataValues[field] === modelInstance._previousDataValues[field]);
},
};
_.forEach(extensions, (extend, key) => {
validator[key] = extend; validator[key] = extend;
}); });
}; }
exports.extendModelValidations = extendModelValidations;
// Deprecate this. // Deprecate this.
validator.notNull = function() { validator.notNull = function() {
...@@ -79,12 +82,8 @@ validator.notNull = function() { ...@@ -79,12 +82,8 @@ validator.notNull = function() {
}; };
// https://github.com/chriso/validator.js/blob/1.5.0/lib/validators.js // https://github.com/chriso/validator.js/blob/1.5.0/lib/validators.js
_.forEach(extensions, function(extend, key) { _.forEach(extensions, (extend, key) => {
validator[key] = extend; validator[key] = extend;
}); });
module.exports = { exports.validator = validator;
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!