TATECHATLAS
◎ 简体中文
人工智能 / 建议

JSON Schema allOf 扩展:additionalProperties 与 unevaluatedProperties

additionalProperties 只能看到同一子模式中的属性,因此 allOf 扩展无法拒绝未知键。使用 draft 2019-09 或更高版本中的 unevaluatedProperties 来识别嵌套子模式属性而无需重新声明它们。

本文内容

additionalProperties 只检查同一子模式中声明的属性。当基准模式在 allOf 数组中定义属性和必需字段时,扩展模式添加新属性时,无法依赖 additionalProperties 来拒绝未知键,因为该关键字无法看到嵌套子模式。文档化的解决方法是将 additionalProperties 移到扩展模式并重新声明基准属性,但这会重复定义并在基准模式更改时破坏。更清晰的解决方法是使用 draft 2019-09 中引入的 unevaluatedProperties。它可以跨 allOf 中的所有子模式评估属性,因此可以识别嵌套子模式中声明的属性并在不重新声明它们的情况下正确拒绝未知键。然而,unevaluatedProperties 需要 draft 2019-09 或更高版本,并且它不声明允许的属性;您仍然需要使用 properties 或 patternProperties 来声明允许的键。它还不能验证未知键对不属于组合操作的模式的有效性。

具体示例和适用性限制

示例模式使用 allOf 结合两个对象子模式。第一个子模式声明 street_address、city 和 state 作为属性并要求它们。第二个子模式声明 type 作为一个枚举(residential 或 business)并要求它。if/then 规则仅在 type 为 business 时添加 department。扩展模式将 unevaluatedProperties 设置为 false,这拒绝任何未在组合子模式中声明的属性。实例是有效的,因为 street_address、city、state、type 和 department 都被识别。如果实例包含未知键,验证将失败,因为 unevaluatedProperties 为 false。这在不重新声明 street_address、city、state 或 type 的情况下工作。

关键限制是 unevaluatedProperties 不声明允许的属性。它仅控制在组合子模式评估后是否拒绝未知键。要允许特定键,您仍然需要 properties 或 patternProperties。它还不能验证未知键对不属于组合操作的模式的有效性。如果属性在不与 allOf 组合的模式中声明,unevaluatedProperties 将无法看到它。该关键字仅在 draft 2019-09 或更高版本中可用。较旧的草案需要重新声明的解决方法,其中 additionalProperties 被移到扩展模式并重新声明基准属性。

schema: allOf: - type: object properties: street_address: type: string city: type: string state: type: string required: [street_address, city, state] - type: object properties: type: enum: [residential, business] required: [type] if: type: object properties: type: const: business then: properties: department: type: string unevaluatedProperties: false data: street_address: 1600 Pennsylvania Avenue NW city: Washington state: DC type: business department: HR

为什么 additionalProperties 会破坏 allOf 的模式扩展

additionalProperties 只检查同一子模式中声明的属性。当基准模式在 allOf 数组中定义属性和必需字段时,扩展模式添加新属性时,无法依赖 additionalProperties 来拒绝未知键,因为该关键字无法看到嵌套子模式。文档化的示例显示了一个基准模式,其中 allOf 包含一个声明 street_address、city 和 state 的子模式,以及一个 required 数组。扩展模式添加 type 并要求它,但 additionalProperties 仍然只识别 street_address、city 和 state。任何其他属性都被视为附加属性,因此扩展模式无法在不重新声明基准属性的情况下拒绝未知键。

问题在于使用 allOf 结合模式不会改变 additionalProperties 的工作方式。每个子模式都独立评估以确定 additionalProperties 的目的,因此在后续子模式中声明的属性对基准模式或扩展模式中的关键字不可见。这意味着如果您尝试使用 additionalProperties 来禁止扩展模式中的未知键,它将允许不想要的键或要求您重新声明基准模式的每个属性。解决方法是将 additionalProperties 移到扩展模式并重新声明扩展模式的属性,如源示例所示。这使 additionalProperties 能够识别所有必要的属性,但它重复属性定义并在基准模式更改时破坏。

schema { " allOf " : [ { " type " : "object" , " properties " : { " street_address " : { " type " : "string" } , " city " : { " type " : "string" } , " state " : { " type " : "string" } } , " required " : [ "street_address" , "city" , "state" ] } ] , " properties " : { " street_address " : true , " city " : true , " state " : true , " type " : { " enum " : [ "residential" , "business" ] } } , " required " : [ "type" ] , " additionalProperties " : false }

