async-validator
Advanced tools
Comparing version 1.10.1 to 1.11.0
119
es/index.js
@@ -1,7 +0,33 @@ | ||
import _extends from 'babel-runtime/helpers/extends'; | ||
import _typeof from 'babel-runtime/helpers/typeof'; | ||
import { format, complementError, asyncMap, warning, deepMerge } from './util'; | ||
import validators from './validator/'; | ||
import { messages as defaultMessages, newMessages } from './messages'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _extends2 = require('babel-runtime/helpers/extends'); | ||
var _extends3 = _interopRequireDefault(_extends2); | ||
var _promise = require('babel-runtime/core-js/promise'); | ||
var _promise2 = _interopRequireDefault(_promise); | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
var _typeof2 = require('babel-runtime/helpers/typeof'); | ||
var _typeof3 = _interopRequireDefault(_typeof2); | ||
var _util = require('./util'); | ||
var _validator = require('./validator/'); | ||
var _validator2 = _interopRequireDefault(_validator); | ||
var _messages2 = require('./messages'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/** | ||
@@ -15,3 +41,3 @@ * Encapsulates a validation schema. | ||
this.rules = null; | ||
this._messages = defaultMessages; | ||
this._messages = _messages2.messages; | ||
this.define(descriptor); | ||
@@ -23,3 +49,3 @@ } | ||
if (_messages) { | ||
this._messages = deepMerge(newMessages(), _messages); | ||
this._messages = (0, _util.deepMerge)((0, _messages2.newMessages)(), _messages); | ||
} | ||
@@ -32,3 +58,3 @@ return this._messages; | ||
} | ||
if ((typeof rules === 'undefined' ? 'undefined' : _typeof(rules)) !== 'object' || Array.isArray(rules)) { | ||
if ((typeof rules === 'undefined' ? 'undefined' : (0, _typeof3['default'])(rules)) !== 'object' || Array.isArray(rules)) { | ||
throw new Error('Rules must be an object'); | ||
@@ -50,3 +76,3 @@ } | ||
var o = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var oc = arguments[2]; | ||
var oc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {}; | ||
@@ -60,11 +86,10 @@ var source = source_; | ||
} | ||
if (!this.rules || Object.keys(this.rules).length === 0) { | ||
if (!this.rules || (0, _keys2['default'])(this.rules).length === 0) { | ||
if (callback) { | ||
callback(); | ||
} | ||
return; | ||
return _promise2['default'].resolve(); | ||
} | ||
function complete(results) { | ||
var i = void 0; | ||
var field = void 0; | ||
var errors = []; | ||
@@ -75,3 +100,5 @@ var fields = {}; | ||
if (Array.isArray(e)) { | ||
errors = errors.concat.apply(errors, e); | ||
var _errors; | ||
errors = (_errors = errors).concat.apply(_errors, e); | ||
} else { | ||
@@ -89,7 +116,3 @@ errors.push(e); | ||
} else { | ||
for (i = 0; i < errors.length; i++) { | ||
field = errors[i].field; | ||
fields[field] = fields[field] || []; | ||
fields[field].push(errors[i]); | ||
} | ||
fields = (0, _util.convertFieldsError)(errors); | ||
} | ||
@@ -101,6 +124,6 @@ callback(errors, fields); | ||
var messages = this.messages(); | ||
if (messages === defaultMessages) { | ||
messages = newMessages(); | ||
if (messages === _messages2.messages) { | ||
messages = (0, _messages2.newMessages)(); | ||
} | ||
deepMerge(messages, options.messages); | ||
(0, _util.deepMerge)(messages, options.messages); | ||
options.messages = messages; | ||
@@ -113,3 +136,3 @@ } else { | ||
var series = {}; | ||
var keys = options.keys || Object.keys(this.rules); | ||
var keys = options.keys || (0, _keys2['default'])(this.rules); | ||
keys.forEach(function (z) { | ||
@@ -122,3 +145,3 @@ arr = _this.rules[z]; | ||
if (source === source_) { | ||
source = _extends({}, source); | ||
source = (0, _extends3['default'])({}, source); | ||
} | ||
@@ -132,3 +155,3 @@ value = source[z] = rule.transform(value); | ||
} else { | ||
rule = _extends({}, rule); | ||
rule = (0, _extends3['default'])({}, rule); | ||
} | ||
@@ -152,9 +175,9 @@ rule.validator = _this.getValidationMethod(rule); | ||
var errorFields = {}; | ||
asyncMap(series, options, function (data, doIt) { | ||
return (0, _util.asyncMap)(series, options, function (data, doIt) { | ||
var rule = data.rule; | ||
var deep = (rule.type === 'object' || rule.type === 'array') && (_typeof(rule.fields) === 'object' || _typeof(rule.defaultField) === 'object'); | ||
var deep = (rule.type === 'object' || rule.type === 'array') && ((0, _typeof3['default'])(rule.fields) === 'object' || (0, _typeof3['default'])(rule.defaultField) === 'object'); | ||
deep = deep && (rule.required || !rule.required && data.value); | ||
rule.field = data.field; | ||
function addFullfield(key, schema) { | ||
return _extends({}, schema, { | ||
return (0, _extends3['default'])({}, schema, { | ||
fullField: rule.fullField + '.' + key | ||
@@ -178,3 +201,3 @@ }); | ||
errors = errors.map(complementError(rule)); | ||
errors = errors.map((0, _util.complementError)(rule)); | ||
@@ -193,5 +216,5 @@ if (options.first && errors.length) { | ||
if (rule.message) { | ||
errors = [].concat(rule.message).map(complementError(rule)); | ||
errors = [].concat(rule.message).map((0, _util.complementError)(rule)); | ||
} else if (options.error) { | ||
errors = [options.error(rule, format(options.messages.required, rule.field))]; | ||
errors = [options.error(rule, (0, _util.format)(options.messages.required, rule.field))]; | ||
} else { | ||
@@ -211,3 +234,3 @@ errors = []; | ||
} | ||
fieldsSchema = _extends({}, fieldsSchema, data.rule.fields); | ||
fieldsSchema = (0, _extends3['default'])({}, fieldsSchema, data.rule.fields); | ||
for (var f in fieldsSchema) { | ||
@@ -231,3 +254,17 @@ if (fieldsSchema.hasOwnProperty(f)) { | ||
var res = rule.validator(rule, data.value, cb, data.source, options); | ||
var res = void 0; | ||
if (rule.asyncValidator) { | ||
res = rule.asyncValidator(rule, data.value, cb, data.source, options); | ||
} else if (rule.validator) { | ||
res = rule.validator(rule, data.value, cb, data.source, options); | ||
if (res === true) { | ||
cb(); | ||
} else if (res === false) { | ||
cb(rule.message || rule.field + ' fails'); | ||
} else if (res instanceof Array) { | ||
cb(res); | ||
} else if (res instanceof Error) { | ||
cb(res.message); | ||
} | ||
} | ||
if (res && res.then) { | ||
@@ -248,4 +285,4 @@ res.then(function () { | ||
} | ||
if (typeof rule.validator !== 'function' && rule.type && !validators.hasOwnProperty(rule.type)) { | ||
throw new Error(format('Unknown rule type %s', rule.type)); | ||
if (typeof rule.validator !== 'function' && rule.type && !_validator2['default'].hasOwnProperty(rule.type)) { | ||
throw new Error((0, _util.format)('Unknown rule type %s', rule.type)); | ||
} | ||
@@ -258,3 +295,3 @@ return rule.type || 'string'; | ||
} | ||
var keys = Object.keys(rule); | ||
var keys = (0, _keys2['default'])(rule); | ||
var messageIndex = keys.indexOf('message'); | ||
@@ -265,5 +302,5 @@ if (messageIndex !== -1) { | ||
if (keys.length === 1 && keys[0] === 'required') { | ||
return validators.required; | ||
return _validator2['default'].required; | ||
} | ||
return validators[this.getType(rule)] || false; | ||
return _validator2['default'][this.getType(rule)] || false; | ||
} | ||
@@ -276,9 +313,9 @@ }; | ||
} | ||
validators[type] = validator; | ||
_validator2['default'][type] = validator; | ||
}; | ||
Schema.warning = warning; | ||
Schema.warning = _util.warning; | ||
Schema.messages = defaultMessages; | ||
Schema.messages = _messages2.messages; | ||
export default Schema; | ||
exports['default'] = Schema; |
@@ -1,2 +0,17 @@ | ||
export function newMessages() { | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.messages = undefined; | ||
var _stringify = require('babel-runtime/core-js/json/stringify'); | ||
var _stringify2 = _interopRequireDefault(_stringify); | ||
exports.newMessages = newMessages; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function newMessages() { | ||
return { | ||
@@ -49,3 +64,3 @@ 'default': 'Validation error on field %s', | ||
clone: function clone() { | ||
var cloned = JSON.parse(JSON.stringify(this)); | ||
var cloned = JSON.parse((0, _stringify2['default'])(this)); | ||
cloned.clone = this.clone; | ||
@@ -57,2 +72,2 @@ return cloned; | ||
export var messages = newMessages(); | ||
var messages = exports.messages = newMessages(); |
@@ -1,2 +0,13 @@ | ||
import * as util from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _util = require('../util'); | ||
var util = _interopRequireWildcard(_util); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
var ENUM = 'enum'; | ||
@@ -22,2 +33,2 @@ | ||
export default enumerable; | ||
exports['default'] = enumerable; |
@@ -1,15 +0,40 @@ | ||
import required from './required'; | ||
import whitespace from './whitespace'; | ||
import type from './type'; | ||
import range from './range'; | ||
import enumRule from './enum'; | ||
import pattern from './pattern'; | ||
'use strict'; | ||
export default { | ||
required: required, | ||
whitespace: whitespace, | ||
type: type, | ||
range: range, | ||
'enum': enumRule, | ||
pattern: pattern | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _required = require('./required'); | ||
var _required2 = _interopRequireDefault(_required); | ||
var _whitespace = require('./whitespace'); | ||
var _whitespace2 = _interopRequireDefault(_whitespace); | ||
var _type = require('./type'); | ||
var _type2 = _interopRequireDefault(_type); | ||
var _range = require('./range'); | ||
var _range2 = _interopRequireDefault(_range); | ||
var _enum = require('./enum'); | ||
var _enum2 = _interopRequireDefault(_enum); | ||
var _pattern = require('./pattern'); | ||
var _pattern2 = _interopRequireDefault(_pattern); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
exports['default'] = { | ||
required: _required2['default'], | ||
whitespace: _whitespace2['default'], | ||
type: _type2['default'], | ||
range: _range2['default'], | ||
'enum': _enum2['default'], | ||
pattern: _pattern2['default'] | ||
}; |
@@ -1,3 +0,13 @@ | ||
import * as util from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _util = require('../util'); | ||
var util = _interopRequireWildcard(_util); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
/** | ||
@@ -33,2 +43,2 @@ * Rule for validating a regular expression pattern. | ||
export default pattern; | ||
exports['default'] = pattern; |
@@ -1,3 +0,13 @@ | ||
import * as util from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _util = require('../util'); | ||
var util = _interopRequireWildcard(_util); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
/** | ||
@@ -58,2 +68,2 @@ * Rule for validating minimum and maximum allowed values. | ||
export default range; | ||
exports['default'] = range; |
@@ -1,3 +0,13 @@ | ||
import * as util from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _util = require('../util'); | ||
var util = _interopRequireWildcard(_util); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
/** | ||
@@ -20,2 +30,2 @@ * Rule for validating required fields. | ||
export default required; | ||
exports['default'] = required; |
@@ -1,5 +0,23 @@ | ||
import _typeof from 'babel-runtime/helpers/typeof'; | ||
import * as util from '../util'; | ||
import required from './required'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _typeof2 = require('babel-runtime/helpers/typeof'); | ||
var _typeof3 = _interopRequireDefault(_typeof2); | ||
var _util = require('../util'); | ||
var util = _interopRequireWildcard(_util); | ||
var _required = require('./required'); | ||
var _required2 = _interopRequireDefault(_required); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/* eslint max-len:0 */ | ||
@@ -44,3 +62,3 @@ | ||
object: function object(value) { | ||
return (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && !types.array(value); | ||
return (typeof value === 'undefined' ? 'undefined' : (0, _typeof3['default'])(value)) === 'object' && !types.array(value); | ||
}, | ||
@@ -74,3 +92,3 @@ method: function method(value) { | ||
if (rule.required && value === undefined) { | ||
required(rule, value, source, errors, options); | ||
(0, _required2['default'])(rule, value, source, errors, options); | ||
return; | ||
@@ -85,3 +103,3 @@ } | ||
// straight typeof check | ||
} else if (ruleType && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) !== rule.type) { | ||
} else if (ruleType && (typeof value === 'undefined' ? 'undefined' : (0, _typeof3['default'])(value)) !== rule.type) { | ||
errors.push(util.format(options.messages.types[ruleType], rule.fullField, rule.type)); | ||
@@ -91,2 +109,2 @@ } | ||
export default type; | ||
exports['default'] = type; |
@@ -1,3 +0,13 @@ | ||
import * as util from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _util = require('../util'); | ||
var util = _interopRequireWildcard(_util); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
/** | ||
@@ -20,2 +30,2 @@ * Rule for validating whitespace. | ||
export default whitespace; | ||
exports['default'] = whitespace; |
117
es/util.js
@@ -1,10 +0,47 @@ | ||
import _extends from 'babel-runtime/helpers/extends'; | ||
import _typeof from 'babel-runtime/helpers/typeof'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.warning = undefined; | ||
var _extends2 = require('babel-runtime/helpers/extends'); | ||
var _extends3 = _interopRequireDefault(_extends2); | ||
var _typeof2 = require('babel-runtime/helpers/typeof'); | ||
var _typeof3 = _interopRequireDefault(_typeof2); | ||
var _promise = require('babel-runtime/core-js/promise'); | ||
var _promise2 = _interopRequireDefault(_promise); | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
var _stringify = require('babel-runtime/core-js/json/stringify'); | ||
var _stringify2 = _interopRequireDefault(_stringify); | ||
exports.convertFieldsError = convertFieldsError; | ||
exports.format = format; | ||
exports.isEmptyValue = isEmptyValue; | ||
exports.isEmptyObject = isEmptyObject; | ||
exports.asyncMap = asyncMap; | ||
exports.complementError = complementError; | ||
exports.deepMerge = deepMerge; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/* eslint no-console:0 */ | ||
var formatRegExp = /%[sdj%]/g; | ||
export var warning = function warning() {}; | ||
var warning = exports.warning = function warning() {}; | ||
// don't print warning message when in production env or node runtime | ||
if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined' && typeof document !== 'undefined') { | ||
warning = function warning(type, errors) { | ||
exports.warning = warning = function warning(type, errors) { | ||
if (typeof console !== 'undefined' && console.warn) { | ||
@@ -20,3 +57,14 @@ if (errors.every(function (e) { | ||
export function format() { | ||
function convertFieldsError(errors) { | ||
if (!errors || !errors.length) return null; | ||
var fields = {}; | ||
errors.forEach(function (error) { | ||
var field = error.field; | ||
fields[field] = fields[field] || []; | ||
fields[field].push(error); | ||
}); | ||
return fields; | ||
} | ||
function format() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
@@ -47,3 +95,3 @@ args[_key] = arguments[_key]; | ||
try { | ||
return JSON.stringify(args[i++]); | ||
return (0, _stringify2['default'])(args[i++]); | ||
} catch (_) { | ||
@@ -69,3 +117,3 @@ return '[Circular]'; | ||
export function isEmptyValue(value, type) { | ||
function isEmptyValue(value, type) { | ||
if (value === undefined || value === null) { | ||
@@ -83,4 +131,4 @@ return true; | ||
export function isEmptyObject(obj) { | ||
return Object.keys(obj).length === 0; | ||
function isEmptyObject(obj) { | ||
return (0, _keys2['default'])(obj).length === 0; | ||
} | ||
@@ -129,3 +177,3 @@ | ||
var ret = []; | ||
Object.keys(objArr).forEach(function (k) { | ||
(0, _keys2['default'])(objArr).forEach(function (k) { | ||
ret.push.apply(ret, objArr[k]); | ||
@@ -136,3 +184,3 @@ }); | ||
export function asyncMap(objArr, option, func, callback) { | ||
function asyncMap(objArr, option, func, callback) { | ||
if (option.first) { | ||
@@ -144,26 +192,33 @@ var flattenArr = flattenObjArr(objArr); | ||
if (firstFields === true) { | ||
firstFields = Object.keys(objArr); | ||
firstFields = (0, _keys2['default'])(objArr); | ||
} | ||
var objArrKeys = Object.keys(objArr); | ||
var objArrKeys = (0, _keys2['default'])(objArr); | ||
var objArrLength = objArrKeys.length; | ||
var total = 0; | ||
var results = []; | ||
var next = function next(errors) { | ||
results.push.apply(results, errors); | ||
total++; | ||
if (total === objArrLength) { | ||
callback(results); | ||
} | ||
}; | ||
objArrKeys.forEach(function (key) { | ||
var arr = objArr[key]; | ||
if (firstFields.indexOf(key) !== -1) { | ||
asyncSerialArray(arr, func, next); | ||
} else { | ||
asyncParallelArray(arr, func, next); | ||
} | ||
var pending = new _promise2['default'](function (resolve, reject) { | ||
var next = function next(errors) { | ||
results.push.apply(results, errors); | ||
total++; | ||
if (total === objArrLength) { | ||
callback(results); | ||
return results.length ? reject({ errors: results, fields: convertFieldsError(results) }) : resolve(); | ||
} | ||
}; | ||
objArrKeys.forEach(function (key) { | ||
var arr = objArr[key]; | ||
if (firstFields.indexOf(key) !== -1) { | ||
asyncSerialArray(arr, func, next); | ||
} else { | ||
asyncParallelArray(arr, func, next); | ||
} | ||
}); | ||
}); | ||
pending['catch'](function (e) { | ||
return e; | ||
}); | ||
return pending; | ||
} | ||
export function complementError(rule) { | ||
function complementError(rule) { | ||
return function (oe) { | ||
@@ -181,3 +236,3 @@ if (oe && oe.message) { | ||
export function deepMerge(target, source) { | ||
function deepMerge(target, source) { | ||
if (source) { | ||
@@ -187,4 +242,4 @@ for (var s in source) { | ||
var value = source[s]; | ||
if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && _typeof(target[s]) === 'object') { | ||
target[s] = _extends({}, target[s], value); | ||
if ((typeof value === 'undefined' ? 'undefined' : (0, _typeof3['default'])(value)) === 'object' && (0, _typeof3['default'])(target[s]) === 'object') { | ||
target[s] = (0, _extends3['default'])({}, target[s], value); | ||
} else { | ||
@@ -191,0 +246,0 @@ target[s] = value; |
@@ -1,3 +0,15 @@ | ||
import rules from '../rule/'; | ||
import { isEmptyValue } from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
var _util = require('../util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/** | ||
@@ -17,9 +29,9 @@ * Validates an array. | ||
if (validate) { | ||
if (isEmptyValue(value, 'array') && !rule.required) { | ||
if ((0, _util.isEmptyValue)(value, 'array') && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options, 'array'); | ||
if (!isEmptyValue(value, 'array')) { | ||
rules.type(rule, value, source, errors, options); | ||
rules.range(rule, value, source, errors, options); | ||
_rule2['default'].required(rule, value, source, errors, options, 'array'); | ||
if (!(0, _util.isEmptyValue)(value, 'array')) { | ||
_rule2['default'].type(rule, value, source, errors, options); | ||
_rule2['default'].range(rule, value, source, errors, options); | ||
} | ||
@@ -30,2 +42,2 @@ } | ||
export default array; | ||
exports['default'] = array; |
@@ -1,4 +0,15 @@ | ||
import { isEmptyValue } from '../util'; | ||
import rules from '../rule/'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _util = require('../util'); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/** | ||
@@ -18,8 +29,8 @@ * Validates a boolean. | ||
if (validate) { | ||
if (isEmptyValue(value) && !rule.required) { | ||
if ((0, _util.isEmptyValue)(value) && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options); | ||
_rule2['default'].required(rule, value, source, errors, options); | ||
if (value !== undefined) { | ||
rules.type(rule, value, source, errors, options); | ||
_rule2['default'].type(rule, value, source, errors, options); | ||
} | ||
@@ -30,2 +41,2 @@ } | ||
export default boolean; | ||
exports['default'] = boolean; |
@@ -1,4 +0,15 @@ | ||
import rules from '../rule/'; | ||
import { isEmptyValue } from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
var _util = require('../util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function date(rule, value, callback, source, options) { | ||
@@ -10,7 +21,7 @@ // console.log('integer rule called %j', rule); | ||
if (validate) { | ||
if (isEmptyValue(value) && !rule.required) { | ||
if ((0, _util.isEmptyValue)(value) && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options); | ||
if (!isEmptyValue(value)) { | ||
_rule2['default'].required(rule, value, source, errors, options); | ||
if (!(0, _util.isEmptyValue)(value)) { | ||
var dateObject = void 0; | ||
@@ -24,5 +35,5 @@ | ||
rules.type(rule, dateObject, source, errors, options); | ||
_rule2['default'].type(rule, dateObject, source, errors, options); | ||
if (dateObject) { | ||
rules.range(rule, dateObject.getTime(), source, errors, options); | ||
_rule2['default'].range(rule, dateObject.getTime(), source, errors, options); | ||
} | ||
@@ -34,2 +45,2 @@ } | ||
export default date; | ||
exports['default'] = date; |
@@ -1,3 +0,15 @@ | ||
import rules from '../rule/'; | ||
import { isEmptyValue } from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
var _util = require('../util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var ENUM = 'enum'; | ||
@@ -19,8 +31,8 @@ | ||
if (validate) { | ||
if (isEmptyValue(value) && !rule.required) { | ||
if ((0, _util.isEmptyValue)(value) && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options); | ||
_rule2['default'].required(rule, value, source, errors, options); | ||
if (value) { | ||
rules[ENUM](rule, value, source, errors, options); | ||
_rule2['default'][ENUM](rule, value, source, errors, options); | ||
} | ||
@@ -31,2 +43,2 @@ } | ||
export default enumerable; | ||
exports['default'] = enumerable; |
@@ -1,4 +0,15 @@ | ||
import rules from '../rule/'; | ||
import { isEmptyValue } from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
var _util = require('../util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/** | ||
@@ -18,9 +29,9 @@ * Validates a number is a floating point number. | ||
if (validate) { | ||
if (isEmptyValue(value) && !rule.required) { | ||
if ((0, _util.isEmptyValue)(value) && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options); | ||
_rule2['default'].required(rule, value, source, errors, options); | ||
if (value !== undefined) { | ||
rules.type(rule, value, source, errors, options); | ||
rules.range(rule, value, source, errors, options); | ||
_rule2['default'].type(rule, value, source, errors, options); | ||
_rule2['default'].range(rule, value, source, errors, options); | ||
} | ||
@@ -31,2 +42,2 @@ } | ||
export default floatFn; | ||
exports['default'] = floatFn; |
@@ -1,33 +0,82 @@ | ||
import string from './string'; | ||
import method from './method'; | ||
import number from './number'; | ||
import boolean from './boolean'; | ||
import regexp from './regexp'; | ||
import integer from './integer'; | ||
import float from './float'; | ||
import array from './array'; | ||
import object from './object'; | ||
import enumValidator from './enum'; | ||
import pattern from './pattern'; | ||
import date from './date'; | ||
import required from './required'; | ||
import type from './type'; | ||
'use strict'; | ||
export default { | ||
string: string, | ||
method: method, | ||
number: number, | ||
boolean: boolean, | ||
regexp: regexp, | ||
integer: integer, | ||
float: float, | ||
array: array, | ||
object: object, | ||
'enum': enumValidator, | ||
pattern: pattern, | ||
date: date, | ||
url: type, | ||
hex: type, | ||
email: type, | ||
required: required | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _string = require('./string'); | ||
var _string2 = _interopRequireDefault(_string); | ||
var _method = require('./method'); | ||
var _method2 = _interopRequireDefault(_method); | ||
var _number = require('./number'); | ||
var _number2 = _interopRequireDefault(_number); | ||
var _boolean = require('./boolean'); | ||
var _boolean2 = _interopRequireDefault(_boolean); | ||
var _regexp = require('./regexp'); | ||
var _regexp2 = _interopRequireDefault(_regexp); | ||
var _integer = require('./integer'); | ||
var _integer2 = _interopRequireDefault(_integer); | ||
var _float = require('./float'); | ||
var _float2 = _interopRequireDefault(_float); | ||
var _array = require('./array'); | ||
var _array2 = _interopRequireDefault(_array); | ||
var _object = require('./object'); | ||
var _object2 = _interopRequireDefault(_object); | ||
var _enum = require('./enum'); | ||
var _enum2 = _interopRequireDefault(_enum); | ||
var _pattern = require('./pattern'); | ||
var _pattern2 = _interopRequireDefault(_pattern); | ||
var _date = require('./date'); | ||
var _date2 = _interopRequireDefault(_date); | ||
var _required = require('./required'); | ||
var _required2 = _interopRequireDefault(_required); | ||
var _type = require('./type'); | ||
var _type2 = _interopRequireDefault(_type); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
exports['default'] = { | ||
string: _string2['default'], | ||
method: _method2['default'], | ||
number: _number2['default'], | ||
boolean: _boolean2['default'], | ||
regexp: _regexp2['default'], | ||
integer: _integer2['default'], | ||
float: _float2['default'], | ||
array: _array2['default'], | ||
object: _object2['default'], | ||
'enum': _enum2['default'], | ||
pattern: _pattern2['default'], | ||
date: _date2['default'], | ||
url: _type2['default'], | ||
hex: _type2['default'], | ||
email: _type2['default'], | ||
required: _required2['default'] | ||
}; |
@@ -1,4 +0,15 @@ | ||
import rules from '../rule/'; | ||
import { isEmptyValue } from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
var _util = require('../util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/** | ||
@@ -18,9 +29,9 @@ * Validates a number is an integer. | ||
if (validate) { | ||
if (isEmptyValue(value) && !rule.required) { | ||
if ((0, _util.isEmptyValue)(value) && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options); | ||
_rule2['default'].required(rule, value, source, errors, options); | ||
if (value !== undefined) { | ||
rules.type(rule, value, source, errors, options); | ||
rules.range(rule, value, source, errors, options); | ||
_rule2['default'].type(rule, value, source, errors, options); | ||
_rule2['default'].range(rule, value, source, errors, options); | ||
} | ||
@@ -31,2 +42,2 @@ } | ||
export default integer; | ||
exports['default'] = integer; |
@@ -1,4 +0,15 @@ | ||
import rules from '../rule/'; | ||
import { isEmptyValue } from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
var _util = require('../util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/** | ||
@@ -18,8 +29,8 @@ * Validates a function. | ||
if (validate) { | ||
if (isEmptyValue(value) && !rule.required) { | ||
if ((0, _util.isEmptyValue)(value) && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options); | ||
_rule2['default'].required(rule, value, source, errors, options); | ||
if (value !== undefined) { | ||
rules.type(rule, value, source, errors, options); | ||
_rule2['default'].type(rule, value, source, errors, options); | ||
} | ||
@@ -30,2 +41,2 @@ } | ||
export default method; | ||
exports['default'] = method; |
@@ -1,4 +0,15 @@ | ||
import rules from '../rule/'; | ||
import { isEmptyValue } from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
var _util = require('../util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/** | ||
@@ -18,9 +29,12 @@ * Validates a number. | ||
if (validate) { | ||
if (isEmptyValue(value) && !rule.required) { | ||
if (value === '') { | ||
value = undefined; | ||
} | ||
if ((0, _util.isEmptyValue)(value) && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options); | ||
_rule2['default'].required(rule, value, source, errors, options); | ||
if (value !== undefined) { | ||
rules.type(rule, value, source, errors, options); | ||
rules.range(rule, value, source, errors, options); | ||
_rule2['default'].type(rule, value, source, errors, options); | ||
_rule2['default'].range(rule, value, source, errors, options); | ||
} | ||
@@ -31,2 +45,2 @@ } | ||
export default number; | ||
exports['default'] = number; |
@@ -1,4 +0,15 @@ | ||
import rules from '../rule/'; | ||
import { isEmptyValue } from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
var _util = require('../util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/** | ||
@@ -18,8 +29,8 @@ * Validates an object. | ||
if (validate) { | ||
if (isEmptyValue(value) && !rule.required) { | ||
if ((0, _util.isEmptyValue)(value) && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options); | ||
_rule2['default'].required(rule, value, source, errors, options); | ||
if (value !== undefined) { | ||
rules.type(rule, value, source, errors, options); | ||
_rule2['default'].type(rule, value, source, errors, options); | ||
} | ||
@@ -30,2 +41,2 @@ } | ||
export default object; | ||
exports['default'] = object; |
@@ -1,4 +0,15 @@ | ||
import rules from '../rule/'; | ||
import { isEmptyValue } from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
var _util = require('../util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/** | ||
@@ -21,8 +32,8 @@ * Validates a regular expression pattern. | ||
if (validate) { | ||
if (isEmptyValue(value, 'string') && !rule.required) { | ||
if ((0, _util.isEmptyValue)(value, 'string') && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options); | ||
if (!isEmptyValue(value, 'string')) { | ||
rules.pattern(rule, value, source, errors, options); | ||
_rule2['default'].required(rule, value, source, errors, options); | ||
if (!(0, _util.isEmptyValue)(value, 'string')) { | ||
_rule2['default'].pattern(rule, value, source, errors, options); | ||
} | ||
@@ -33,2 +44,2 @@ } | ||
export default pattern; | ||
exports['default'] = pattern; |
@@ -1,4 +0,15 @@ | ||
import rules from '../rule/'; | ||
import { isEmptyValue } from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
var _util = require('../util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/** | ||
@@ -18,8 +29,8 @@ * Validates the regular expression type. | ||
if (validate) { | ||
if (isEmptyValue(value) && !rule.required) { | ||
if ((0, _util.isEmptyValue)(value) && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options); | ||
if (!isEmptyValue(value)) { | ||
rules.type(rule, value, source, errors, options); | ||
_rule2['default'].required(rule, value, source, errors, options); | ||
if (!(0, _util.isEmptyValue)(value)) { | ||
_rule2['default'].type(rule, value, source, errors, options); | ||
} | ||
@@ -30,2 +41,2 @@ } | ||
export default regexp; | ||
exports['default'] = regexp; |
@@ -1,11 +0,24 @@ | ||
import _typeof from 'babel-runtime/helpers/typeof'; | ||
import rules from '../rule/'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _typeof2 = require('babel-runtime/helpers/typeof'); | ||
var _typeof3 = _interopRequireDefault(_typeof2); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function required(rule, value, callback, source, options) { | ||
var errors = []; | ||
var type = Array.isArray(value) ? 'array' : typeof value === 'undefined' ? 'undefined' : _typeof(value); | ||
rules.required(rule, value, source, errors, options, type); | ||
var type = Array.isArray(value) ? 'array' : typeof value === 'undefined' ? 'undefined' : (0, _typeof3['default'])(value); | ||
_rule2['default'].required(rule, value, source, errors, options, type); | ||
callback(errors); | ||
} | ||
export default required; | ||
exports['default'] = required; |
@@ -1,4 +0,15 @@ | ||
import rules from '../rule/'; | ||
import { isEmptyValue } from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
var _util = require('../util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
/** | ||
@@ -18,12 +29,12 @@ * Performs validation for string types. | ||
if (validate) { | ||
if (isEmptyValue(value, 'string') && !rule.required) { | ||
if ((0, _util.isEmptyValue)(value, 'string') && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options, 'string'); | ||
if (!isEmptyValue(value, 'string')) { | ||
rules.type(rule, value, source, errors, options); | ||
rules.range(rule, value, source, errors, options); | ||
rules.pattern(rule, value, source, errors, options); | ||
_rule2['default'].required(rule, value, source, errors, options, 'string'); | ||
if (!(0, _util.isEmptyValue)(value, 'string')) { | ||
_rule2['default'].type(rule, value, source, errors, options); | ||
_rule2['default'].range(rule, value, source, errors, options); | ||
_rule2['default'].pattern(rule, value, source, errors, options); | ||
if (rule.whitespace === true) { | ||
rules.whitespace(rule, value, source, errors, options); | ||
_rule2['default'].whitespace(rule, value, source, errors, options); | ||
} | ||
@@ -35,2 +46,2 @@ } | ||
export default string; | ||
exports['default'] = string; |
@@ -1,4 +0,15 @@ | ||
import rules from '../rule/'; | ||
import { isEmptyValue } from '../util'; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _rule = require('../rule/'); | ||
var _rule2 = _interopRequireDefault(_rule); | ||
var _util = require('../util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function type(rule, value, callback, source, options) { | ||
@@ -9,8 +20,8 @@ var ruleType = rule.type; | ||
if (validate) { | ||
if (isEmptyValue(value, ruleType) && !rule.required) { | ||
if ((0, _util.isEmptyValue)(value, ruleType) && !rule.required) { | ||
return callback(); | ||
} | ||
rules.required(rule, value, source, errors, options, ruleType); | ||
if (!isEmptyValue(value, ruleType)) { | ||
rules.type(rule, value, source, errors, options); | ||
_rule2['default'].required(rule, value, source, errors, options, ruleType); | ||
if (!(0, _util.isEmptyValue)(value, ruleType)) { | ||
_rule2['default'].type(rule, value, source, errors, options); | ||
} | ||
@@ -21,2 +32,2 @@ } | ||
export default type; | ||
exports['default'] = type; |
# History | ||
---- | ||
## 1.11.0 / 2019-03-22 | ||
- support promise usage(asyncValidator) | ||
## 1.10.1 / 2018-12-18 | ||
@@ -38,2 +42,2 @@ | ||
- add firstFields option. | ||
- see tests/validator.spec.js | ||
- see tests/validator.spec.js |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -11,2 +9,10 @@ var _extends2 = require('babel-runtime/helpers/extends'); | ||
var _promise = require('babel-runtime/core-js/promise'); | ||
var _promise2 = _interopRequireDefault(_promise); | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
var _typeof2 = require('babel-runtime/helpers/typeof'); | ||
@@ -66,3 +72,3 @@ | ||
var o = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var oc = arguments[2]; | ||
var oc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {}; | ||
@@ -76,11 +82,10 @@ var source = source_; | ||
} | ||
if (!this.rules || Object.keys(this.rules).length === 0) { | ||
if (!this.rules || (0, _keys2['default'])(this.rules).length === 0) { | ||
if (callback) { | ||
callback(); | ||
} | ||
return; | ||
return _promise2['default'].resolve(); | ||
} | ||
function complete(results) { | ||
var i = void 0; | ||
var field = void 0; | ||
var errors = []; | ||
@@ -91,3 +96,5 @@ var fields = {}; | ||
if (Array.isArray(e)) { | ||
errors = errors.concat.apply(errors, e); | ||
var _errors; | ||
errors = (_errors = errors).concat.apply(_errors, e); | ||
} else { | ||
@@ -105,7 +112,3 @@ errors.push(e); | ||
} else { | ||
for (i = 0; i < errors.length; i++) { | ||
field = errors[i].field; | ||
fields[field] = fields[field] || []; | ||
fields[field].push(errors[i]); | ||
} | ||
fields = (0, _util.convertFieldsError)(errors); | ||
} | ||
@@ -128,3 +131,3 @@ callback(errors, fields); | ||
var series = {}; | ||
var keys = options.keys || Object.keys(this.rules); | ||
var keys = options.keys || (0, _keys2['default'])(this.rules); | ||
keys.forEach(function (z) { | ||
@@ -165,3 +168,3 @@ arr = _this.rules[z]; | ||
var errorFields = {}; | ||
(0, _util.asyncMap)(series, options, function (data, doIt) { | ||
return (0, _util.asyncMap)(series, options, function (data, doIt) { | ||
var rule = data.rule; | ||
@@ -241,3 +244,17 @@ var deep = (rule.type === 'object' || rule.type === 'array') && ((0, _typeof3['default'])(rule.fields) === 'object' || (0, _typeof3['default'])(rule.defaultField) === 'object'); | ||
var res = rule.validator(rule, data.value, cb, data.source, options); | ||
var res = void 0; | ||
if (rule.asyncValidator) { | ||
res = rule.asyncValidator(rule, data.value, cb, data.source, options); | ||
} else if (rule.validator) { | ||
res = rule.validator(rule, data.value, cb, data.source, options); | ||
if (res === true) { | ||
cb(); | ||
} else if (res === false) { | ||
cb(rule.message || rule.field + ' fails'); | ||
} else if (res instanceof Array) { | ||
cb(res); | ||
} else if (res instanceof Error) { | ||
cb(res.message); | ||
} | ||
} | ||
if (res && res.then) { | ||
@@ -267,3 +284,3 @@ res.then(function () { | ||
} | ||
var keys = Object.keys(rule); | ||
var keys = (0, _keys2['default'])(rule); | ||
var messageIndex = keys.indexOf('message'); | ||
@@ -270,0 +287,0 @@ if (messageIndex !== -1) { |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports.messages = undefined; | ||
var _stringify = require('babel-runtime/core-js/json/stringify'); | ||
var _stringify2 = _interopRequireDefault(_stringify); | ||
exports.newMessages = newMessages; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function newMessages() { | ||
@@ -55,3 +62,3 @@ return { | ||
clone: function clone() { | ||
var cloned = JSON.parse(JSON.stringify(this)); | ||
var cloned = JSON.parse((0, _stringify2['default'])(this)); | ||
cloned.clone = this.clone; | ||
@@ -58,0 +65,0 @@ return cloned; |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _util = require('../util'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _required = require('./required'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _util = require('../util'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _util = require('../util'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _util = require('../util'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _typeof2 = require('babel-runtime/helpers/typeof'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _util = require('../util'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports.warning = undefined; | ||
@@ -16,2 +14,15 @@ | ||
var _promise = require('babel-runtime/core-js/promise'); | ||
var _promise2 = _interopRequireDefault(_promise); | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
var _stringify = require('babel-runtime/core-js/json/stringify'); | ||
var _stringify2 = _interopRequireDefault(_stringify); | ||
exports.convertFieldsError = convertFieldsError; | ||
exports.format = format; | ||
@@ -26,2 +37,4 @@ exports.isEmptyValue = isEmptyValue; | ||
/* eslint no-console:0 */ | ||
var formatRegExp = /%[sdj%]/g; | ||
@@ -44,2 +57,13 @@ | ||
function convertFieldsError(errors) { | ||
if (!errors || !errors.length) return null; | ||
var fields = {}; | ||
errors.forEach(function (error) { | ||
var field = error.field; | ||
fields[field] = fields[field] || []; | ||
fields[field].push(error); | ||
}); | ||
return fields; | ||
} | ||
function format() { | ||
@@ -71,3 +95,3 @@ for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
try { | ||
return JSON.stringify(args[i++]); | ||
return (0, _stringify2['default'])(args[i++]); | ||
} catch (_) { | ||
@@ -107,3 +131,3 @@ return '[Circular]'; | ||
function isEmptyObject(obj) { | ||
return Object.keys(obj).length === 0; | ||
return (0, _keys2['default'])(obj).length === 0; | ||
} | ||
@@ -152,3 +176,3 @@ | ||
var ret = []; | ||
Object.keys(objArr).forEach(function (k) { | ||
(0, _keys2['default'])(objArr).forEach(function (k) { | ||
ret.push.apply(ret, objArr[k]); | ||
@@ -166,23 +190,30 @@ }); | ||
if (firstFields === true) { | ||
firstFields = Object.keys(objArr); | ||
firstFields = (0, _keys2['default'])(objArr); | ||
} | ||
var objArrKeys = Object.keys(objArr); | ||
var objArrKeys = (0, _keys2['default'])(objArr); | ||
var objArrLength = objArrKeys.length; | ||
var total = 0; | ||
var results = []; | ||
var next = function next(errors) { | ||
results.push.apply(results, errors); | ||
total++; | ||
if (total === objArrLength) { | ||
callback(results); | ||
} | ||
}; | ||
objArrKeys.forEach(function (key) { | ||
var arr = objArr[key]; | ||
if (firstFields.indexOf(key) !== -1) { | ||
asyncSerialArray(arr, func, next); | ||
} else { | ||
asyncParallelArray(arr, func, next); | ||
} | ||
var pending = new _promise2['default'](function (resolve, reject) { | ||
var next = function next(errors) { | ||
results.push.apply(results, errors); | ||
total++; | ||
if (total === objArrLength) { | ||
callback(results); | ||
return results.length ? reject({ errors: results, fields: convertFieldsError(results) }) : resolve(); | ||
} | ||
}; | ||
objArrKeys.forEach(function (key) { | ||
var arr = objArr[key]; | ||
if (firstFields.indexOf(key) !== -1) { | ||
asyncSerialArray(arr, func, next); | ||
} else { | ||
asyncParallelArray(arr, func, next); | ||
} | ||
}); | ||
}); | ||
pending['catch'](function (e) { | ||
return e; | ||
}); | ||
return pending; | ||
} | ||
@@ -189,0 +220,0 @@ |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _rule = require('../rule/'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _util = require('../util'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _rule = require('../rule/'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _rule = require('../rule/'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _rule = require('../rule/'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _string = require('./string'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _rule = require('../rule/'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _rule = require('../rule/'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -29,2 +27,5 @@ var _rule = require('../rule/'); | ||
if (validate) { | ||
if (value === '') { | ||
value = undefined; | ||
} | ||
if ((0, _util.isEmptyValue)(value) && !rule.required) { | ||
@@ -31,0 +32,0 @@ return callback(); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _rule = require('../rule/'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _rule = require('../rule/'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _rule = require('../rule/'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _typeof2 = require('babel-runtime/helpers/typeof'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _rule = require('../rule/'); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _rule = require('../rule/'); |
{ | ||
"name": "async-validator", | ||
"version": "1.10.1", | ||
"version": "1.11.0", | ||
"description": "validate form asynchronous", | ||
@@ -47,6 +47,8 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel-plugin-transform-runtime": "^6.23.0", | ||
"babel-preset-env": "^1.7.0", | ||
"coveralls": "^2.13.1", | ||
"jest": "20.x", | ||
"jest": "^23.6.0", | ||
"pre-commit": "1.x", | ||
"rc-tools": "6.x" | ||
"rc-tools": "7.x" | ||
}, | ||
@@ -53,0 +55,0 @@ "pre-commit": [ |
105
README.md
@@ -38,4 +38,8 @@ # async-validator | ||
var descriptor = { | ||
name: {type: "string", required: true} | ||
} | ||
name: { | ||
type: "string", | ||
required: true, | ||
validator: (rule, value) => value === 'muji', | ||
}, | ||
}; | ||
var validator = new schema(descriptor); | ||
@@ -51,2 +55,22 @@ validator.validate({name: "muji"}, (errors, fields) => { | ||
}); | ||
// PROMISE USAGE | ||
validator.validate({ | ||
name: "muji", | ||
asyncValidator: (rule, value) => axios.post('/nameValidator', { name: value }), | ||
}, (errors, fields) => { | ||
if(errors) { | ||
// validation failed, errors is an array of all errors | ||
// fields is an object keyed by field name with an array of | ||
// errors per field | ||
return handleErrors(errors, fields); | ||
} | ||
// validation passed | ||
}) | ||
.then(() => { | ||
// validation passed | ||
}) | ||
.catch(({ errors, fields }) => { | ||
return handleErrors(errors, fields); | ||
}) | ||
``` | ||
@@ -57,3 +81,3 @@ | ||
```javascript | ||
function(source, [options], callback) | ||
function(source, [options], callback): Promise | ||
``` | ||
@@ -65,2 +89,6 @@ | ||
The method will return a Promise object like: | ||
* `then()`,validation passed | ||
* `catch({ errors, fields })`,validation failed, errors is an array of all errors, fields is an object keyed by field name with an array of | ||
### Options | ||
@@ -85,3 +113,3 @@ | ||
* `value`: The value of the source object property being validated. | ||
* `callback`: A callback function to invoke once validation is complete. It expects to be passed an array of `Error` instances to indicate validation failure. | ||
* `callback`: A callback function to invoke once validation is complete. It expects to be passed an array of `Error` instances to indicate validation failure. If the check is synchronous, you can directly return a ` false ` or ` Error ` or ` Error Array `. | ||
* `source`: The source object that was passed to the `validate` method. | ||
@@ -91,3 +119,3 @@ * `options`: Additional options. | ||
The options passed to `validate` are passed on to the validation functions so that you may reference transient data (such as model references) in validation functions. However, some option names are reserved; if you use these properties of the options object they are overwritten. The reserved properties are `messages`, `exception` and `error`. | ||
The options passed to `validate` or `asyncValidate` are passed on to the validation functions so that you may reference transient data (such as model references) in validation functions. However, some option names are reserved; if you use these properties of the options object they are overwritten. The reserved properties are `messages`, `exception` and `error`. | ||
@@ -105,3 +133,3 @@ ```javascript | ||
} | ||
callback(errors); | ||
return errors; | ||
} | ||
@@ -128,3 +156,3 @@ } | ||
// and add a validation error to the errors array if it does | ||
callback(errors); | ||
return errors; | ||
}} | ||
@@ -228,5 +256,7 @@ ] | ||
var validator = new schema(descriptor); | ||
validator.validate({ address: {} }, (errors, fields) => { | ||
// now only errors for street | ||
}); | ||
validator.validate({ address: {} }) | ||
.catch(({ errors, fields }) => { | ||
// now only errors for street and name | ||
}); | ||
``` | ||
@@ -285,5 +315,4 @@ | ||
var source = {name: " user "}; | ||
validator.validate(source, (errors, fields) => { | ||
assert.equal(source.name, "user"); | ||
}); | ||
validator.validate(source) | ||
.then(() => assert.equal(source.name, "user")); | ||
``` | ||
@@ -328,5 +357,5 @@ | ||
### validator | ||
### asyncValidator | ||
you can custom validate function for specified field: | ||
You can customize the asynchronous validation function for the specified field: | ||
@@ -336,3 +365,3 @@ ```js | ||
asyncField:{ | ||
validator(rule,value,callback){ | ||
asyncValidator(rule,value,callback){ | ||
ajax({ | ||
@@ -350,15 +379,45 @@ url:'xx', | ||
promiseField:{ | ||
validator(rule, value){ | ||
return ajax({ | ||
url:'xx', | ||
value:value | ||
}); | ||
} | ||
asyncValidator(rule, value){ | ||
return ajax({ | ||
url:'xx', | ||
value:value | ||
}); | ||
} | ||
} | ||
}; | ||
``` | ||
### validator | ||
you can custom validate function for specified field: | ||
```js | ||
const fields = { | ||
field:{ | ||
validator(rule,value,callback){ | ||
return value === 'test'; | ||
}, | ||
message: 'Value is not equal to "test".', | ||
}, | ||
field2:{ | ||
validator(rule,value,callback){ | ||
return new Error(`'${value} is not equal to "test".'`); | ||
}, | ||
}, | ||
arrField:{ | ||
validator(rule, value){ | ||
return [ | ||
new Error('Message 1'), | ||
new Error('Message 2'), | ||
]; | ||
} | ||
}, | ||
}; | ||
``` | ||
## FAQ | ||
### avoid warning | ||
### How to avoid warning | ||
@@ -365,0 +424,0 @@ ```js |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
112116
2599
434
6