object


{ "type": "object" }
{ "key": "value" }
{ 1: "123" }
"123"

properties

定义各种键和他们的值类型
{ "type": "object", "properties": { "name": "string", "age": "number" } }
{ "name": "busyhe" "age": 123 }
{ "name": "busyhe" }
{}
{ "name": "busyhe", "age": 123, "gender": "男" }
{ "name": 12 }

required

存放必要属性列表
{ "type": "object", "properties": { "name": "string", "age": "number" }, "required": ["name"] }
{ "name": "busyhe", "gender": "男" }
{ "age": 123 }

maxProperties

限制属性值得最大数量,参数为非负整数

minProperties

限制属性值得最小数量,参数为非负整数
{ "type": "object", "minProperties": 2, "maxProperties": 3 }
{ "name": "busyhe", "age": 123 }
{ "name": "busyhe", "age": 123, "gender": "男" }
{ "name": "busyhe" }
{ "name": "busyhe", "age": 123, "gender": "男", "height": 183 }

additionalProperties

控制是否允许 properties 列出的其他的属性,默认允许其他属性,如果为对象,则该对象是将用于验证未在中列出的任何其他属性的架构properties。
{ "type": "object", "properties": { "name": "string", "age": "number" }, "additionalProperties": false }
{ "name": "busyhe", "age": 123 }
{ "name": "busyhe", "age": 123, "gender": "男", "height": 183 }
{ "type": "object", "properties": { "name": "string", "age": "number" }, "additionalProperties": { "type": "string" } }
{ "name": "busyhe", "age": 123, "gender": "男" }

propertyNames

如果待校验JSON对象中的每个一级key都能通过该关键字指定的JSON Schema的校验,那么才认为待校验的JSON对象通过校验。
{ "propertyNames": { "type": "string" } }
eg:
{ "type": "object", "propertyNames": { "pattern": "^[A-Za-z_][A-Za-z0-9_]*$" } }
{ "_asd_001": "123" }
{ "_asd 001": "123" }

dependencies

确定依赖关系
  • 属性依赖,标识一个属性对另一个属性的依赖
    • { "type": "object", "properties": { "age": { "type": "number" }, "name": { "type": "string" }, "gender": "string" }, "dependencies": { "name": ["gender"] } } // success { "age": 123, "name": "123", "gender": "women" } // error 缺少gender { "age": 123, "name": "123" }
  • 模式依赖
    • { "type": "object", "properties": { "age": { "type": "number" }, "name": { "type": "string" } }, "dependencies": { "name": { "properties": { "gender": { "type": "string" } }, "required": ["gender"] } } } // success { "age": 123, "name": "123", "gender": "women" }

patternProperties:

"patternProperties": { "^a": { "type": "number" }, "^b": { "type": "string" } }
{ "abc": 123, "bc": "123" }

array


Items

该关键字的值是一个有效的JSON Schema或者一组有效的JSON Schema
{ "type": "array", "items": { "type": "number" } }
[1, 2, 3, 4, 5]
[1, 2, "3", 4, 5]

additionalItems

关键字是否允许超出所定义的项目,false则是不允许在数组中添加其他项
{ "type": "array", "items": [ { "type": "number" }, { "type": "string" }, { "type": "string", "enum": ["Street", "Avenue", "Boulevard"] }, { "type": "string", "enum": ["NW", "NE", "SW", "SE"] } ], "additionalItems": false } --- // success [1600, "Pennsylvania", "Avenue", "NW"] // error [1600, "Pennsylvania", "Avenue", "NW", "Washington"]
{ "type": "array", "items": [ { "type": "number" }, { "type": "string" }, { "type": "string", "enum": ["Street", "Avenue", "Boulevard"] }, { "type": "string", "enum": ["NW", "NE", "SW", "SE"] } ], "additionalItems": { "type": "string" } } --- // success [1600, "Pennsylvania", "Avenue", "NW", "Washington"] // error [1600, "Pennsylvania", "Avenue", "NW", 20500]

contains

针对数组中的一个或者多个验证
{ "type": "array", "contains": { "type": "number" } } --- // success [1, 2, 3] [1, "2", 3] // error ["1", "2", "3"]

minItems

限制数组最小长度,每个关键字必须为非负数

maxItems

限制数组最大长度,每个关键字必须为非负数

uniqueItems

确保数组中的每一项都是唯一的
{ "type": "array", "uniqueItems": true }
[1, 2, 3, 4, 5]
[]
[1, 2, 3, 3, 4]

string

minLength

字符串最小长度。

maxLength

字符串最大长度。

pattern

限制一个字符串转换为特定的正则表达式
{ "type": "string", "pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$" }
"555-1212"
"(888)555-1212 ext. 532"

format

关键字允许对某些类型的字符串值通常用于基本的语义验证。
  • "date-time" :2018-11-13T20:20:39+00:00。
  • "time" :20:20:39+00:00
  • "date" :2018-11-13。
  • "email" : 互联网电子邮箱
  • "idn-email" :Internet电子邮箱地址的国际化形式
  • "hostname":互联网主机名
  • "idn-hostname":国际化internet主机名
  • "uri"
  • "uri-reference"
  • "iri"
  • "iri-reference"
  • "uri-template"
  • "json-pointer"
  • "relative-json-pointer"

boolean


true false

number / integer


integer

该integer类型用于整数。
{ "type": "integer" }
42
-1
3.1123
integer:类型用于整数,在javascript中 1.0 也是被允许的,在python则否, 为避免这种情况,使用 multipleOf 关键字可在所有的模式上实现相同的行为
{ "type": "number", "multipleOf": 1.0 }

number

可用于任何数字类型,整数或浮点数
{ "type": "number" }
42
-1
3.14
"42"

multipleOf

关键字可以将数字限制为给定数字的倍数 。可以将其设置为任何正数
{ "type" : "number", "multipleOf" : 10 }
0
10
20
-20
23
2

maximum

表示取值范围应该小于或等于maximum

exclusiveMaximum

小于不等于

minimum

表示取值范围应该大于或等于minimum

exclusiveMinimum

大于不等于
x >= `minimum` x > `exclusiveMinimum` x <= `maximum` x < `exclusiveMaximum`

null


表示缺失值
null
badge