给定这个
JSON对象:
{ "objects": { "foo": { "id": 1,"name": "Foo" },"bar": { "id": 2,"name": "Bar" } } }
这是一个包含子对象的对象,其中每个子对象具有相同的结构 – 它们都是相同的类型.每个子对象都是唯一键控的,因此它的作用类似于命名数组.
我想验证对象属性中的每个对象是否针对JSON Schema引用进行验证.
如果objects属性是一个数组,例如:
{ "objects": [ { "id": 1,"name": "Foo" },{ "id": 2,"name": "Bar" } ] }
我可以使用模式定义验证这一点,例如:
{ "id": "my-schema","required": [ "objects" ],"properties": { "objects": { "type": "array","items": { "type": "object","required": [ "id","name" ],"properties": { "id": { "type": "integer" },"name": { "type": "string" } } } } } }
这是因为类型是数组,这允许项目的验证.
是否可以做类似的事情,但嵌套对象?
谢谢!
解决方法
你可以尝试这样的事情:
{ "id": "my-schema","type": "object","properties": { "objects": { "type": "object","patternProperties": { "[a-z]+": { "type": "object","properties": { "id": { "type": "integer" },"name": { "type": "string" } },"additionalProperties": false,"required": [ "id","name" ] } } } } }