我正在使用 com.github.fge.jsonschema 。使用Java。
以下是JSON模式。
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Employee",
"description": "employee description",
"type": "object",
"properties": {
"eid": {
"description": "The unique identifier for a emp",
"type": "integer"
},
"ename": {
"description": "Name of the emp",
"type": "string"
},
"qual":{
"$ref": "#/definitions/qualification"
}
},
"definitions": {
"qualification":
{
"description": "Qualification",
"type": "string"
}
}
}
这是根据架构进行验证的JSON。
{
"eid":1000,
"ename": "mrun",
"qualification": "BE"
}
问题是,如果我们传递了任何错误的数据,它将正确验证eid和ename的类型(即整数或字符串)。 例如:
{
"eid":"Mrun", //should be Integer
"ename": 72831287, //should be String
"qualification": 98372489 //should be String
}
如果仅通过错误的类型进行资格鉴定,则其验证为true(即,它不验证类型的资格可能是因为它是嵌套的)。
需要对整个JSON执行验证。
还有其他解决方案可以验证JSON中的嵌套对象吗?
谢谢。
答案 0 :(得分:0)
您的示例
{
"eid":"Mrun",
"ename": 72831287,
"qualification": 98372489
}
与您的架构不匹配。您的架构需要像
这样的对象{
"eid": "Mrun",
"ename": 72831287,
"qual": {
"qualification": 98372489
}
}
但是,如果您只想重用“限定”定义,则您的架构应类似于
"properties": {
"eid": {
"description": "The unique identifier for a emp",
"type": "integer"
},
"ename": {
"description": "Name of the emp",
"type": "string"
},
"qualification":{
"$ref": "#/definitions/qualification"
}
}