yiminghe /async-validator

validate form asynchronous

★ 9,066⑂ 762👁 108⑃ 92● 261▤ 0
TypeScriptMITCreated 2015-03-10
+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 you

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON