是否可以使用 json-schema 规范进行业务规则验证?
我正在尝试使用 https://json-schema.org/ 来验证多个服务中的 json 结构。规则之一是对象必须存在于文档中的某处。本质上它是一个查找值。例如给出以下
#json document
{
"books": [
{
"id": 1,
"title": "Book1",
"author": 2
}
],
"authors": [
{
"id": 2,
"name": "Bo"
}
]
}
#schema
{
"$schema" : "http://json-schema.org/draft-07/schema#",
"type" : "object",
"properties" : {
"books" : {
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"id": {
"type": "integer"
},
"title": {
"type": "string"
},
"author": {
"type": "integer"
}
}
}
},
"authors" : {
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
}
}
}
}
}
在规范中有没有办法说“#/books/author”必须存在于
authors
数组中?还是我想对规范本身做很多事情,而这应该使用模式验证器之外的自定义脚本来完成?
据我所知,这不是 json-schema 规范支持的东西,我只是想验证一下。
开箱即用的 JSON Schema 标准不支持它。正如您所提到的,您需要在自己的脚本/代码中执行此操作。