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参数验证的更多相关文章

  1. C# 中参数验证方式的演变

    一般在写方法的时候,第一步就是进行参数验证,这也体现了编码者的细心和缜密,但是在很多时候这个过程很枯燥和乏味,比如在拿到一个API设计文档的时候,通常会规定类型参数是否允许为空,如果是字符可能有长度限 ...

  2. Java和C#下的参数验证

    参数的输入和验证问题是开发时经常遇到的,一般的验证方法如下: public bool Register(string name, int age) { if (string.IsNullOrEmpty ...

  3. DUBBO参数验证

    public class ValidationParameter implements Serializable {           private static final long seria ...

  4. ASP.NET WebAPI 11 参数验证

    在绑定完Action的所有参数后,WebAPI并不会马上执行该方法,而要对参数进行验证,以保证输入的合法性. ModelState 在ApiController中一个ModelState属性用来获取参 ...

  5. C# 中参数验证方式

    C# 中参数验证方式 一般在写方法的时候,第一步就是进行参数验证,这也体现了编码者的细心和缜密,但是在很多时候这个过程很枯燥和乏味,比如在拿到一个API设计文档的时候,通常会规定类型参数是否允许为空, ...

  6. 自动化CodeReview - ASP.NET Core请求参数验证

    自动化CodeReview系列目录 自动化CodeReview - ASP.NET Core依赖注入 自动化CodeReview - ASP.NET Core请求参数验证 参数验证实现 在做服务端开发 ...

  7. Kotlin + Spring Boot 请求参数验证

    编写 Web 应用程序的时候,经常要做的事就是要对前端传回的数据进行简单的验证,比如是否非空.字符长度是否满足要求,邮箱格式是否正确等等.在 Spring Boot 中,可以使用 Bean Valid ...

  8. 参数验证 @Validated 和 @Valid 的区别

    来源:blog.csdn.net/qq_27680317/article/details/79970590 整编:Java技术栈(公众号ID:javastack) Spring Validation验 ...

  9. Dubbo -- 系统学习 笔记 -- 示例 -- 参数验证

    示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 参数验证 参数验证功能是基于JSR303实现的,用户只需标识JSR303标准的验证Annotation,并通过声明filter来 ...

  10. ASP.NET Web API 2 之参数验证

    Ø  前言 目前 C# 比较流行使用 ASP.NET Web API 来承载 Web 接口,提供与客户端之间的数据交互,现在的版本已经是 2.0 了.既然是接口就少不了对输入参数的验证,所以本文主要探 ...

随机推荐

  1. 手把手教你用LOTO虚拟示波器搭建测试系统整机

    虚拟示波器如果用于个人的研发调试工作,主要能体现出它的小巧便携以及功能强大.而它的另一个巨大优势,可集成性可定制性高,则是在我们做项目中搭建测试系统的时候才能更好的体现出来. 通常测试系统要求长时间工 ...

  2. 分布式配置nacos搭建踩坑指南(下)

    上一篇介绍了在配置nacos中的碰到的坑,这一篇介绍一下如何正确进行nacos的环境搭建和配置,所以本文分为两部分,第一部分为环境搭建,介绍如何安装和运行.第二部分为alibaba Sprint Bo ...

  3. 重学SpringBoot. step4 Redis的应用

    Redis的应用 Redis支持的七种数据类型:字符串.散列.列表(链表).集合.有序集合.基数和地理位置,具体用Java怎么操作其实可以直接看redisTemplate的源代码. Redis引出来的 ...

  4. ctfshow_web入门 文件上传

    文件上传 还是是一边学习一边做笔记的文章,由于是学习,所以会显得啰嗦 还请各位师傅担待~ 如果有不正确的地方,请师傅们斧正谢谢师傅们 web150 火狐关闭js的插件一旦开启,就没法点击上传按钮了. ...

  5. JZOJ 3736. 【NOI2014模拟7.11】数学题

    \(\text{Problem}\) 给出向量 \(\boldsymbol a = (x1,y1), \boldsymbol b = (x2,y2)\) 求 \(|\lambda_1\boldsymb ...

  6. JZOJ 5350. 【NOIP2017提高A组模拟9.7】陶陶摘苹果

    题目 分析 很神奇的事情又发生了!! 很容易想到设 \(f_{i,j}\) 表示考虑前 \(i\) 个区间,已选 \(j\) 个区间且必选第 \(i\) 时能覆盖到的最多苹果数 转移 \(O(n)\) ...

  7. drag拖拽相关

    实际开发中会遇到一些和拖拽有关的问题 : 1.移动端不支持鼠标拖拽功能, 2.拖拽时会选中页面中的其他文字,解决办法: <div onSelectStart="return false ...

  8. 获取微信小程序列表渲染 index

    微信小程序列表渲染 index(索引值)通过 wx:for-index="index" 来获取: <view class="item" wx:for=&q ...

  9. 基于docker的spark分布式与单线程、多线程wordcount的对比实验

    1. 分布式环境搭建 1.1 基于docker的spark配置文件 docker-compose.yml version: '2' services: spark: image: docker.io/ ...

  10. Linux操作命令(九)1.comm命令 2.diff命令 3.patch命令

    1.comm 命令 比较文本文件的内容 comm 命令将逐行比较已经排序的两个文件.显示结果包括 3 列:第 1 列为只在第一个文件中找到的行,第 2 列为只在第二个文件中找到的行,第 3 列为两个文 ...