当前位置:网站首页>async-validator.js数据校验器
async-validator.js数据校验器
2022-06-28 18:35:00 【彭世瑜】
文档:
安装
npm i async-validator
示例
// demo.mjs
// node(v16.14.0)
// import Schema from 'async-validator';
// fix: 文档给出的引入方式报错
import asyncValidator from 'async-validator';
const Validator = asyncValidator.default;
// 定义校验规则
const rules = {
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 Validator(rules);
let data = {
name: 'Tom', age: 16 };
validator
.validate(data)
.then(() => {
// validation passed or without error message
console.log('validate passed');
})
.catch(({
errors, fields }) => {
console.log(errors, fields);
});
打印出的校验信息
[
{
message: 'name fails', fieldValue: 'Tom', field: 'name' },
{
message: 'too young', fieldValue: 16, field: 'age' }
] {
name: [ {
message: 'name fails', fieldValue: 'Tom', field: 'name' } ],
age: [ {
message: 'too young', fieldValue: 16, field: 'age' } ]
}
rule的属性
type
required
pattern
len
enum
min/max
可以使用的 Type
| 类型 | 描述 |
|---|---|
| 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. |
| Must be of type email. | |
| any | Can be any type. |
边栏推荐
猜你喜欢

324. swing sequencing II

A preliminary study of IO model

亿信华辰:地产企业数字化转型想要把握时代机遇

Qt 中 QObjectCleanupHandler 使用总结

【Unity3D】发射(RayCast)物理射线(Ray)

牛津大学教授Michael Wooldridge:AI社区近40年如何看待神经网络

第2章 处理文件、摄像头和图形用户界面cameo应用

POI Excel转换工具

Yixin Huachen: real estate enterprises want to grasp the opportunity of the times for digital transformation

About Critical Values
随机推荐
PMP怎么补考?补考费用是多少?
leetcode 1423. Maximum Points You Can Obtain from Cards(从牌中能得到的最大点数和)
刷题分析工具
从理论到实践增强STEAM和工程教育
Shell 未知汇总1
use. NETCORE's own background job, which simply simulates producers and consumers' processing of request response data in and out of the queue
东方财富软件股票开户是靠谱的吗?在哪开户安全
Concept and code implementation of heap
CVPR2022 | 浙大、蚂蚁集团提出基于标签关系树的层级残差多粒度分类网络,建模多粒度标签间的层级知识
618 activity season - the arrival of special discounts for hundreds of low code platforms
SMARCA2抗体研究:Abnova SMARCA2 单克隆抗体方案
Unity about oculus quest2 developing 002-ui interaction based on XR interaction Toolkit
Cann media data processing V2, jpegd interface introduction
闭包的理解
Lumiprobe 蛋白质标记研究方案
使用.NetCore自带的后台作业,出入队简单模拟生产者消费者处理请求响应的数据
内存抖动
About Statistical Distributions
Oom out of memory memory overflow
原生实现.NET5.0+ 自定义日志