{"_id":"@rc-component/async-validator","_rev":"4538842","name":"@rc-component/async-validator","description":"Validate form asynchronous","dist-tags":{"latest":"5.1.0"},"maintainers":[{"name":"afc163","email":""},{"name":"chenshuai2144","email":""},{"name":"madccc","email":""},{"name":"peachscript","email":""},{"name":"zombiej","email":""}],"time":{"modified":"2026-04-09T15:06:20.000Z","created":"2023-10-16T07:48:09.934Z","5.1.0":"2026-01-08T06:53:57.339Z","5.0.4":"2024-05-14T06:57:24.548Z","5.0.3":"2024-05-08T10:05:34.078Z","5.0.2":"2024-05-06T03:47:17.344Z","5.0.1":"2024-04-16T06:57:45.887Z","5.0.0":"2023-10-16T07:48:09.934Z"},"users":{},"repository":{"type":"git","url":"git+ssh://git@github.com/react-component/async-validator.git"},"versions":{"5.1.0":{"name":"@rc-component/async-validator","version":"5.1.0","description":"Validate form asynchronous","keywords":["async-validator","form"],"homepage":"http://github.com/react-component/async-validator","bugs":{"url":"http://github.com/react-component/async-validator/issues"},"repository":{"type":"git","url":"git+ssh://git@github.com/react-component/async-validator.git"},"license":"MIT","main":"./lib/index","module":"./es/index","types":"./lib/index.d.ts","scripts":{"compile":"father build","docs:build":"dumi build","docs:deploy":"gh-pages -d .docs-dist","gh-pages":"npm run docs:build && npm run docs:deploy","lint":"eslint src --ext .tsx,.ts,.jsx,.js","now-build":"npm run docs:build","prepublishOnly":"npm run compile && np --yolo --no-publish","prettier":"prettier . --write","postpublish":"npm run gh-pages","start":"dumi dev","test":"rc-test","coverage":"rc-test --coverage","tsc":"tsc --noEmit"},"pre-commit":["lint-staged"],"lint-staged":{"*.{tsx,js,jsx,ts,json}":["prettier --write"]},"dependencies":{"@babel/runtime":"^7.24.4"},"devDependencies":{"@rc-component/father-plugin":"^1.0.2","@types/jest":"^29.5.12","@types/react":"18.x","@types/react-dom":"18.x","@umijs/fabric":"^4.0.1","dumi":"^2.2.17","eslint":"^8.0.0","father":"^4.4.0","gh-pages":"^6.1.1","lint-staged":"^15.2.2","np":"^10.0.3","prettier":"^3.2.5","rc-test":"^7.0.15","react":"^18.2.0","react-dom":"^18.2.0","typescript":"5.x"},"engines":{"node":">=14.x"},"publishConfig":{"access":"public"},"gitHead":"b2b321766055ee9a1257f629a434906625a14557","_id":"@rc-component/async-validator@5.1.0","_nodeVersion":"22.15.1","_npmVersion":"11.7.0","dist":{"shasum":"e81f31e676d9cadc71e4310bbf1749c7a5882291","size":22796,"noattachment":false,"key":"/@rc-component/async-validator/-/@rc-component/async-validator-5.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@rc-component/async-validator/download/@rc-component/async-validator-5.1.0.tgz"},"_npmUser":{"name":"zombiej","email":"smith3816@gmail.com"},"directories":{},"maintainers":[{"name":"afc163","email":""},{"name":"chenshuai2144","email":""},{"name":"madccc","email":""},{"name":"peachscript","email":""},{"name":"zombiej","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/async-validator_5.1.0_1767855237197_0.6934274533238898"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2026-01-08T06:53:57.339Z","publish_time":1767855237339,"_source_registry_name":"default","_cnpm_publish_time":1767855237339},"5.0.4":{"name":"@rc-component/async-validator","version":"5.0.4","description":"Validate form asynchronous","keywords":["async-validator","form"],"homepage":"http://github.com/react-component/async-validator","bugs":{"url":"http://github.com/react-component/async-validator/issues"},"repository":{"type":"git","url":"git+ssh://git@github.com/react-component/async-validator.git"},"license":"MIT","main":"./lib/index","module":"./es/index","types":"./lib/index.d.ts","scripts":{"compile":"father build","docs:build":"dumi build","docs:deploy":"gh-pages -d .docs-dist","gh-pages":"npm run docs:build && npm run docs:deploy","lint":"eslint src --ext .tsx,.ts,.jsx,.js","now-build":"npm run docs:build","prepublishOnly":"npm run compile && np --yolo --no-publish","prettier":"prettier . --write","postpublish":"npm run gh-pages","start":"dumi dev","test":"rc-test","coverage":"rc-test --coverage","tsc":"tsc --noEmit"},"pre-commit":["lint-staged"],"lint-staged":{"*.{tsx,js,jsx,ts,json}":["prettier --write"]},"dependencies":{"@babel/runtime":"^7.24.4"},"devDependencies":{"@rc-component/father-plugin":"^1.0.2","@types/jest":"^29.5.12","@types/react":"18.x","@types/react-dom":"18.x","@umijs/fabric":"^4.0.1","dumi":"^2.2.17","eslint":"^8.0.0","father":"^4.4.0","gh-pages":"^6.1.1","lint-staged":"^15.2.2","np":"^10.0.3","prettier":"^3.2.5","rc-test":"^7.0.15","react":"^18.2.0","react-dom":"^18.2.0","typescript":"5.x"},"engines":{"node":">=14.x"},"publishConfig":{"access":"public"},"_id":"@rc-component/async-validator@5.0.4","gitHead":"a3921fbdf767ef3f097e46f025881f50d6b5386f","_nodeVersion":"20.11.1","_npmVersion":"10.2.4","dist":{"shasum":"5291ad92f00a14b6766fc81735c234277f83e948","size":22531,"noattachment":false,"key":"/@rc-component/async-validator/-/@rc-component/async-validator-5.0.4.tgz","tarball":"http://registry.cnpm.dingdandao.com/@rc-component/async-validator/download/@rc-component/async-validator-5.0.4.tgz"},"_npmUser":{"name":"afc163","email":"afc163@gmail.com"},"directories":{},"maintainers":[{"name":"afc163","email":""},{"name":"chenshuai2144","email":""},{"name":"madccc","email":""},{"name":"peachscript","email":""},{"name":"zombiej","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/async-validator_5.0.4_1715669844380_0.12419958097693584"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2024-05-14T06:57:24.548Z","publish_time":1715669844548,"_source_registry_name":"default","_cnpm_publish_time":1715669844548},"5.0.3":{"name":"@rc-component/async-validator","version":"5.0.3","description":"Validate form asynchronous","keywords":["async-validator","form"],"homepage":"http://github.com/react-component/async-validator","bugs":{"url":"http://github.com/react-component/async-validator/issues"},"repository":{"type":"git","url":"git+ssh://git@github.com/react-component/async-validator.git"},"license":"MIT","main":"./lib/index","module":"./es/index","types":"./lib/index.d.ts","scripts":{"compile":"father build","docs:build":"dumi build","docs:deploy":"gh-pages -d .docs-dist","gh-pages":"npm run docs:build && npm run docs:deploy","lint":"eslint src --ext .tsx,.ts,.jsx,.js","now-build":"npm run docs:build","prepublishOnly":"npm run compile && np --yolo --no-publish","prettier":"prettier . --write","postpublish":"npm run gh-pages","start":"dumi dev","test":"rc-test","coverage":"rc-test --coverage","tsc":"tsc --noEmit"},"pre-commit":["lint-staged"],"lint-staged":{"*.{tsx,js,jsx,ts,json}":["prettier --write"]},"dependencies":{"@babel/runtime":"^7.24.4"},"devDependencies":{"@rc-component/father-plugin":"^1.0.2","@types/jest":"^29.5.12","@types/react":"18.x","@types/react-dom":"18.x","@umijs/fabric":"^4.0.1","dumi":"^2.2.17","eslint":"^8.0.0","father":"^4.4.0","gh-pages":"^6.1.1","lint-staged":"^15.2.2","np":"^10.0.3","prettier":"^3.2.5","rc-test":"^7.0.15","react":"^18.2.0","react-dom":"^18.2.0","typescript":"5.x"},"engines":{"node":">=14.x"},"publishConfig":{"access":"public"},"_id":"@rc-component/async-validator@5.0.3","gitHead":"17ad4116b9950ddd3ef22f1ad63b9c21bd595691","_nodeVersion":"18.19.1","_npmVersion":"10.2.4","dist":{"shasum":"4a31e02171e30a9f4f60cf95d9fa8c4c26bf8b59","size":22417,"noattachment":false,"key":"/@rc-component/async-validator/-/@rc-component/async-validator-5.0.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/@rc-component/async-validator/download/@rc-component/async-validator-5.0.3.tgz"},"_npmUser":{"name":"zombiej","email":"smith3816@gmail.com"},"directories":{},"maintainers":[{"name":"afc163","email":""},{"name":"chenshuai2144","email":""},{"name":"madccc","email":""},{"name":"peachscript","email":""},{"name":"zombiej","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/async-validator_5.0.3_1715162733894_0.7521690316091028"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2024-05-08T10:05:34.078Z","publish_time":1715162734078,"_source_registry_name":"default","_cnpm_publish_time":1715162734078},"5.0.2":{"name":"@rc-component/async-validator","version":"5.0.2","description":"Validate form asynchronous","keywords":["async-validator","form"],"homepage":"http://github.com/react-component/async-validator","bugs":{"url":"http://github.com/react-component/async-validator/issues"},"repository":{"type":"git","url":"git+ssh://git@github.com/react-component/async-validator.git"},"license":"MIT","main":"./lib/index","module":"./es/index","types":"./lib/index.d.ts","scripts":{"compile":"father build","docs:build":"dumi build","docs:deploy":"gh-pages -d .docs-dist","gh-pages":"npm run docs:build && npm run docs:deploy","lint":"eslint src --ext .tsx,.ts,.jsx,.js","now-build":"npm run docs:build","prepublishOnly":"npm run compile && np --yolo --no-publish","prettier":"prettier . --write","postpublish":"npm run gh-pages","start":"dumi dev","test":"rc-test","coverage":"rc-test --coverage","tsc":"tsc --noEmit"},"pre-commit":["lint-staged"],"lint-staged":{"*.{tsx,js,jsx,ts,json}":["prettier --write"]},"dependencies":{"@babel/runtime":"^7.24.4"},"devDependencies":{"@rc-component/father-plugin":"^1.0.2","@types/jest":"^29.5.12","@types/react":"18.x","@types/react-dom":"18.x","@umijs/fabric":"^4.0.1","dumi":"^2.2.17","eslint":"^8.0.0","father":"^4.4.0","gh-pages":"^6.1.1","lint-staged":"^15.2.2","np":"^10.0.3","prettier":"^3.2.5","rc-test":"^7.0.15","react":"^18.2.0","react-dom":"^18.2.0","typescript":"5.x"},"engines":{"node":">=14.x"},"publishConfig":{"access":"public"},"_id":"@rc-component/async-validator@5.0.2","gitHead":"8dbf489f4a751e65670718120e923f65547c2b50","_nodeVersion":"20.11.1","_npmVersion":"10.2.4","dist":{"shasum":"721584df8787991dc70e79cbacd634b9f594b0e3","size":22562,"noattachment":false,"key":"/@rc-component/async-validator/-/@rc-component/async-validator-5.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/@rc-component/async-validator/download/@rc-component/async-validator-5.0.2.tgz"},"_npmUser":{"name":"afc163","email":"afc163@gmail.com"},"directories":{},"maintainers":[{"name":"afc163","email":""},{"name":"chenshuai2144","email":""},{"name":"madccc","email":""},{"name":"peachscript","email":""},{"name":"zombiej","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/async-validator_5.0.2_1714967237101_0.6841426550751983"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2024-05-06T03:47:17.344Z","publish_time":1714967237344,"_source_registry_name":"default","_cnpm_publish_time":1714967237344},"5.0.1":{"name":"@rc-component/async-validator","version":"5.0.1","description":"Validate form asynchronous","keywords":["async-validator","form"],"homepage":"http://github.com/react-component/async-validator","bugs":{"url":"http://github.com/react-component/async-validator/issues"},"repository":{"type":"git","url":"git+ssh://git@github.com/react-component/async-validator.git"},"license":"MIT","main":"./lib/index","module":"./es/index","types":"./lib/index.d.ts","scripts":{"compile":"father build","docs:build":"dumi build","docs:deploy":"gh-pages -d .docs-dist","gh-pages":"npm run docs:build && npm run docs:deploy","lint":"eslint src --ext .tsx,.ts,.jsx,.js","now-build":"npm run docs:build","prepublishOnly":"npm run compile && np --yolo --no-publish","prettier":"prettier . --write","postpublish":"npm run gh-pages","start":"dumi dev","test":"rc-test","coverage":"rc-test --coverage","tsc":"tsc --noEmit"},"pre-commit":["lint-staged"],"lint-staged":{"*.{tsx,js,jsx,ts,json}":["prettier --write"]},"dependencies":{"@babel/runtime":"^7.24.4"},"devDependencies":{"@rc-component/father-plugin":"^1.0.2","@types/jest":"^29.5.12","@types/react":"18.x","@types/react-dom":"18.x","@umijs/fabric":"^4.0.1","dumi":"^2.2.17","eslint":"^8.0.0","father":"^4.4.0","gh-pages":"^6.1.1","lint-staged":"^15.2.2","np":"^10.0.3","prettier":"^3.2.5","rc-test":"^7.0.15","react":"^18.2.0","react-dom":"^18.2.0","typescript":"5.x"},"engines":{"node":">=14.x"},"publishConfig":{"access":"public"},"_id":"@rc-component/async-validator@5.0.1","gitHead":"94abdf50c3e49a1c9bf529cf6d74e7ca3eea8876","_nodeVersion":"20.11.1","_npmVersion":"10.2.4","dist":{"shasum":"c5cb4f51604d42f930d559e8ada8d29b045cd8d1","size":22575,"noattachment":false,"key":"/@rc-component/async-validator/-/@rc-component/async-validator-5.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/@rc-component/async-validator/download/@rc-component/async-validator-5.0.1.tgz"},"_npmUser":{"name":"afc163","email":"afc163@gmail.com"},"directories":{},"maintainers":[{"name":"afc163","email":""},{"name":"chenshuai2144","email":""},{"name":"madccc","email":""},{"name":"peachscript","email":""},{"name":"zombiej","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/async-validator_5.0.1_1713250665691_0.7081380578323164"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2024-04-16T06:57:45.887Z","publish_time":1713250665887,"_source_registry_name":"default","_cnpm_publish_time":1713250665887},"5.0.0":{"name":"@rc-component/async-validator","version":"5.0.0","description":"Validate form asynchronous","keywords":["async-validator","form"],"homepage":"http://github.com/react-component/async-validator","bugs":{"url":"http://github.com/react-component/async-validator/issues"},"repository":{"type":"git","url":"git+ssh://git@github.com/react-component/async-validator.git"},"license":"MIT","main":"./lib/index","module":"./es/index","types":"./lib/index.d.ts","scripts":{"compile":"father build","docs:build":"dumi build","docs:deploy":"gh-pages -d docs-dist","gh-pages":"npm run docs:build && father doc deploy","lint":"eslint src --ext .tsx,.ts,.jsx,.js","now-build":"npm run docs:build","prepublishOnly":"npm run compile && np --yolo --no-publish","prettier":"prettier . --write","postpublish":"npm run gh-pages","start":"dumi dev","test":"rc-test","coverage":"rc-test --coverage","tsc":"tsc --noEmit"},"pre-commit":["lint-staged"],"lint-staged":{"*.{tsx,js,jsx,ts,json}":["prettier --write"]},"dependencies":{"@babel/runtime":"^7.23.1"},"devDependencies":{"@rc-component/father-plugin":"^1.0.2","@types/jest":"^29.5.5","@types/react":"18.x","@types/react-dom":"18.x","@umijs/fabric":"^4.0.1","dumi":"^2.1.10","eslint":"^8.51.0","father":"^4.0.0","gh-pages":"^6.0.0","lint-staged":"^15.0.1","np":"^8.0.0","prettier":"^2.8.8","rc-test":"^7.0.15","react":"^18.2.0","react-dom":"^18.2.0","typescript":"5.x"},"engines":{"node":">=14.x"},"publishConfig":{"access":"public"},"gitHead":"a37fe28a524fc7a75f2cd699b778f08678e94ff8","_id":"@rc-component/async-validator@5.0.0","_nodeVersion":"16.19.1","_npmVersion":"9.5.0","dist":{"shasum":"def9b5eea1cd8cee9f2ca9ce91ee49b282ad37c4","size":22426,"noattachment":false,"key":"/@rc-component/async-validator/-/@rc-component/async-validator-5.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@rc-component/async-validator/download/@rc-component/async-validator-5.0.0.tgz"},"_npmUser":{"name":"afc163","email":"afc163@gmail.com"},"directories":{},"maintainers":[{"name":"afc163","email":""},{"name":"chenshuai2144","email":""},{"name":"madccc","email":""},{"name":"peachscript","email":""},{"name":"zombiej","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/async-validator_5.0.0_1697442489719_0.28552707221099793"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2023-10-16T07:48:09.934Z","publish_time":1697442489934,"_source_registry_name":"default","_cnpm_publish_time":1697442489934}},"readme":"# @rc-component/async-validator\n\n[![NPM version][npm-image]][npm-url]\n[![npm download][download-image]][download-url]\n[![build status][github-actions-image]][github-actions-url]\n[![Codecov][codecov-image]][codecov-url]\n[![dumi][dumi-image]][dumi-url]\n\n[npm-image]: http://img.shields.io/npm/v/@rc-component/async-validator.svg?style=flat-square\n[npm-url]: http://npmjs.org/package/@rc-component/async-validator\n[travis-image]: https://img.shields.io/travis/react-component/async-validator/master?style=flat-square\n[travis-url]: https://travis-ci.com/react-component/async-validator\n[github-actions-image]: https://github.com/react-component/async-validator/actions/workflows/ci.yml/badge.svg\n[github-actions-url]: https://github.com/react-component/async-validator/actions/workflows/ci.yml\n[codecov-image]: https://img.shields.io/codecov/c/github/react-component/async-validator/master.svg?style=flat-square\n[codecov-url]: https://app.codecov.io/gh/react-component/async-validator\n[david-url]: https://david-dm.org/react-component/async-validator\n[david-image]: https://david-dm.org/react-component/async-validator/status.svg?style=flat-square\n[david-dev-url]: https://david-dm.org/react-component/async-validator?type=dev\n[david-dev-image]: https://david-dm.org/react-component/async-validator/dev-status.svg?style=flat-square\n[download-image]: https://img.shields.io/npm/dm/@rc-component/async-validator.svg?style=flat-square\n[download-url]: https://npmjs.org/package/@rc-component/async-validator\n[bundlephobia-url]: https://bundlephobia.com/package/@rc-component/async-validator\n[bundlephobia-image]: https://badgen.net/bundlephobia/minzip/@rc-component/async-validator\n[dumi-url]: https://github.com/umijs/dumi\n[dumi-image]: https://img.shields.io/badge/docs%20by-dumi-blue?style=flat-square\n\nValidate form asynchronous. A variation of https://github.com/freeformsystems/async-validate\n\n## Install\n\n```bash\nnpm i @rc-component/async-validator\n```\n\n## Usage\n\nBasic 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:\n\n```js\nimport Schema from '@rc-component/async-validator';\nconst descriptor = {\n  name: {\n    type: 'string',\n    required: true,\n    validator: (rule, value) => value === 'muji',\n  },\n  age: {\n    type: 'number',\n    asyncValidator: (rule, value) => {\n      return new Promise((resolve, reject) => {\n        if (value < 18) {\n          reject('too young'); // reject with error message\n        } else {\n          resolve();\n        }\n      });\n    },\n  },\n};\nconst validator = new Schema(descriptor);\nvalidator.validate({ name: 'muji' }, (errors, fields) => {\n  if (errors) {\n    // validation failed, errors is an array of all errors\n    // fields is an object keyed by field name with an array of\n    // errors per field\n    return handleErrors(errors, fields);\n  }\n  // validation passed\n});\n\n// PROMISE USAGE\nvalidator\n  .validate({ name: 'muji', age: 16 })\n  .then(() => {\n    // validation passed or without error message\n  })\n  .catch(({ errors, fields }) => {\n    return handleErrors(errors, fields);\n  });\n```\n\n## API\n\n### Validate\n\n```js\nfunction(source, [options], callback): Promise\n```\n\n- `source`: The object to validate (required).\n- `options`: An object describing processing options for the validation (optional).\n- `callback`: A callback function to invoke when validation completes (optional).\n\nThe method will return a Promise object like:\n\n- `then()`，validation passed\n- `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\n\n### Options\n\n- `suppressWarning`: Boolean, whether to suppress internal warning about invalid value.\n\n- `first`: Boolean, Invoke `callback` when the first validation rule generates an error,\n  no more validation rules are processed.\n  If your validation involves multiple asynchronous calls (for example, database queries) and you only need the first error use this option.\n\n- `firstFields`: Boolean|String[], Invoke `callback` when the first validation rule of the specified field generates an error,\n  no more validation rules of the same field are processed. `true` means all fields.\n\n### Rules\n\nRules may be functions that perform validation.\n\n```js\nfunction(rule, value, callback, source, options)\n```\n\n- `rule`: The validation rule in the source descriptor that corresponds to the field name being validated. It is always assigned a `field` property with the name of the field being validated.\n- `value`: The value of the source object property being validated.\n- `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`.\n- `source`: The source object that was passed to the `validate` method.\n- `options`: Additional options.\n- `options.messages`: The object containing validation error messages, will be deep merged with defaultMessages.\n\nThe 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`.\n\n```js\nimport Schema from '@rc-component/async-validator';\nconst descriptor = {\n  name(rule, value, callback, source, options) {\n    const errors = [];\n    if (!/^[a-z0-9]+$/.test(value)) {\n      errors.push(\n        new Error(util.format('%s must be lowercase alphanumeric characters', rule.field)),\n      );\n    }\n    return errors;\n  },\n};\nconst validator = new Schema(descriptor);\nvalidator.validate({ name: 'Firstname' }, (errors, fields) => {\n  if (errors) {\n    return handleErrors(errors, fields);\n  }\n  // validation passed\n});\n```\n\nIt is often useful to test against multiple validation rules for a single field, to do so make the rule an array of objects, for example:\n\n```js\nconst descriptor = {\n  email: [\n    { type: 'string', required: true, pattern: Schema.pattern.email },\n    {\n      validator(rule, value, callback, source, options) {\n        const errors = [];\n        // test if email address already exists in a database\n        // and add a validation error to the errors array if it does\n        return errors;\n      },\n    },\n  ],\n};\n```\n\n#### Type\n\nIndicates the `type` of validator to use. Recognised type values are:\n\n- `string`: Must be of type `string`. `This is the default type.`\n- `number`: Must be of type `number`.\n- `boolean`: Must be of type `boolean`.\n- `method`: Must be of type `function`.\n- `regexp`: Must be an instance of `RegExp` or a string that does not generate an exception when creating a new `RegExp`.\n- `integer`: Must be of type `number` and an integer.\n- `float`: Must be of type `number` and a floating point number.\n- `array`: Must be an array as determined by `Array.isArray`.\n- `object`: Must be of type `object` and not `Array.isArray`.\n- `enum`: Value must exist in the `enum`.\n- `date`: Value must be valid as determined by `Date`\n- `url`: Must be of type `url`.\n- `hex`: Must be of type `hex`.\n- `email`: Must be of type `email`.\n- `tel`: Must be of type `tel`.\n- `any`: Can be any type.\n\n#### Required\n\nThe `required` rule property indicates that the field must exist on the source object being validated.\n\n#### Pattern\n\nThe `pattern` rule property indicates a regular expression that the value must match to pass validation.\n\n#### Range\n\nA range is defined using the `min` and `max` properties. For `string` and `array` types comparison is performed against the `length`, for `number` types the number must not be less than `min` nor greater than `max`.\n\n#### Length\n\nTo validate an exact length of a field specify the `len` property. For `string` and `array` types comparison is performed on the `length` property, for the `number` type this property indicates an exact match for the `number`, ie, it may only be strictly equal to `len`.\n\nIf the `len` property is combined with the `min` and `max` range properties, `len` takes precedence.\n\n#### Enumerable\n\n> Since version 3.0.0 if you want to validate the values `0` or `false` inside `enum` types, you have to include them explicitly.\n\nTo validate a value from a list of possible values use the `enum` type with a `enum` property listing the valid values for the field, for example:\n\n```js\nconst descriptor = {\n  role: { type: 'enum', enum: ['admin', 'user', 'guest'] },\n};\n```\n\n#### Whitespace\n\nIt is typical to treat required fields that only contain whitespace as errors. To add an additional test for a string that consists solely of whitespace add a `whitespace` property to a rule with a value of `true`. The rule must be a `string` type.\n\nYou may wish to sanitize user input instead of testing for whitespace, see [transform](#transform) for an example that would allow you to strip whitespace.\n\n#### Deep Rules\n\nIf you need to validate deep object properties you may do so for validation rules that are of the `object` or `array` type by assigning nested rules to a `fields` property of the rule.\n\n```js\nconst descriptor = {\n  address: {\n    type: 'object',\n    required: true,\n    fields: {\n      street: { type: 'string', required: true },\n      city: { type: 'string', required: true },\n      zip: { type: 'string', required: true, len: 8, message: 'invalid zip' },\n    },\n  },\n  name: { type: 'string', required: true },\n};\nconst validator = new Schema(descriptor);\nvalidator.validate({ address: {} }, (errors, fields) => {\n  // errors for address.street, address.city, address.zip\n});\n```\n\nNote that if you do not specify the `required` property on the parent rule it is perfectly valid for the field not to be declared on the source object and the deep validation rules will not be executed as there is nothing to validate against.\n\nDeep rule validation creates a schema for the nested rules so you can also specify the `options` passed to the `schema.validate()` method.\n\n```js\nconst descriptor = {\n  address: {\n    type: 'object',\n    required: true,\n    options: { first: true },\n    fields: {\n      street: { type: 'string', required: true },\n      city: { type: 'string', required: true },\n      zip: { type: 'string', required: true, len: 8, message: 'invalid zip' },\n    },\n  },\n  name: { type: 'string', required: true },\n};\nconst validator = new Schema(descriptor);\n\nvalidator.validate({ address: {} }).catch(({ errors, fields }) => {\n  // now only errors for street and name\n});\n```\n\nThe parent rule is also validated so if you have a set of rules such as:\n\n```js\nconst descriptor = {\n  roles: {\n    type: 'array',\n    required: true,\n    len: 3,\n    fields: {\n      0: { type: 'string', required: true },\n      1: { type: 'string', required: true },\n      2: { type: 'string', required: true },\n    },\n  },\n};\n```\n\nAnd supply a source object of `{ roles: ['admin', 'user'] }` then two errors will be created. One for the array length mismatch and one for the missing required array entry at index 2.\n\n#### defaultField\n\nThe `defaultField` property can be used with the `array` or `object` type for validating all values of the container.\nIt may be an `object` or `array` containing validation rules. For example:\n\n```js\nconst descriptor = {\n  urls: {\n    type: 'array',\n    required: true,\n    defaultField: { type: 'url' },\n  },\n};\n```\n\nNote that `defaultField` is expanded to `fields`, see [deep rules](#deep-rules).\n\n#### Transform\n\nSometimes it is necessary to transform a value before validation, possibly to coerce the value or to sanitize it in some way. To do this add a `transform` function to the validation rule. The property is transformed prior to validation and returned as promise result or callback result when pass validation.\n\n```js\nimport Schema from '@rc-component/async-validator';\nconst descriptor = {\n  name: {\n    type: 'string',\n    required: true,\n    pattern: /^[a-z]+$/,\n    transform(value) {\n      return value.trim();\n    },\n  },\n};\nconst validator = new Schema(descriptor);\nconst source = { name: ' user  ' };\n\nvalidator.validate(source)\n  .then((data) => assert.equal(data.name, 'user'));\n\nvalidator.validate(source,(errors, data)=>{\n  assert.equal(data.name, 'user'));\n});\n```\n\nWithout the `transform` function validation would fail due to the pattern not matching as the input contains leading and trailing whitespace, but by adding the transform function validation passes and the field value is sanitized at the same time.\n\n#### Messages\n\nDepending upon your application requirements, you may need i18n support or you may prefer different validation error messages.\n\nThe easiest way to achieve this is to assign a `message` to a rule:\n\n```js\n{ name: { type: 'string', required: true, message: 'Name is required' } }\n```\n\nMessage can be any type, such as jsx format.\n\n```js\n{ name: { type: 'string', required: true, message: '<b>Name is required</b>' } }\n```\n\nMessage can also be a function, e.g. if you use vue-i18n:\n\n```js\n{ name: { type: 'string', required: true, message: () => this.$t( 'name is required' ) } }\n```\n\nPotentially you may require the same schema validation rules for different languages, in which case duplicating the schema rules for each language does not make sense.\n\nIn this scenario you could just provide your own messages for the language and assign it to the schema:\n\n```js\nimport Schema from '@rc-component/async-validator';\nconst cn = {\n  required: '%s 必填',\n};\nconst descriptor = { name: { type: 'string', required: true } };\nconst validator = new Schema(descriptor);\n// deep merge with defaultMessages\nvalidator.messages(cn);\n...\n```\n\nIf you are defining your own validation functions it is better practice to assign the message strings to a messages object and then access the messages via the `options.messages` property within the validation function.\n\n#### asyncValidator\n\nYou can customize the asynchronous validation function for the specified field:\n\n```js\nconst fields = {\n  asyncField: {\n    asyncValidator(rule, value, callback) {\n      ajax({\n        url: 'xx',\n        value: value,\n      }).then(\n        function (data) {\n          callback();\n        },\n        function (error) {\n          callback(new Error(error));\n        },\n      );\n    },\n  },\n\n  promiseField: {\n    asyncValidator(rule, value) {\n      return ajax({\n        url: 'xx',\n        value: value,\n      });\n    },\n  },\n};\n```\n\n#### validator\n\nYou can custom validate function for specified field:\n\n```js\nconst fields = {\n  field: {\n    validator(rule, value, callback) {\n      return value === 'test';\n    },\n    message: 'Value is not equal to \"test\".',\n  },\n\n  field2: {\n    validator(rule, value, callback) {\n      return new Error(`${value} is not equal to 'test'.`);\n    },\n  },\n\n  arrField: {\n    validator(rule, value) {\n      return [new Error('Message 1'), new Error('Message 2')];\n    },\n  },\n};\n```\n\n## FAQ\n\n### How to avoid global warning\n\n```js\nimport Schema from '@rc-component/async-validator';\nSchema.warning = function () {};\n```\n\nor\n\n```js\nglobalThis.ASYNC_VALIDATOR_NO_WARNING = 1;\n```\n\n### How to check if it is `true`\n\nUse `enum` type passing `true` as option.\n\n```js\n{\n  type: 'enum',\n  enum: [true],\n  message: '',\n}\n```\n\n## Test Case\n\n```bash\nnpm test\n```\n\n## Coverage\n\n```bash\nnpm run coverage\n```\n\nOpen coverage/ dir\n\n## License\n\nEverything is [MIT](https://en.wikipedia.org/wiki/MIT_License).\n","_attachments":{},"homepage":"http://github.com/react-component/async-validator","bugs":{"url":"http://github.com/react-component/async-validator/issues"},"license":"MIT"}