根据JSON模式验证JSON(在Java中)
我使用的是com.github.fge.jsonschema。在Java中工作 下面是JSON模式根据JSON模式验证JSON(在Java中),java,json,validation,jsonschema,json-schema-validator,Java,Json,Validation,Jsonschema,Json Schema Validator,我使用的是com.github.fge.jsonschema。在Java中工作 下面是JSON模式 "$schema": "http://json-schema.org/draft-04/schema#", "title": "Employee", "description": "employee description", "type": "object", "properties": { "eid": { "de
"$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的类型(即整数或字符串)。
例如:
如果我们只为限定传递了错误的类型,那么它将验证为true(即,它不会验证限定的类型,可能是因为它是嵌套的)
需要对整个JSON执行验证
是否有其他解决方案来验证JSON中的嵌套对象?
提前感谢。您的示例
{
"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"
}
}
你的榜样
{
"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"
}
}
这是你的全部计划吗?它验证了整个事情的正确性吗?看起来唯一可能的问题是根据发布的第一个答案。。。您有
属性>资格
而不是属性>资格
。否则它看起来很好。这是你的全部模式吗?它验证了整个事情的正确性吗?看起来唯一可能的问题是根据发布的第一个答案。。。您有属性>资格
而不是属性>资格
。否则它看起来很好。这是一个无效的模式allOf
仅在模式或子模式对象中使用时才有意义,因为它应用了子模式。我已经检查了我的模式在线JSON模式验证程序,该模式是正确的。我关心的是,限定条件是一个字符串,但当我传递整数时,它仍然将其验证为true,这是错误的。不,您的模式不正确。只需将其粘贴到示例中并根据您的示例进行验证,然后粘贴我的模式并根据您的示例进行验证,并查看我的模式是否正确,因为它将限定标记为字符串。这是一个无效的模式allOf
仅在模式或子模式对象中使用时才有意义,因为它应用了子模式。我已经检查了我的模式在线JSON模式验证程序,该模式是正确的。我关心的是,限定条件是一个字符串,但当我传递整数时,它仍然将其验证为true,这是错误的。不,您的模式不正确。只需将其粘贴到示例中并根据您的示例进行验证,然后粘贴我的模式并根据您的示例进行验证,并查看我的模式是否正确,因为它将限定标记为需要的字符串。