如何在不修改引用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:
allOfwith the original schema reference: This pulls in all the other properties and constraints fromline-item.jsonthat you still need.- Redefine
statusand enforce it as required: The second schema inallOfreplaces thestatusproperty's definition with your own fromdefinitions.json, and explicitly marks it as required (reinforcing the original requirement but with your own rules). notclause to block the original enum: This tells the validator that thestatusproperty cannot match any of the original enum values fromline-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 originalline-item.json'sstatusproperty. If you don't want to hardcode these, you could reference the originalstatusdefinition directly in thenotclause (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
相关产品推荐
相关产品推荐

