JSON 架构 - 除了架构中声明的字段外,实例没有其他字段
JSON Schema - Instance has no additional fields other than the ones declared in the schema
我想弄清楚是否有办法验证特定 JSON 实例在架构中声明的字段之外没有其他字段。
让我们以这个模式为例:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Client",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"email": {
"type": "string"
},
"isActive": {
"type": "boolean"
}
},
"required": []
}
我想强制执行此 "type" 的任何 JSON 只能有姓名、电子邮件和 isActive。如果验证了以下 JSON 我希望验证失败:
{
"name": "John",
"email": "john@example.com",
"isActive": true,
"extrafield": 123
}
我正在使用 json-schema-validator 来执行验证,但我感觉我使用的特定验证器不仅仅是一个实现 issue/assumption,它是我的 JSON 模式那不是强制防止未定义的字段。
我怎样才能做到这一点?
谢谢
您要查找的是additionalProperties
。
The value of "additionalProperties" MUST be a valid JSON Schema.
This keyword determines how child instances validate for objects,
and does not directly validate the immediate instance itself.
Validation with "additionalProperties" applies only to the child
values of instance names that do not match any names in "properties",
and do not match any regular expression in "patternProperties".
For all such properties, validation succeeds if the child instance
validates against the "additionalProperties" schema.
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.5.6
翻译:properties
中的键的值是一个模式,应用于键值实例的对象。
实例对象的任何未被 properties
(或 patternProperties
)验证的属性,然后由 additionalProperties
.
评估
任何模式的值都可以是 false
,这表明验证失败,因此您要将 additionalProperties: false
添加到您的模式。
但是请注意,additionalProperties
无法“看穿”其他应用程序关键字,如 oneOf
,并且仅适用于 properties
和 patternProperties
。
(draft-8 将引入一个新的关键字unevaluatedProperties
来提供此功能,但该版本目前未发布)。
我想弄清楚是否有办法验证特定 JSON 实例在架构中声明的字段之外没有其他字段。
让我们以这个模式为例:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Client",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"email": {
"type": "string"
},
"isActive": {
"type": "boolean"
}
},
"required": []
}
我想强制执行此 "type" 的任何 JSON 只能有姓名、电子邮件和 isActive。如果验证了以下 JSON 我希望验证失败:
{
"name": "John",
"email": "john@example.com",
"isActive": true,
"extrafield": 123
}
我正在使用 json-schema-validator 来执行验证,但我感觉我使用的特定验证器不仅仅是一个实现 issue/assumption,它是我的 JSON 模式那不是强制防止未定义的字段。
我怎样才能做到这一点? 谢谢
您要查找的是additionalProperties
。
The value of "additionalProperties" MUST be a valid JSON Schema.
This keyword determines how child instances validate for objects, and does not directly validate the immediate instance itself.
Validation with "additionalProperties" applies only to the child
values of instance names that do not match any names in "properties", and do not match any regular expression in "patternProperties".For all such properties, validation succeeds if the child instance validates against the "additionalProperties" schema.
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.5.6
翻译:properties
中的键的值是一个模式,应用于键值实例的对象。
实例对象的任何未被 properties
(或 patternProperties
)验证的属性,然后由 additionalProperties
.
任何模式的值都可以是 false
,这表明验证失败,因此您要将 additionalProperties: false
添加到您的模式。
但是请注意,additionalProperties
无法“看穿”其他应用程序关键字,如 oneOf
,并且仅适用于 properties
和 patternProperties
。
(draft-8 将引入一个新的关键字unevaluatedProperties
来提供此功能,但该版本目前未发布)。