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 了.既然是接口就少不了对输入参数的验证,所以本文主要探 ...
随机推荐
- python3异常打印堆栈信息
import traceback try: a=1/0 except: print(traceback.format_exc())
- 项目与自身毕设对比整理规划6 330 done
3.30 没用呢- - 功夫不负有心人啊啊啊啊啊啊啊啊啊啊啊啊啊啊 成功了- - 但是很可恶 需要把数据返回然后放在那个文件里面才可以 现在就是问题就是返回数据的时候需要把这个文件存成数据接口 fin ...
- P5_认识小程序项目的基本组成结构
项目结构 了解项目的基本组成结构 pages 用来存放所有小程序的页面 utils 用来存放工具性质的模块(例如:格式化时间的自定义模块) app.js 小程序项目的入口文件 app.json 小程序 ...
- 简要介绍WASAPI播放音频的方法
正文 填一下之前挖的坑,这回就说说怎么用WASAPI播放声音吧. 本文完整代码可以在以下链接找到 https://gitcode.net/PeaZomboss/learnaudios 目录是demo/ ...
- K3S 系列文章-5G IoT 网关设备 POD 访问报错 DNS 'i/o timeout'分析与解决
开篇 <K3s 系列文章> <Rancher 系列文章> 问题概述 20220606 5G IoT 网关设备同时安装 K3S Server, 但是 POD 却无法访问互联网地址 ...
- 关于vux-ui框架的scroller组件所踩的坑
这是我在做一个demo的一个上垃加载下拉刷新功能时所遇到的问题,由于伤了好一会脑筋,所以留下这篇笔记以供后续查询: 在上代码前建议在开发项目时不要优先选择vux这个框架,因为有一些常用的功能组件官方已 ...
- ElasticSearch7.6入门
笔记记录 B站狂神说Java的ElasticSearch课程:https://www.bilibili.com/video/BV17a4y1x7zq 一.ElasticSearch概述 官网:http ...
- Python爬虫:原来微博上的视频下载链接在这啊
最近看了一下网页版的微博,觉得那上面的视频不错,想获取它上面的下载链接,于是就写了这篇博文. 1. 几个视频播放平台的下载链接的实现 1. 西瓜视频 西瓜视频这个平台上面的视频下载链接一开始就存在于视 ...
- ThreadLocal最终版本
ThreadLocal工作原理 目录 ThreadLocal工作原理 一.官方文档描述 二.为什么使用ThreadLocal 2.1.案例 三.ThreadLocal和syncronized关键字区别 ...
- gridfs + nginx + mongodb 实现图片服务器
项目预览网址 : http://trans.waibaobao.cn/file/pics 安装:前提安装mongodb 作为文件储存库 1)nginx-gridfs安装 a.安装所用依赖包 yum - ...