async-validator 源码学习笔记(四):validator
系列文章:
1、async-validator 源码学习(一):文档翻译
2、async-validator 源码学习笔记(二):目录结构
3、async-validator 源码学习笔记(三):rule
源码目录结构如图:
validator 源码分析
validator 与 rule 紧密相连,rule 目录下的文件主要功能是校验 value 和 rule ,然后给 errors 数组中添加 error 。validator 则是把 校验的 value 细分成各种类型,对不同的类型进行不同的 rule 校验组合,便于回调函数 callback 对最终的 errors 数组做最终的处理。
校验流程如下:
1、校验方法结构相同,第一步先判断是否需要进行校验:
- 字段是必须的。
- 字段是非必须的,但 source 对象中的该字段有值且不为空。
2、如果是需要校验的,校验的步骤为:
- 先校验是否为空。
- 校验该字段不为空的 rule。
- 再校验该类型对应的其他的 rule。
3、校验完成之后,最后开始执行回调,用回调函数返回 errors 。
validator 文件夹中的 index.d.ts:
是 validator 目录的统一出口管理。
declare const _default: {
string: import("..").ExecuteValidator;
method: import("..").ExecuteValidator;
number: import("..").ExecuteValidator;
boolean: import("..").ExecuteValidator;
regexp: import("..").ExecuteValidator;
integer: import("..").ExecuteValidator;
float: import("..").ExecuteValidator;
array: import("..").ExecuteValidator;
object: import("..").ExecuteValidator;
enum: import("..").ExecuteValidator;
pattern: import("..").ExecuteValidator;
date: import("..").ExecuteValidator;
url: import("..").ExecuteValidator;
hex: import("..").ExecuteValidator;
email: import("..").ExecuteValidator;
required: import("..").ExecuteValidator;
any: import("..").ExecuteValidator;
};
export default _default;
import("..").ExecuteValidator 限制类型,ExecuteValidator 被定义在 interface.ts 文件内。
// 摘自其中一部分
/**
* Performs validation for any type.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param callback The callback function.
* @param source The source object being validated.
* @param options The validation options.
* @param options.messages The validation messages.
*/
export declare type ExecuteValidator = (
rule: InternalRuleItem,
value: Value,
callback: (error?: string[]) => void,
source: Values,
options: ValidateOption
) => void;
上述的解释翻译为中文:
/*
执行任何类型验证
@param rule 校验的规则
@param value 需要校验字段的当前值
@param callback 回调函数
@param source 需要校验的字段
@param options 校验选项
@param options.message 校验的 messages
*/
any.d.ts
校验任意类型只需要一步,校验不为空即可。
import { ExecuteValidator } from '../interface';
declare const any: ExecuteValidator;
export default any;
array.d.ts
校验数组。
import { ExecuteValidator } from '../interface';
declare const array: ExecuteValidator;
export default array;
校验数组,一般需要两步:1、校验非空数组。2、校验范围。
array?: {
len?: ValidateMessage<[FullField, Range]>;
min?: ValidateMessage<[FullField, Range]>;
max?: ValidateMessage<[FullField, Range]>;
range?: ValidateMessage<[FullField, Range, Range]>;
};
boolean.d.ts
import { ExecuteValidator } from '../interface';
declare const boolean: ExecuteValidator;
export default boolean;
date.d.ts
校验时间。
import { ExecuteValidator } from '../interface';
declare const date: ExecuteValidator;
export default date;
declare type ValidateMessage<T extends any[] = unknown[]> = string | ((...args: T) => string);
date?: {
format?: ValidateMessage;
parse?: ValidateMessage;
invalid?: ValidateMessage;
};
enum.d.ts
校验枚举值。
import { ExecuteValidator } from '../interface';
declare const enumerable: ExecuteValidator;
export default enumerable;
enum?: ValidateMessage<[FullField, EnumString]>;
float.d.ts
校验浮点数。
import { ExecuteValidator } from '../interface';
declare const floatFn: ExecuteValidator;
export default floatFn;
integer.d.ts
校验整数。
import { ExecuteValidator } from '../interface';
declare const integer: ExecuteValidator;
export default integer;
method.d.ts
import { ExecuteValidator } from '../interface';
declare const method: ExecuteValidator;
export default method;
number.d.ts
import { ExecuteValidator } from '../interface';
declare const number: ExecuteValidator;
export default number;
校验数字,一般需要两步:1、校验不为空。2、校验范围。
number?: {
len?: ValidateMessage<[FullField, Range]>;
min?: ValidateMessage<[FullField, Range]>;
max?: ValidateMessage<[FullField, Range]>;
range?: ValidateMessage<[FullField, Range, Range]>;
};
object.d.ts
校验对象,一般需要两步:1、校验不为空。2、校验类型。
import { ExecuteValidator } from '../interface';
declare const object: ExecuteValidator;
export default object;
pattern.d.ts
需要两步。第一步校验不为空,第二步校验 pattern。
import { ExecuteValidator } from '../interface';
declare const pattern: ExecuteValidator;
export default pattern;
regexp.d.ts
校验正则表达式。
import { ExecuteValidator } from '../interface';
declare const regexp: ExecuteValidator;
export default regexp;
type.d.ts
import { ExecuteValidator } from '../interface';
declare const type: ExecuteValidator;
export default type;
async-validator 源码学习笔记(四):validator的更多相关文章
- yii2源码学习笔记(四)
继续了解组件Component.php /** * Returns a value indicating whether a property is defined for this componen ...
- async-validator 源码学习笔记(五):Schema
系列文章: 1.async-validator 源码学习(一):文档翻译 2.async-validator 源码学习笔记(二):目录结构 3.async-validator 源码学习笔记(三):ru ...
- async-validator 源码学习笔记(六):validate 方法
系列文章: 1.async-validator 源码学习(一):文档翻译 2.async-validator 源码学习笔记(二):目录结构 3.async-validator 源码学习笔记(三):ru ...
- async-validator 源码学习笔记(三):rule
系列文章: 1.async-validator 源码学习(一):文档翻译 2.async-validator 源码学习笔记(二):目录结构 rule 主要实现的是校验规则,文件结构为下图: 一.rul ...
- Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点
Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...
- Underscore.js 源码学习笔记(下)
上接 Underscore.js 源码学习笔记(上) === 756 行开始 函数部分. var executeBound = function(sourceFunc, boundFunc, cont ...
- Hadoop源码学习笔记(5) ——回顾DataNode和NameNode的类结构
Hadoop源码学习笔记(5) ——回顾DataNode和NameNode的类结构 之前我们简要的看过了DataNode的main函数以及整个类的大至,现在结合前面我们研究的线程和RPC,则可以进一步 ...
- JDK源码学习笔记——LinkedHashMap
HashMap有一个问题,就是迭代HashMap的顺序并不是HashMap放置的顺序,也就是无序. LinkedHashMap保证了元素迭代的顺序.该迭代顺序可以是插入顺序或者是访问顺序.通过维护一个 ...
- jquery源码学习笔记三:jQuery工厂剖析
jquery源码学习笔记二:jQuery工厂 jquery源码学习笔记一:总体结构 上两篇说过,query的核心是一个jQuery工厂.其代码如下 function( window, noGlobal ...
随机推荐
- 使用grpcui测试gRPC服务
grpcui类似Swagger UI,可以用来测试gRPC服务,使用起来特别简单. 其原理是通过自动发现gRPC服务协议(当然前提是gRPC服务暴露了Protobuf协议),然后启动一个带界面的Web ...
- GIL解释器锁 & 进程池与线程池
今日内容 GIL 全局解释器锁(重要理论) 验证 GIL 的存在及功能 验证 python 多线程是否有用 死锁现象 进程池与线程池(使用频率高) IO模型 详细参考: https://www.bil ...
- HashMap(1.7)源码学习
一. 1.7 和1.8区别 数据结构: 1.7: 数组 + 链表 1.8 : 数组 + 链表 + 红黑树 put: 1.7: 头插法 1.8: 尾插法 hash计算: 1.7 : Objects.ha ...
- 针对Office宏病毒的高级检测
前言 攻击者可能发送带有恶意附件的钓鱼邮件,诱导受害者点击从而获取对方的系统控制权限 期间会借助 Atomic 工具完成攻击复现,再对具体的过程细节进行分析取证,然后深入研究.剖析其行为特征 最后输出 ...
- SpringBoot 自定义内容协商策略 configureContentNegotiation
在自定义的config配置类中,重写configureContentNegotiation方法 @Bean public WebMvcConfigurer webMvcConfigurer(){ re ...
- 轩辕展览-为什么要做VR虚拟展厅设计?
沉浸感,有趣和互动体验VR虚拟展厅设计给客户带来高度的沉浸感和互动体验,给客户一种真实的感觉,让客户更愿意参与,使商家的宣传更加客观. 展示方式多样化 ,增加宣传优势在展示产品或企业时,VR全景可达到 ...
- MySQL之InnoDB存储引擎 - 读书笔记
1. MySQL 的存储引擎 MySQL 数据库的一大特色是有插件式存储引擎概念.日常使用频率最高的两种存储引擎: InnoDB 存储引擎支持事务,其特点是行锁设计.支持外键.非锁定读(默认读取操作不 ...
- c# Winform中如何把图片添加到resources中
我们在Winform项目中中需要插入图片资源,但是新建的项目中找不到Resources文件夹,怎么才能出现呢? 1:双击项目下的Resources.resx,出现视图 2:单击"添加资源&q ...
- Vue el-date-picker 日期组件的使用
一:显示年月 <el-date-picker v-model="selectMonth" type="month" placeholder="选 ...
- CV之各种不熟悉但比较重要的笔记
解析: skip connection 就是一种跳跃式传递.在ResNet中引入了一种叫residual network残差网络结构,其和普通的CNN的区别在于从输入源直接向输出源多连接了一条传递线, ...