使用单个JSONSchema验证多个JSON
我想知道是否可以使用一个JSONSchema(草案-04)来验证几个JSON,例如: JSON 1:使用单个JSONSchema验证多个JSON,json,jsonschema,Json,Jsonschema,我想知道是否可以使用一个JSONSchema(草案-04)来验证几个JSON,例如: JSON 1: { "Credentiales": { "Name": "123456", "Password": "word" }, "Reference": "1" } JSON 2: { "ConsumerInfo": { "Reference": "1", "Consumer": "89", "Fir
{
"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--
草稿-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"}
]
}
欢迎您可以使用相同的JSON模式文件来验证任意多个JSON实例。我不太清楚你在问什么。你能给一个更详细的解释你正在尝试做什么,也许一些代码请?非常感谢你的回答。我需要验证不同操作的几个JSON(REST服务),我想用一个文件JSON模式来验证,这是我的想法。到目前为止,您有什么模式?您是否为每个示例创建了一个模式,并希望将它们组合起来?如果是,请提供这两个模式。您只想使用一个JSON模式的原因是什么?通常,您要为每个要验证的JSON响应类型创建一个。看看你们的例子,你们有非常不同的数据模型,这很正常。非常感谢你们,这对我来说真的很有效,这就是我想要的。尽管我还有其他疑问:1。该文件与草案-06的定义一致,使用草案-04的定义有任何问题吗?2.可以在文档中使用更多的限制,例如,在JSON文件中只能使用模式中提到的元素(以及在XSD中定义了强制和可选元素,并且不能在要分析的xml文件中添加更多的元素)。非常感谢。我添加了一个与draft-04兼容的版本。如果这个答案对你合适,你可以投赞成票并接受答案。对于draft-04兼容性,主要区别在于
additionalProperties:false
替换为additionalProperties:{“not”:{}
。检查draft04中的更改。至于限制,additionalProperties:false
意味着您不能拥有架构中提到的属性以外的其他属性。您还可以使用required
关键字将某些属性设置为强制属性。