Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using dependencies schema on certain prop #2011

Open
neko-para opened this issue Jul 3, 2024 · 4 comments
Open

Allow using dependencies schema on certain prop #2011

neko-para opened this issue Jul 3, 2024 · 4 comments

Comments

@neko-para
Copy link

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


@domoritz domoritz changed the title [feature request] Allow using dependencies schema on certain prop Allow using dependencies schema on certain prop Jul 3, 2024
@domoritz
Copy link
Member

domoritz commented Jul 3, 2024

Thanks for bringing up this issue and suggesting a solution. It would be neat to explore this idea. Feel free to take a stab.

@arthurfiorette
Copy link
Collaborator

is this specified in the Json schema reference?

@neko-para
Copy link
Author

The behavior of vscode isn't specified, but can be predicted. Object that only contains type:typeA isn't valid, thus typeA isn't shown.

https://json-schema.org/draft-07/draft-handrews-json-schema-validation-01#rfc.section.6.5.7

@domoritz
Copy link
Member

domoritz commented Jul 3, 2024

One thing I worry about is support in existing schema tools like the ones used in Vegas Altair (which is one of my primary use cases). If a change makes type hierarchies clearer, it could be worth the change, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants