You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不修改引用JSON Schema的前提下重定义必填属性status

How to Override a Required Enum Property in a Referenced JSON Schema (Draft-04)

Got it, let's break down how to fix this. The core issue here is that JSON Schema Draft-04 uses a "logical AND" approach with allOf—so when you just add a new properties.status definition alongside your referenced schema, you're actually requiring the status property to satisfy both the original enum constraint and your new definition, which is impossible. And adding a local definitions.status does nothing because the original schema doesn't reference that local definition.

Here's the working solution that lets you keep using the original schema while replacing the status property with your own required definition:

"lineitem": {
  "name": "Item",
  "description": "Single item found in an order.",
  "type": "object",
  "additionalProperties": true,
  "allOf": [
    { "$ref": "../line-item.json" },
    {
      "properties": {
        "status": { "$ref": "definitions.json#/definitions/status" }
      },
      "required": ["status"]
    }
  ],
  "not": {
    "properties": {
      "status": {
        "enum": ["ORIGINAL_ENUM_VALUE_1", "ORIGINAL_ENUM_VALUE_2"]
      }
    }
  }
}

Let's walk through each part:

  • allOf with the original schema reference: This pulls in all the other properties and constraints from line-item.json that you still need.
  • Redefine status and enforce it as required: The second schema in allOf replaces the status property's definition with your own from definitions.json, and explicitly marks it as required (reinforcing the original requirement but with your own rules).
  • not clause to block the original enum: This tells the validator that the status property cannot match any of the original enum values from line-item.json, effectively overriding the original constraint.

Important Notes:

  • You'll need to replace ["ORIGINAL_ENUM_VALUE_1", "ORIGINAL_ENUM_VALUE_2"] with all the enum values defined in the original line-item.json's status property. If you don't want to hardcode these, you could reference the original status definition directly in the not clause (e.g., "$ref": "../line-item.json#/properties/status").
  • Since you can't modify the original schema, this is the closest you can get to "overriding" a property in Draft-04—later versions of JSON Schema (like Draft-2019-09+) have more streamlined mechanisms for this, but Draft-04 requires this workaround.

内容的提问来源于stack exchange,提问作者ShiningLight

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:26:46