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官网整理

最新文章

  1. 基于Windows 10平台的PM2.5检测器制作
  2. C# List 的一些操作 (两List元素是否想同,List是否包含在另一个List中)
  3. 一起买beta版PHP单元测试
  4. git 学习笔记6--remote & log
  5. [Nginx][HttpUpstreamModule]翻译负载均衡
  6. BIEE 维表
  7. 使用Topshelf 5步创建Windows 服务 z
  8. zedboard U盘挂载+交叉编译helloworld
  9. OpenJudge 2721 忽略大小写比较字符串大小
  10. leptonica 学习笔记2——pixBackgroundNormSimple
  11. IOS 播放动态Gif图片
  12. python list 切片实验
  13. Dubbo框架应用之(一)--服务体系
  14. Mac平台Clion配置GLFW+GLAD的项目
  15. 搞懂Python的类和对象名称空间
  16. 唯美PS转手绘之SAI篇_百度经验
  17. .net core 配置
  18. Spring获取application.properties
  19. javaweb获取项目路径的方法
  20. rabbitmq direct、fanout、topic 三种Exchange java 代码比较

热门文章

  1. 构建api gateway之 负载均衡
  2. socket模块实现网络编程及struct模块解决黏包问题
  3. Pytest插件pytest-assume多重断言
  4. Spring Cloud 2022.0.1 Spring Cloud Zookeeper4.0
  5. Linux10-rpm和yum
  6. 字符串(str)内置方法补充、列表(list)内置方法、可变类型与不可变类型、队列和栈
  7. WinUI 剪裁发布中的一个小坑
  8. 你想知道的do{...}while(0)的作用,都在这里了
  9. clicknium-划时代的自动化
  10. 物语(monogatari)