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

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 31 additions and 32 deletions
'use strict';
var validator = require('validator')
, _ = require('lodash');
const validator = require('validator');
const _ = require('lodash');
var extensions = {
extend: function(name, fn) {
const extensions = {
extend(name, fn) {
this[name] = fn;
return this;
},
notEmpty: function(str) {
notEmpty(str) {
return !str.match(/^[\s\t\r\n]*$/);
},
len: function(str, min, max) {
len(str, min, max) {
return this.isLength(str, min, max);
},
isUrl: function(str) {
isUrl(str) {
return this.isURL(str);
},
isIPv6: function(str) {
isIPv6(str) {
return this.isIP(str, 6);
},
isIPv4: function(str) {
isIPv4(str) {
return this.isIP(str, 4);
},
notIn: function(str, values) {
notIn(str, values) {
return !this.isIn(str, values);
},
regex: function(str, pattern, modifiers) {
regex(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) {
notRegex(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]+))?$/);
},
min: function(str, val) {
var number = parseFloat(str);
min(str, val) {
const number = parseFloat(str);
return isNaN(number) || number >= val;
},
max: function(str, val) {
var number = parseFloat(str);
max(str, val) {
const number = parseFloat(str);
return isNaN(number) || number <= val;
},
not: function(str, pattern, modifiers) {
not(str, pattern, modifiers) {
return this.notRegex(str, pattern, modifiers);
},
contains: function(str, elem) {
contains(str, elem) {
return str.indexOf(elem) >= 0 && !!elem;
},
notContains: function(str, elem) {
notContains(str, elem) {
return !this.contains(str, elem);
},
is: function(str, pattern, modifiers) {
is(str, pattern, modifiers) {
return this.regex(str, pattern, modifiers);
},
};
var extendModelValidations = function(modelInstance) {
var extensions = {
isImmutable: function(str, param, field) {
exports.extensions = extensions;
function extendModelValidations(modelInstance) {
const extensions = {
isImmutable(str, param, field) {
return (modelInstance.isNewRecord || modelInstance.dataValues[field] === modelInstance._previousDataValues[field]);
},
};
_.forEach(extensions, function(extend, key) {
_.forEach(extensions, (extend, key) => {
validator[key] = extend;
});
};
}
exports.extendModelValidations = extendModelValidations;
// Deprecate this.
validator.notNull = function() {
......@@ -79,12 +82,8 @@ validator.notNull = function() {
};
// 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;
});
module.exports = {
extensions: extensions,
extendModelValidations: extendModelValidations,
validator: validator,
};
exports.validator = validator;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!