# 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 < 18) {
reject('too young'); // reject with error message
} else {
resolve();
}
});
},
},
};
const validator = new Schema(descriptor);
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
});
// 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 your validation involves multiple asynchronous calls (for example, database queries) and you only need the first error use this option.
* `firstFields`: Boolean|String[], Invoke `callback` when the first validation rule of the specified field generates an error,
no more validation rules of the same field are processed. `true` means all fields.
### Rules
Rules may be functions that perform validation.
```js
function(rule, value, callback, source, options)
```
* `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.
* `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. 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.
* `options`: Additional options.
* `options.messages`: The object containing validation error messages, will be deep merged with defaultMessages.
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`.
```js
import Schema from 'async-validator';
const descriptor = {
name(rule, value, callback, source, options) {
const errors = [];
if (!/^[a-z0-9]+$/.test(value)) {
errors.push(new Error(
util.format('%s must be lowercase alphanumeric characters', rule.field),
));
}
return errors;
},
};
const validator = new Schema(descriptor);
validator.validate({ name: 'Firstname' }, (errors, fields) => {
if (errors) {
return handleErrors(errors, fields);
}
// validation passed
});
```
It 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:
```js
const descriptor = {
email: [
{ type: 'string', required: true, pattern: Schema.pattern.email },
{
validator(rule, value, callback, source, options) {
const errors = [];
// test if email address already exists in a database
// and add a validation error to the errors array if it does
return errors;
},
},
],
};
```
#### Type
Indicates the `type` of validator to use. Recognised type values are:
* `string`: Must be of type `string`. `This is the default type.`
* `number`: Must be of type `number`.
* `boolean`: Must be of type `boolean`.
* `method`: Must be of type `function`.
* `regexp`: Must be an instance of `RegExp` or a string that does not generate an exception when creating a new `RegExp`.
* `integer`: Must be of type `number` and an integer.
* `float`: Must be of type `number` and a floating point number.
* `array`: Must be an array as determined by `Array.isArray`.
* `object`: Must be of type `object` and not `Array.isArray`.
* `enum`: Value must exist in the `enum`.
* `date`: Value must be valid as determined by `Date`
* `url`: Must be of type `url`.
* `hex`: Must be of type `hex`.
* `email`: Must be of type `email`.
* `any`: Can be any type.
#### Required
The `required` rule property indicates that the field must exist on the source object being validated.
#### Pattern
The `pattern` rule property indicates a regular expression that the value must match to pass validation.
#### Range
A 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`.
#### Length
To 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`.
If the `len` property is combined with the `min` and `max` range properties, `len` takes precedence.
#### Enumerable
> Since version 3.0.0 if you want to validate the values `0` or `false` inside `enum` types, you have to include them explicitly.
To 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:
```js
const descriptor = {
role: { type: 'enum', enum: ['admin', 'user
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
微信头像生成小程序,基于skyline+vue3+t该项目已适配微信小程序隐私政策,截图、获取头像等诸多新的接口 如何使用: 将代码导入到HBuilderX开发工具,可无需修改任何代码,运行并编译到微信开发者工具 skyline需要使用微信开发工具nightly最新版,基础库选择3.0.1以上 在微信开发者工具修改 AppID为自己在小程序平台申请的 AppID 预览时取消勾选开发者工具详情里的【不校验合法域名】 发布时请先在小程序后台开发者设置里添加信任的域名:https://pic.rmb.bdstatic.com 上述域名是因为默认旗子挂件图片来源是该链接前缀 测试无误后提交审核前,隐私条款里选择 1、获取昵称头像能力, 2、图片存储相册能力, 3、相册选择照片能力 图片安全检测可自自行开发,或者默认不要改动 参数自定义都在common/config.ts里,根据备注设置 skyline目前仅支持激励视频流量主,仅需配置广告id即可
资源推荐
资源详情
资源评论
收起资源包目录
2023微信头像生成小程序源码/基于skyline+vue3+t (1053个子文件)
CHANGELOG 48B
index.css 34KB
icon.css 6KB
index.html 672B
index.js 12KB
index.js 12KB
index.js 9KB
index.js 9KB
index.js 8KB
index.js 8KB
index.js 5KB
index.js 5KB
index.js 4KB
index.js 4KB
cupertino-route.js 4KB
index.js 4KB
index.js 4KB
ru.js 3KB
sk.js 3KB
cs.js 3KB
sr-cyrl.js 3KB
uk.js 3KB
bn-bd.js 3KB
index.js 3KB
index.js 3KB
fi.js 3KB
sr.js 2KB
index.js 2KB
index.js 2KB
is.js 2KB
bn.js 2KB
common.js 2KB
bo.js 2KB
lt.js 2KB
pl.js 2KB
scale-route.js 2KB
he.js 2KB
et.js 2KB
ku.js 2KB
br.js 2KB
hu.js 2KB
scale-route.js 2KB
ta.js 2KB
ar.js 2KB
ml.js 2KB
tzm.js 2KB
popup-route.js 2KB
ne.js 2KB
zh-cn.js 2KB
de-at.js 2KB
de.js 2KB
de-ch.js 2KB
popup-s-route.js 2KB
zh.js 2KB
kn.js 2KB
zh-tw.js 2KB
index.js 2KB
index.js 2KB
gu.js 2KB
bg.js 2KB
te.js 2KB
pa-in.js 2KB
index.js 2KB
index.js 2KB
th.js 2KB
ka.js 2KB
my.js 2KB
utils.js 2KB
utils.js 2KB
dv.js 2KB
index.js 2KB
half-route.js 2KB
hi.js 2KB
hafl-screen-route.js 2KB
index.js 2KB
index.js 2KB
lo.js 1KB
hr.js 1KB
km.js 1KB
si.js 1KB
opacity-route.js 1KB
opacity-route.js 1KB
custom-route.js 1KB
ug-cn.js 1KB
ar-iq.js 1KB
index.js 1KB
index.js 1KB
am.js 1KB
mn.js 1KB
el.js 1KB
ar-sa.js 1KB
index.js 1KB
index.js 1KB
ar-tn.js 1KB
ar-dz.js 1KB
hy-am.js 1KB
ar-ma.js 1KB
ar-kw.js 1KB
sv-fi.js 1KB
kk.js 1KB
共 1053 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
资源评论
破碎的天堂鸟
- 粉丝: 887
- 资源: 622
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功