Skip to content

Allow using dependencies schema on certain prop #2011

Open
@neko-para

Description

@neko-para

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.

84d2f0a91693edbbe344581fa00dd343

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"
                        }
                    ]
                }
            }
        }
    }
}

557291bfcfa13cfb39bfecdc9e3051f3


Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions