使用单个 JSONSchema 验证多个 JSON
validate several JSONs with a single JSONSchema
我想知道是否可以使用单个 JSONSchema (draft-04) 来验证多个 JSONs,例如:
JSON 1:
{
"Credentiales": {
"Name": "123456",
"Password": "word"
},
"Reference": "1"
}
JSON 2:
{
"ConsumerInfo": {
"Reference": "1",
"Consumer": "89",
"FirstName": "Ern",
"LastName": "Torres",
"Address": "White Street 50",
"City": "Ges",
"State": "Santa",
"PhoneNumber": "+12354569874",
"ConfirmedEmailingDate": "2017-02-15 03:10:55"
}
}
感谢您的帮助,对于给您带来的不便,我们深表歉意
是的,你可以。这个想法是使用 oneOf
关键字和 $ref
来重用定义。此 JSON 模式验证其中之一(我没有指定所有 CustomerInfo 属性,但您明白了,所以请填写空白)
--编辑 2018-09-01--
04 草案 JSON 架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "JSON schema of Credentiales and CustomerInfo",
"definitions": {
"Credentiales": {
"type": "object",
"properties": {
"Name": {"type": "string"},
"Password": {"type": "string"}
}
},
"Reference": {
"type": "string"
},
"CustomerInfo": {
"type": "object",
"properties": {
"Reference": {"$ref": "#/definitions/Reference"},
"Consumer": {"type": "string"},
"FirstName": {"type": "string"}
!!!! FILL IN THE BLANKS: ADD THE OTHER PROPERTIES HERE AND REMOVE THIS COMMENT !!!
},
"additionalProperties": {"not": {}}
}
},
"oneOf": [
{
"type": "object",
"properties": {
"Credentiales": {
"$ref": "#/definitions/Credentiales"
},
"Reference": {
"$ref": "#/definitions/Reference"
},
"additionalProperties": {"not": {}}
}
},
{"$ref":"#/definitions/CustomerInfo"}
]
}
--编辑 2018-08-31--
Draft-06 JSON 架构版本:
{
"$schema": "http://json-schema.org/draft-06/schema",
"$id": "http://example.com/root.json",
"title": "JSON schema of Credentiales and CustomerInfo",
"definitions": {
"Credentiales": {
"type": "object",
"properties": {
"Name": {"type": "string"},
"Password": {"type": "string"}
}
},
"Reference": {
"type": "string"
},
"CustomerInfo": {
"type": "object",
"properties": {
"Reference": {"$ref": "#/definitions/Reference"},
"Consumer": {"type": "string"},
"FirstName": {"type": "string"}
!!!! FILL IN THE BLANKS: ADD THE OTHER PROPERTIES HERE AND REMOVE THIS COMMENT !!!
},
"additionalProperties": false
}
},
"oneOf": [
{
"type": "object",
"properties": {
"Credentiales": {
"$ref": "#/definitions/Credentiales"
},
"Reference": {
"$ref": "#/definitions/Reference"
},
"additionalProperties": false
}
},
{"$ref":"#/definitions/CustomerInfo"}
]
}
我想知道是否可以使用单个 JSONSchema (draft-04) 来验证多个 JSONs,例如:
JSON 1:
{
"Credentiales": {
"Name": "123456",
"Password": "word"
},
"Reference": "1"
}
JSON 2:
{
"ConsumerInfo": {
"Reference": "1",
"Consumer": "89",
"FirstName": "Ern",
"LastName": "Torres",
"Address": "White Street 50",
"City": "Ges",
"State": "Santa",
"PhoneNumber": "+12354569874",
"ConfirmedEmailingDate": "2017-02-15 03:10:55"
}
}
感谢您的帮助,对于给您带来的不便,我们深表歉意
是的,你可以。这个想法是使用 oneOf
关键字和 $ref
来重用定义。此 JSON 模式验证其中之一(我没有指定所有 CustomerInfo 属性,但您明白了,所以请填写空白)
--编辑 2018-09-01--
04 草案 JSON 架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "JSON schema of Credentiales and CustomerInfo",
"definitions": {
"Credentiales": {
"type": "object",
"properties": {
"Name": {"type": "string"},
"Password": {"type": "string"}
}
},
"Reference": {
"type": "string"
},
"CustomerInfo": {
"type": "object",
"properties": {
"Reference": {"$ref": "#/definitions/Reference"},
"Consumer": {"type": "string"},
"FirstName": {"type": "string"}
!!!! FILL IN THE BLANKS: ADD THE OTHER PROPERTIES HERE AND REMOVE THIS COMMENT !!!
},
"additionalProperties": {"not": {}}
}
},
"oneOf": [
{
"type": "object",
"properties": {
"Credentiales": {
"$ref": "#/definitions/Credentiales"
},
"Reference": {
"$ref": "#/definitions/Reference"
},
"additionalProperties": {"not": {}}
}
},
{"$ref":"#/definitions/CustomerInfo"}
]
}
--编辑 2018-08-31--
Draft-06 JSON 架构版本:
{
"$schema": "http://json-schema.org/draft-06/schema",
"$id": "http://example.com/root.json",
"title": "JSON schema of Credentiales and CustomerInfo",
"definitions": {
"Credentiales": {
"type": "object",
"properties": {
"Name": {"type": "string"},
"Password": {"type": "string"}
}
},
"Reference": {
"type": "string"
},
"CustomerInfo": {
"type": "object",
"properties": {
"Reference": {"$ref": "#/definitions/Reference"},
"Consumer": {"type": "string"},
"FirstName": {"type": "string"}
!!!! FILL IN THE BLANKS: ADD THE OTHER PROPERTIES HERE AND REMOVE THIS COMMENT !!!
},
"additionalProperties": false
}
},
"oneOf": [
{
"type": "object",
"properties": {
"Credentiales": {
"$ref": "#/definitions/Credentiales"
},
"Reference": {
"$ref": "#/definitions/Reference"
},
"additionalProperties": false
}
},
{"$ref":"#/definitions/CustomerInfo"}
]
}