+0
日增长
9,066
stars
762
forks
108
watchers
92
prs
261
issues
0
releases
README
async-validator
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![node version][node-image]][node-url]
[![npm download][download-image]][download-url]
[![npm bundle size (minified + gzip)][bundlesize-image]][bundlesize-url]
[npm-image]: https://img.shields.io/npm/v/async-validator.svg?style=flat-square
[npm-url]: https://npmjs.org/package/async-validator
[travis-image]:https://app.travis-ci.com/yiminghe/async-validator.svg?branch=master
[travis-url]: https://app.travis-ci.com/github/yiminghe/async-validator
[coveralls-image]: https://img.shields.io/coveralls/yiminghe/async-validator.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/yiminghe/async-validator?branch=master
[node-image]: https://img.shields.io/badge/node.js-%3E=4.0.0-green.svg?style=flat-square
[node-url]: https://nodejs.org/download/
[download-image]: https://img.shields.io/npm/dm/async-validator.svg?style=flat-square
[download-url]: https://npmjs.org/package/async-validator
[bundlesize-image]: https://img.shields.io/bundlephobia/minzip/async-validator.svg?label=gzip%20size
[bundlesize-url]: https://bundlephobia.com/result?p=async-validator
Validate form asynchronous. A variation of https://github.com/freeformsystems/async-validate
Install
bash
npm i async-validator
Usage
Basic usage involves defining a descriptor, assigning it to a schema and passing the object to be validated and a callback function to the validate method of the schema:
js
import Schema from 'async-validator';
const descriptor = {
name: {
type: 'string',
required: true,
validator: (rule, value) = value === 'muji',
},
age: {
type: 'number',
asyncValidator: (rule, value) = {
return new Promise((resolve, reject) = {
if (value {
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
});
// PROMISE USAGE
validator.validate({ name: 'muji', age: 16 }).then(() = {
// validation passed or without error message
}).catch(({ errors, fields }) = {
return handleErrors(errors, fields);
});
API
Validate
js
function(source, [options], callback): Promise
source: The object to validate (required).
options: An object describing processing options for the validation (optional).
callback: A callback function to invoke when validation completes (optional).
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 errors per field
Options
suppressWarning: Boolean, whether to suppress internal warning about invalid value.
first: Boolean, Invoke callback when the first validation rule generates an error,
no more validation rules are processed.
If youTop issues
#76如何在字段的校验中触发对另一个字段的校验15 comments · 2017-11-24#108请问下数组里面的对象属性怎样做校验10 comments · 2018-06-13#65多类型验证 + 弱类型兼容10 comments · 2017-08-28#73"123" 难道不是数字吗9 comments · 2017-11-13#299建议增加字段间校验8 comments · 2021-11-26#154number 数值的验证有问题吧7 comments · 2019-03-25#121无法校验手机号phone7 comments · 2018-08-22#60校验规则 触发 trigger 可以自定义吗 7 comments · 2017-08-03