参考文档:combine schemas
The keywords used to combine schemas are:
- allOf: Must be valid against all of the subschemas (校验对象要满足所有子schema)
- anyOf: Must be valid against any of the subschemas (校验对象要满足至少一个子schema)
- oneOf: Must be valid against exactly one of the subschemas (校验对象要满足其中一个子schema)
All of these keywords must be set to an array, where each item is a schema.
In addition, there is:
- not: Must not be valid against the given schema
待校验内容
待校验内容:
{
"count": 50
}
allOf
schema示例:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"required": [],
"properties": {
"count": {
"$id": "#/properties/count",
"allOf": [{
"type": "number"
},
{
"minimum": 90
}]
}
}
}
校验结果:
{
"schemaLocation": "#/properties/count",
"pointerToViolation": "#/count",
"causingExceptions": [
{
"schemaLocation": "#/properties/count/allOf/1",
"pointerToViolation": "#/count",
"causingExceptions": [],
"keyword": "minimum",
"message": "50 is not greater or equal to 90"
}
],
"keyword": "allOf",
"message": "#: only 1 subschema matches out of 2",
"validateResult": "FAILED"
}
所有条件满足时,校验才会通过
anyOf
schema示例:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"required": [],
"properties": {
"count": {
"$id": "#/properties/count",
"anyOf": [{
"type": "number"
},
{
"minimum": 20
}]
}
}
}
校验结果:
校验 type 和 mininum,只要有一个满足条件,校验就可以成功
oneOf
schema示例:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"required": [],
"properties": {
"count": {
"$id": "#/properties/count",
"oneOf": [{
"type": "number"
},
{
"minimum": 20
}]
}
}
}
校验结果:
{
"schemaLocation": "#/properties/count",
"pointerToViolation": "#/count",
"causingExceptions": [],
"keyword": "oneOf",
"message": "#: 2 subschemas matched instead of one",
"validateResult": "FAILED"
}
有且仅有一个条件满足时,校验才能通过
not
schema示例:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"required": [],
"properties": {
"count": {
"$id": "#/properties/count",
"not": {
"type": "number"
}
}
}
}
校验结果:
{
"schemaLocation": "#/properties/count",
"pointerToViolation": "#/count",
"causingExceptions": [],
"keyword": "not",
"message": "subject must not be valid against schema {\"type\":\"number\"}",
"validateResult": "FAILED"
}
不满足校验条件时,校验才能通过
复杂示例
待校验内容
{
"ARRAY": [
{
"item": 2
},
{
"item": "true"
}
]
}
schema示例
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"required": [],
"properties": {
"ARRAY": {
"$id": "#/properties/ARRAY",
"type": "array",
"items": {
"$id": "#/properties/ARRAY/items",
"type": "object",
"required": [],
"oneOf": [
{
"properties": {
"item": {
"$id": "#/properties/ARRAY/items/properties/item",
"type": "number",
"minimum": 1
}
}
},
{
"properties": {
"item": {
"$id": "#/properties/ARRAY/items/properties/item",
"type": "string",
"parttern": "true"
}
}
}
]
}
}
}
}
数组元素可能存在不同类型的取值,这里对数组元素进行了多种可能性的校验