async-validator
Advanced tools
Comparing version 1.8.5 to 1.9.0
import _extends from 'babel-runtime/helpers/extends'; | ||
import _typeof from 'babel-runtime/helpers/typeof'; | ||
import { format, complementError, asyncMap, warning, deepMerge } from './util'; | ||
import { format, complementError, asyncMap, warning, deepMerge, convertFieldsError } from './util'; | ||
import validators from './validator/'; | ||
@@ -47,3 +47,3 @@ import { messages as defaultMessages, newMessages } from './messages'; | ||
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 () {}; | ||
@@ -61,7 +61,6 @@ var source = source_; | ||
} | ||
return; | ||
return Promise.resolve(); | ||
} | ||
function complete(results) { | ||
var i = void 0; | ||
var field = void 0; | ||
var errors = []; | ||
@@ -85,7 +84,3 @@ var fields = {}; | ||
} else { | ||
for (i = 0; i < errors.length; i++) { | ||
field = errors[i].field; | ||
fields[field] = fields[field] || []; | ||
fields[field].push(errors[i]); | ||
} | ||
fields = convertFieldsError(errors); | ||
} | ||
@@ -144,3 +139,3 @@ callback(errors, fields); | ||
var errorFields = {}; | ||
asyncMap(series, options, function (data, doIt) { | ||
return asyncMap(series, options, function (data, doIt) { | ||
var rule = data.rule; | ||
@@ -147,0 +142,0 @@ var deep = (rule.type === 'object' || rule.type === 'array') && (_typeof(rule.fields) === 'object' || _typeof(rule.defaultField) === 'object'); |
@@ -20,2 +20,13 @@ import _extends from 'babel-runtime/helpers/extends'; | ||
export 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; | ||
} | ||
export function format() { | ||
@@ -145,16 +156,19 @@ for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
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); | ||
} | ||
return new Promise(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); | ||
} | ||
}); | ||
}); | ||
@@ -161,0 +175,0 @@ } |
# History | ||
---- | ||
## 1.9.0 / 2018-10-10 | ||
- .validate returns promise | ||
## 1.8.0 / 2017-08-16 | ||
@@ -5,0 +9,0 @@ |
@@ -65,3 +65,3 @@ 'use strict'; | ||
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 () {}; | ||
@@ -79,7 +79,6 @@ var source = source_; | ||
} | ||
return; | ||
return Promise.resolve(); | ||
} | ||
function complete(results) { | ||
var i = void 0; | ||
var field = void 0; | ||
var errors = []; | ||
@@ -103,7 +102,3 @@ var fields = {}; | ||
} 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); | ||
} | ||
@@ -162,3 +157,3 @@ callback(errors, fields); | ||
var errorFields = {}; | ||
(0, _util.asyncMap)(series, options, function (data, doIt) { | ||
return (0, _util.asyncMap)(series, options, function (data, doIt) { | ||
var rule = data.rule; | ||
@@ -165,0 +160,0 @@ var deep = (rule.type === 'object' || rule.type === 'array') && ((0, _typeof3['default'])(rule.fields) === 'object' || (0, _typeof3['default'])(rule.defaultField) === 'object'); |
@@ -16,2 +16,3 @@ 'use strict'; | ||
exports.convertFieldsError = convertFieldsError; | ||
exports.format = format; | ||
@@ -43,2 +44,13 @@ exports.isEmptyValue = isEmptyValue; | ||
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() { | ||
@@ -168,16 +180,19 @@ for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
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); | ||
} | ||
return new Promise(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); | ||
} | ||
}); | ||
}); | ||
@@ -184,0 +199,0 @@ } |
{ | ||
"name": "async-validator", | ||
"version": "1.8.5", | ||
"version": "1.9.0", | ||
"description": "validate form asynchronous", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -50,2 +50,19 @@ # async-validator | ||
}); | ||
// PROMISE USAGE | ||
validator.validate({name: "muji"}, (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); | ||
}) | ||
``` | ||
@@ -63,2 +80,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 | ||
@@ -65,0 +86,0 @@ |
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
99110
2391
387