參考文檔: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"
}
}
}
]
}
}
}
}
數組元素可能存在不同類型的取值,這裏對數組元素進行了多種可能性的校驗