ajv参数验证
1.验证枚举类型
var schema = {
"properties": {
"data": {
"type": "object",
"required": ["code", "status", "message", "data", "token"],
"properties": {
"code": {
"type": "number"
},
"status": {
"type": "number",
"enum": [0, 1] //枚举
},
"message": {
"type": "string"
},
"data": {
"type": "array", //数组
items:{
type:"number"
}
},
"token": {
"type": "string"
}
}
}
}
};
2.验证email(format、minLength、minimum、default)
const Ajv = require('ajv'); let schema = {
type: 'object',
required: ['username', 'email', 'password'],
properties: {
username: {
type: 'string',
minLength: 4
},
email: {
type: 'string',
format: 'email'
},
password: {
type: 'string',
minLength: 6
},
age: {
type: 'integer',
minimum: 0
},
sex: {
enum: ['boy', 'girl', 'secret'],
default: 'secret'
},
}
}; let ajv = new Ajv();
let validate = ajv.compile(schema); let valid = validate(data);
if (!valid) console.log(validate.errors);
3.if/then/else
{
type: "object",
if: {properties: {foo: {minimum: 10}}},
then: {required: ["bar"]},
else: {required: ["baz"]}
}
{
type: "integer",
minimum: 1,
maximum: 1000,
if: {minimum: 100},
then: {multipleOf: 100},
else: {
if: {minimum: 10},
then": {multipleOf: 10}
}
}
4.regex:
const schema = {
type: "object",
properties: {
foo: {type: "string", regexp: "/foo/i"},
bar: {type: "string", regexp: {pattern: "bar", flags: "i"}},
},
}
5. Keywords for arrays
const schema = {
type: "array",
uniqueItemProperties: ["id", "name"],
} const validData = [{id: 1}, {id: 2}, {id: 3}] const invalidData1 = [
{id: 1},
{id: 1}, // duplicate "id"
{id: 3},
] const invalidData2 = [
{id: 1, name: "taco"},
{id: 2, name: "taco"}, // duplicate "name"
{id: 3, name: "salsa"},
]
6.验证日期格式
onst schema = {
type: "object",
dynamicDefaults: {
ts: "datetime",
r: {func: "randomint", args: {max: 100}},
id: {func: "seq", args: {name: "id"}},
},
properties: {
ts: {
type: "string",
format: "date-time",
},
r: {
type: "integer",
minimum: 0,
exclusiveMaximum: 100,
},
id: {
type: "integer",
minimum: 0,
},
},
} const data = {}
ajv.validate(data) // true
7. JSON data type
==Type can be: number, integer, string, boolean, array, object or null==
1.复合类型: {type: ["number", "string"]}
2. nullable:This keyword can be used to allow null value in addition to the defined type.
{
"type": "string",
"nullable": true
},
{
"type": ["string", "null"]
}
3.Keywords for numbers:
maximum / minimum and exclusiveMaximum / exclusiveMinimum schema: {type: "number", not: {minimum: 3}}
4.Keywords for strings
maxLength / minLength
schema: {type: "string", maxLength: 5}
{type: "string", minLength: 2}
3.pattern: 内容为“正则”
schema: {type: "string", pattern: "[abc]+", enum: ["foo", "bar"]}
5.format
The value of the keyword should be a string. The data to be valid should match the format with this name. Ajv does not include any formats, they can be added with ajv-formats (opens new window) plugin. Example schema: {type: "string", format: "ipv4"}
6.验证array
1. {
"type": "array",
items:{
type:'number'
},
"minItems": 3,
"maxItems": 5,
"uniqueItems": true
} 2.{
type: "array",
items: [{type: "integer"}, {type: "string"}]
}
本文来自ajv官网整理
ajv参数验证的更多相关文章
- C# 中参数验证方式的演变
一般在写方法的时候,第一步就是进行参数验证,这也体现了编码者的细心和缜密,但是在很多时候这个过程很枯燥和乏味,比如在拿到一个API设计文档的时候,通常会规定类型参数是否允许为空,如果是字符可能有长度限 ...
- Java和C#下的参数验证
参数的输入和验证问题是开发时经常遇到的,一般的验证方法如下: public bool Register(string name, int age) { if (string.IsNullOrEmpty ...
- DUBBO参数验证
public class ValidationParameter implements Serializable { private static final long seria ...
- ASP.NET WebAPI 11 参数验证
在绑定完Action的所有参数后,WebAPI并不会马上执行该方法,而要对参数进行验证,以保证输入的合法性. ModelState 在ApiController中一个ModelState属性用来获取参 ...
- C# 中参数验证方式
C# 中参数验证方式 一般在写方法的时候,第一步就是进行参数验证,这也体现了编码者的细心和缜密,但是在很多时候这个过程很枯燥和乏味,比如在拿到一个API设计文档的时候,通常会规定类型参数是否允许为空, ...
- 自动化CodeReview - ASP.NET Core请求参数验证
自动化CodeReview系列目录 自动化CodeReview - ASP.NET Core依赖注入 自动化CodeReview - ASP.NET Core请求参数验证 参数验证实现 在做服务端开发 ...
- Kotlin + Spring Boot 请求参数验证
编写 Web 应用程序的时候,经常要做的事就是要对前端传回的数据进行简单的验证,比如是否非空.字符长度是否满足要求,邮箱格式是否正确等等.在 Spring Boot 中,可以使用 Bean Valid ...
- 参数验证 @Validated 和 @Valid 的区别
来源:blog.csdn.net/qq_27680317/article/details/79970590 整编:Java技术栈(公众号ID:javastack) Spring Validation验 ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 参数验证
示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 参数验证 参数验证功能是基于JSR303实现的,用户只需标识JSR303标准的验证Annotation,并通过声明filter来 ...
- ASP.NET Web API 2 之参数验证
Ø 前言 目前 C# 比较流行使用 ASP.NET Web API 来承载 Web 接口,提供与客户端之间的数据交互,现在的版本已经是 2.0 了.既然是接口就少不了对输入参数的验证,所以本文主要探 ...
随机推荐
- Windows离线安装.net Framework3.5
写在前面 本文主要介绍在Windows离线情况下安装.NETFramework3.5运行环境 使用场景 在日常开发C#程序中,经常会遇到开发过程中无任何问题,但是安装到目标电脑是会无法打开的,如下图 ...
- 11月15日内容总结——软件开发架构、网络编程介绍和OSI七层协议介绍
目录 一.软件开发架构 1.什么是软件开发架构 2.软件开发架构 架构方式一:c/s架构 架构方式二:b/s架构 架构优劣势 二.架构总结 三.网络编程前戏 1.什么是网络编程 2.学习网络编程的目的 ...
- 2023.1.16[模板]BSGS/exBSGS
2023.1.16 [模板]BSGS/exBSGS 全称Boy Step Girl Step 给定一个质数 p,以及一个整数 a,一个整数 b,现在要求你计算一个最小的非负整数 l, 满足\(a^x ...
- JavaScript 函数所能传递的最大参数
取决于实现,取决于浏览器和操作系统标准没有规定(65535一般是有的) 来源:https://stackoverflow.com/questions/22747068/is-there-a-max-n ...
- spring中Utils工具类注入问题
使用工具类的时候,我们想在static修饰的方法中,通过注入来调用其他方法,这里就存在问题. 第一:普通工具类是不在spring的管理下,spring不会依赖注入 第二:即便使用@Autowired完 ...
- MQ 消息队列 比较
为什么需要消息队列 削峰 业务系统在超高并发场景中,由于后端服务来不及同步处理过多.过快的请求,可能导致请求堵塞,严重时可能由于高负荷拖垮Web服务器. 为了能支持最高峰流量,我们通常采取短平快的方式 ...
- 安卓逆向 ARM基础篇
1.ARM 与 Andorid 的关系 android 的操作系统是 LINUX 内核 LINux又是ARM 2.ARM汇编规范 3.ARM指令格式 ARM常用指令开始 1.ARM 的跳转指令 PC ...
- Java 优化:读取配置文件 "万能方式" 跨平台,动态获取文件的绝对路径
Java 优化:读取配置文件 "万能方式" 跨平台,动态获取文件的绝对路径 每博一文案 往事不会像烟雾似的飘散,将永远像铅一般沉重地浇铸在心灵的深处. 不过,日常生活的纷繁不会让人 ...
- 手机访问wamp及wampserver64设置开放权限后局域网无法连接
服务器配置:在手机上WampServer电脑端网页的效果! 1.本地虚拟主机下的 Apache 目录中的bin 目录中的 config 配置文件 http.config文件使用代码编辑器. 2.将ht ...
- 基于C++的OpenGL 05 之坐标系统
1. 引言 本文基于C++语言,描述OpenGL的坐标系统 前置知识可参考: 基于C++的OpenGL 04 之变换 - 当时明月在曾照彩云归 - 博客园 (cnblogs.com) 笔者这里不过多描 ...