unevaluatedProperties 如何识别嵌套子模式属性

unevaluatedProperties 与 additionalProperties 类似,但它可以识别子模式中声明的属性。源文档显示了前一个示例可以在不重新声明属性的情况下重写。模式使用 allOf 结合一个声明 street_address、city 和 state 的子模式,以及一个声明 type 为 residential 或 business 枚举的子模式。if/then 规则仅在 type 为 business 时添加 department。扩展模式将 unevaluatedProperties 设置为 false,这拒绝任何未在组合子模式中声明的属性。这之所以有效,是因为 unevaluatedProperties 在组合子模式应用后评估属性,因此它可以识别嵌套子模式中声明的属性,即使它们在扩展模式中没有重新声明。

机制是 unevaluatedProperties 在组合子模式应用后评估。它检查实例属性与所有评估子模式中声明的属性,不仅仅是同一模式对象中声明的属性。这意味着在 allOf 内的子模式中声明的属性对 unevaluatedProperties 可见,即使它们在扩展模式中没有重新声明。关键字可以因此拒绝未知键而无需重复基准模式的属性。源文档明确指出示例可以在不重新声明属性的情况下重写,并且 then 模式中声明的属性仅在地址类型为 business 时才被视为评估属性。

schema { " allOf " : [ { " type " : "object" , " properties " : { " street_addr
[excerpt]
 { " enum " : [ "residential" , "business" ] } } , " required " : [ "street_address" , "city" , "state" , "type" ] , " if " : { " type " : "object" , " properties " : { " type " : { " const " : "business" } } , " required " : [ "type" ] } , " then " : { " properties " : { " department " : { " type " : "string" } } } , " unevaluatedProperties " : false }

实用建议和权衡

实用建议是在使用 allOf 扩展模式时使用 unevaluatedProperties 代替 additionalProperties。这可以识别嵌套子模式中的属性并避免重新声明基准属性,但需要 draft 2019-09 或更高版本,并保持 properties 或 patternProperties 来声明允许的键。重新声明的解决方法在较旧的草案中有效,但有明显的缺点。您必须将 additionalProperties 移到扩展模式并重新声明扩展模式的属性。这会重复属性定义并增加维护成本和不一致性风险,当基准模式更改时。源文档将其呈现为一种解决方法,而不是推荐的模式,并明确引入 unevaluatedProperties 作为解决方案。

schema { " allOf " : [ { " type " : "object" , " properties " : { " street_addr
[excerpt]
 { " enum " : [ "residential" , "business" ] } } , " required " : [ "street_address" , "city" , "state" , "type" ] , " if " : { " type " : "object" , " properties " : { " type " : { " const " : "business" } } , " required " : [ "type" ] } , " then " : { " properties " : { " department " : { " type " : "string" } } } , " unevaluatedProperties " : false }

适用条件

  • 解析器是否支持 draft 2019-09 或更高版本?
  • 基准模式是否在 allOf 数组中声明属性和必需字段?
  • 扩展模式是否添加新属性,例如 type 和 department?
  • 是否预计实例在包含未知键时被拒绝?
  • 模式是否使用 unevaluatedProperties 代替 additionalProperties?
  • 属性和必需字段是否仍在嵌套子模式中声明?
  • then 分支是否仅在 type 为 business 时添加 department?
  • 实例是否在 type 为 residential 且 department 不存在时有效?
  • 实例是否在 type 为 business 但 department 缺失时无效?
  • 模式是否在不重新声明基准属性的情况下拒绝未知键?

unevaluatedProperties 仅在 draft 2019-09 或更高版本中可用。它不声明允许的属性;请使用 properties 或 patternProperties 来声明允许的键。它不能验证未知键对不属于组合操作的模式的有效性。它也不替代 properties 或 patternProperties 来声明允许的键。如果基准模式不是通过 allOf 组合的,unevaluatedProperties 可能无法看到其属性。关键字在组合子模式中评估属性,因此它依赖于应用的组合操作。

参考来源

  1. Python: json ↗
  2. JSON Schema: objects and required properties ↗
返回顶部 ↑