Open
Description
Consider the following type.
type Opt = {
type: "typeA",
reqProp: number
} | {
type: "typeB",
optProp?: string
}
{
"$ref": "#/definitions/Opt",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Opt": {
"anyOf": [
{
"additionalProperties": false,
"properties": {
"reqProp": {
"type": "number"
},
"type": {
"const": "typeA",
"type": "string"
}
},
"required": [
"type",
"reqProp"
],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
"optProp": {
"type": "string"
},
"type": {
"const": "typeB",
"type": "string"
}
},
"required": [
"type"
],
"type": "object"
}
]
}
}
}
Generating the type above results an anyOf
block. While reqProp
is required, it prevents typeA
being listed in type
option.
The problem can be solved if using dependencies
.
{
"$ref": "#/definitions/Opt",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Opt": {
"properties": {
"type": {
"enum": ["typeA", "typeB"]
}
},
"dependencies": {
"type": {
"oneOf": [
{
"additionalProperties": false,
"properties": {
"reqProp": {
"type": "number"
},
"type": {
"const": "typeA",
"type": "string"
}
},
"required": ["type", "reqProp"],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
"optProp": {
"type": "string"
},
"type": {
"const": "typeB",
"type": "string"
}
},
"required": ["type"],
"type": "object"
}
]
}
}
}
}
}