如何验证 jsonschema 中的对象值?
How to validate object values in jsonschema?
假设我有一个这样的json:
{"1": {"first_name": "a", "last_name": "b"},
"2": {"first_name": "c", "last_name": "d"}}
如您所见,值具有这样的架构:
{"type": "object",
"properties": {
"first_name": {"type": "string"},
"last_name": {"type": "string"}
},
"additionalProperties": false,
"required": ["first_name", "last_name"]}
我想知道如何定义可以验证上述内容的模式json?
additionalProperties
采用 JSON 架构作为其值。 (是的,布尔值是有效的 JSON 模式!)
让我们回顾一下 additionalProperties
关键字的作用...
The behavior of this keyword depends on the presence and annotation
results of "properties" and "patternProperties" within the same schema
object. Validation with "additionalProperties" applies only to the
child values of instance names that do not appear in the annotation
results of either "properties" or "patternProperties".
For all such properties, validation succeeds if the child instance
validates against the "additionalProperties" schema.
https://json-schema.org/draft/2020-12/json-schema-core.html#additionalProperties
简单来说,如果您不在同一个架构对象中使用 properties
或 patternProperties
,则 additionalProperties
的值架构适用于适用对象的所有值你的实例。
因此,您只需按如下方式嵌套现有架构。
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"additionalProperties": YOUR SCHEMA
}
假设我有一个这样的json:
{"1": {"first_name": "a", "last_name": "b"},
"2": {"first_name": "c", "last_name": "d"}}
如您所见,值具有这样的架构:
{"type": "object",
"properties": {
"first_name": {"type": "string"},
"last_name": {"type": "string"}
},
"additionalProperties": false,
"required": ["first_name", "last_name"]}
我想知道如何定义可以验证上述内容的模式json?
additionalProperties
采用 JSON 架构作为其值。 (是的,布尔值是有效的 JSON 模式!)
让我们回顾一下 additionalProperties
关键字的作用...
The behavior of this keyword depends on the presence and annotation results of "properties" and "patternProperties" within the same schema object. Validation with "additionalProperties" applies only to the child values of instance names that do not appear in the annotation results of either "properties" or "patternProperties".
For all such properties, validation succeeds if the child instance validates against the "additionalProperties" schema.
https://json-schema.org/draft/2020-12/json-schema-core.html#additionalProperties
简单来说,如果您不在同一个架构对象中使用 properties
或 patternProperties
,则 additionalProperties
的值架构适用于适用对象的所有值你的实例。
因此,您只需按如下方式嵌套现有架构。
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"additionalProperties": YOUR SCHEMA
}