- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
系列文章:
1、async-validator 源码学习(一):文档翻译
2、async-validator 源码学习笔记(二):目录结构
rule 主要实现的是校验规则,文件结构为下图:
其中 index.d.ts 文件:
declare const _default: {
required: import("..").ExecuteRule;
whitespace: import("..").ExecuteRule;
type: import("..").ExecuteRule;
range: import("..").ExecuteRule;
enum: import("..").ExecuteRule;
pattern: import("..").ExecuteRule;
};
export default _default;
是 rule 目录的统一出口管理,主要是给 errors 数组添加对应的 error 。
required.d.ts 文件:
import { ExecuteRule } from '../interface';
declare const required: ExecuteRule;
export default required;
主要作用是校验必填字段的规则。
其中 ExecuteRule 是来自于 interface.d.ts 文件中的
//摘自其中的一部分
export declare type ExecuteRule =(
rule: InternalRuleItem,
value: Value,
source: Values,
errors: string[],
options: ValidateOption,
type?: string
) => void;
/**
* 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.
*/
ExecuteRule 是统一定义的函数类型别名,统一了函数传递参数和返回值的类型。等价于:
declare const required(rule, value, source, errors, options, type)
方法内的参数及其意义如下:
type.d.ts
import { ExecuteRule } from '../interface';
declare const type: ExecuteRule;
export default type;
校验值的类型,可能的类型有:integer、float、array、regexp、object、method、email、number、data、url、hex
range.d.ts
import { ExecuteRule } from '../interface';
declare const range: ExecuteRule;
export default range;
校验是否满足最大最小值合理区间的规则
whitespace.d.ts
import { ExecuteRule } from '../interface';
/**
* Rule for validating whitespace.
*
* @param rule The validation rule.
* @param value The value of the field on the source object.
* @param source The source object being validated.
* @param errors An array of errors that this rule may add
* validation errors to.
* @param options The validation options.
* @param options.messages The validation messages.
*/declare const whitespace: ExecuteRule;
export default whitespace;
校验空白字符的规则
enum.d.ts
import { ExecuteRule } from '../interface';
declare const enumerable: ExecuteRule;
export default enumerable;
校验值是否存在枚举值列表中的规则
pattern.d.ts
import { ExecuteRule } from '../interface';
declare const pattern: ExecuteRule;
export default pattern;
校验正则表达式的规则
interface.d.ts 中定义 rule 单元格式
export interface RuleItem {
type?: RuleType; //类型
required?: boolean; //是否为空
pattern?: RegExp | string; //正则
min?: number; //最小值或长度
max?: number; //最大值或长度
len?: number; //长度
enum?: Array<string | number | boolean | null | undefined>; //校验值是否存在枚举值列表中的规则
whitespace?: boolean; //是否空白
fields?: Record<string, Rule>;//深度监听属性和规则
options?: ValidateOption;//选项
defaultField?: Rule; //校验属性内部值
transform?: (value: Value) => Value; //校验前转换
message?: string | ((a?: string) => string);//信息提示
//异步校验
asyncValidator?: (rule: InternalRuleItem, value: Value, callback: (error?: string | Error) => void, source: Values, options: ValidateOption) => void | Promise<void>;
//同步校验
validator?: (rule: InternalRuleItem, value: Value, callback: (error?: string | Error) => void, source: Values, options: ValidateOption) => SyncValidateResult | void;
}
//Rule 可以是一个对象,也可以是该对象的数组
export declare type Rule = RuleItem | RuleItem[];
rule 是本字段对应的校验规则:
{
field: "name",
fullField: "name",
message: "姓名为必填项",
required: false,
type: "string",
validator: ƒ required$1(rule, value, callback, source, options)
}
value 是本字段的值:如小明
source 是要校验的整个 source 对象:
{
name: '小明',
info: {
age: 17,
}
}
errors 是本次校验将要去添加的 errors 数组,假设之前没有 error,则 errors 为[],如果之前已经存在了一些 error,则格式如下所示:
[
{
message: '年龄超出范围',
field: 'info.age',
}
]
options 是该字段校验时的选项,当 message 属性为默认值时,格式如下:
{
firstFields: true,
messages: {
array: {len: "%s must be exactly %s in length", min: "%s cannot be less than %s in length", max: "%s cannot be greater than %s in length", range: "%s must be between %s and %s in length"},
clone: ƒ clone(),
date: {format: "%s date %s is invalid for format %s", parse: "%s date could not be parsed, %s is invalid ", invalid: "%s date %s is invalid"},
default: "Validation error on field %s",
enum: "%s must be one of %s",
number: {len: "%s must equal %s", min: "%s cannot be less than %s", max: "%s cannot be greater than %s", range: "%s must be between %s and %s"},
pattern: {mismatch: "%s value %s does not match pattern %s"},
required: "%s is required",
string: {len: "%s must be exactly %s characters", min: "%s must be at least %s characters", max: "%s cannot be longer than %s characters", range: "%s must be between %s and %s characters"},
types: {string: "%s is not a %s", method: "%s is not a %s (function)", array: "%s is not an %s", object: "%s is not an %s", number: "%s is not a %s", …},
whitespace: "%s cannot be empty",
}
}
实际项目开发中验证规则 rule 的写法:
const rules ={
//深度校验1
address: {
type: 'object',
required: true,
fields: {
//深度校验street属性
street: { type: 'string', required: true},
city: { type: 'string', required: true},
zip: {
type: 'string',
required: true,
len: 8,
message: 'invalid zip',
},
},
},
//校验 2 数组形式
username: [
{
type: 'string',
required: true,
whitespace: true,
transform(value) {
returnvalue.trim()
},
message: '用户名不能为空格',
//异步校验
asyncValidator: (rule, value) =>{
return new Promise((resolve, reject) =>{
setTimeout(() =>{
if (value != '') {
resolve()
} else{
reject('error')
}
}, 2000)
})
},
},
{
type: 'string',
min: 3,
max: 20,
message: '长度 3- 20 位',
},
],
}
ACO.Visualization项目 本项目演示蚁群算法求解旅行商问题的可视化过程,包括路径上的信息素浓度、蚁群的运动过程等。项目相关的代码:https://github.com/anycad/A
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我需要用Sql数据库制作并包含的PHP票务系统源码用户客户端和管理员。我需要个人 CMS 的这个来源。谢谢你帮助我。 最佳答案 我在不同的情况下使用了 osticket。 这里: http://ost
我的场景:我想在日志文件中写入发生异常的部分代码(例如,发生异常的行前 5 行和行后 5 行 - 或者至少是该方法的所有代码)。 我的想法是用 C# 代码反编译 pdb 文件,并从该反编译文件中找到一
RocketMQ设定了延迟级别可以让消息延迟消费,延迟消息会使用 SCHEDULE_TOPIC_XXXX 这个主题,每个延迟等级对应一个消息队列,并且与普通消息一样,会保存每个消息队列的消费进度
先附上Hystrix源码图 在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和
此篇博客学习的api如标题,分别是: current_url 获取当前页面的url; page_source 获取当前页面的源码; title 获取当前页面的titl
? 1 2
1、前言 作为一个数据库爱好者,自己动手写过简单的sql解析器以及存储引擎,但感觉还是不够过瘾。<<事务处理-概念与技术>>诚然讲的非常透彻,但只能提纲挈领,不能让你
gory"> 目录 运行时信号量机制 semaphore 前言 作用是什么 几个主要的方法 如何实现
自己写的一个评论系统源码分享给大家,包括有表情,还有评论机制。用户名是随机的 针对某一篇文章进行评论 function subcomment() {
一、概述 StringBuilder是一个可变的字符串序列,这个类被设计去兼容StringBuffer类的API,但不保证线程安全性,是StringBuffer单线程情况下的一个替代实现。在可能的情
一、概述 System是用的非常多的一个final类。它不能被实例化。System类提供了标准的输入输出和错误输出流;访问外部定义的属性和环境变量;加载文件和库的方法;以及高效的拷贝数组中一部分元素
在JDK中,String的使用频率和被研究的程度都非常高,所以接下来我只说一些比较重要的内容。 一、String类的概述 String类的声明如下: public final class Str
一、概述 Class的实例代表着正在运行的Java应用程序的类和接口。枚举是一种类,而直接是一种接口。每一个数组也属于一个类,这个类b被反射为具有相同元素类型和维数的所有数组共享的类对象。八大基本树
一、概述 Compiler这个类被用于支持Java到本地代码编译器和相关服务。在设计上,这个类啥也不做,他充当JIT编译器实现的占位符。 放JVM虚拟机首次启动时,他确定系统属性java.comp
一、概述 StringBuffer是一个线程安全的、可变的字符序列,跟String类似,但它能被修改。StringBuffer在多线程环境下可以很安全地被使用,因为它的方法都是通过synchroni
一、概述 Enum是所有Jav中枚举类的基类。详细的介绍在Java语言规范中有说明。 值得注意的是,java.util.EnumSet和java.util.EnumMap是Enum的两个高效实现,
一、概述 此线程指的是执行程序中的线程。 Java虚拟机允许应用程序同时执行多个执行线程。 每个线程都有优先权。 具有较高优先级的线程优先于优先级较低的线程执行。 每个线程可能也可能不会被标记为守
一、抽象类Number 类继承关系 这里面的原子类、BigDecimal后面都会详细介绍。 属性和抽象方法 二、概述 所有的属性,最小-128,最大127,SIZE和BYTES代码比
我是一名优秀的程序员,十分优秀!