Given this JSON object:
给定这个JSON对象:
{
"objects": {
"foo": {
"id": 1,
"name": "Foo"
},
"bar": {
"id": 2,
"name": "Bar"
}
}
}
This is an object containing sub objects where each sub object has the same structure - they're all the same type. Each sub-object is keyed uniquely, so it acts like a named array.
这是一个包含子对象的对象,其中每个子对象具有相同的结构 - 它们都是相同的类型。每个子对象都是唯一键控的,因此它的作用类似于命名数组。
I want to validate that each object within the objects
property validates against a JSON Schema reference.
我想验证对象属性中的每个对象是否针对JSON Schema引用进行验证。
If the objects
property was an array, such as:
如果objects属性是一个数组,例如:
{
"objects": [
{
"id": 1,
"name": "Foo"
},
{
"id": 2,
"name": "Bar"
}
]
}
I could validate this with a schema definition such as:
我可以使用模式定义验证这一点,例如:
{
"id": "my-schema",
"required": [
"objects"
],
"properties": {
"objects": {
"type": "array",
"items": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
}
}
}
}
}
This is achieved because the type
is array
, and this permits the validation of items
.
这是因为类型是数组,这允许项目的验证。
Is it possible to do something similar, but with nested objects?
是否可以做类似的事情,但嵌套对象?
Thanks!
谢谢!
2 个解决方案
#1
3
You can try something like this:
你可以尝试这样的事情:
{
"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"
]
}
}
}
}
}
#2
0
The above answer works for restricting the object property names to lower-case letters. If you do not need to restrict the property names, then this is simpler:
上面的答案适用于将对象属性名称限制为小写字母。如果您不需要限制属性名称,那么这更简单:
{
"id": "my-schema",
"type": "object",
"properties": {
"objects": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string }
}
},
"required": [
"id",
"name"
]
}
}
}
}
I omitted the inner "additionalProperties": false
from the above answer because I find that using that keyword causes more problems than it solves, but it is a valid use of the keyword if you want validation to fail on the inner objects if they have properties other than "name" and "id".
我从上面的答案中省略了内部的“additionalProperties”:false,因为我发现使用该关键字会导致比它解决的问题更多的问题,但如果你希望验证在内部对象上失败(如果它们具有属性),则它是对关键字的有效使用除了“name”和“id”之外。
#1
3
You can try something like this:
你可以尝试这样的事情:
{
"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"
]
}
}
}
}
}
#2
0
The above answer works for restricting the object property names to lower-case letters. If you do not need to restrict the property names, then this is simpler:
上面的答案适用于将对象属性名称限制为小写字母。如果您不需要限制属性名称,那么这更简单:
{
"id": "my-schema",
"type": "object",
"properties": {
"objects": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string }
}
},
"required": [
"id",
"name"
]
}
}
}
}
I omitted the inner "additionalProperties": false
from the above answer because I find that using that keyword causes more problems than it solves, but it is a valid use of the keyword if you want validation to fail on the inner objects if they have properties other than "name" and "id".
我从上面的答案中省略了内部的“additionalProperties”:false,因为我发现使用该关键字会导致比它解决的问题更多的问题,但如果你希望验证在内部对象上失败(如果它们具有属性),则它是对关键字的有效使用除了“name”和“id”之外。