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

Using oneOf basing on value attribute #738

Closed
NicolaSpreafico opened this issue May 21, 2019 · 4 comments
Closed

Using oneOf basing on value attribute #738

NicolaSpreafico opened this issue May 21, 2019 · 4 comments
Labels

Comments

@NicolaSpreafico
Copy link

NicolaSpreafico commented May 21, 2019

Hi,
I have a json configured as this example:

{
  "id": 1,
  "values": [
    {
      "type": "A",
      "values": {
        "foo": "bar"
      }
    },
    {
      "type": "B",
      "values": {
        "foo": [
          "bar",
          "baz"
        ]
      }
    },
    {
      "type": "C",
      "values": {
        "foo": {
          "bar": "baz"
        }
      }
    }
  ]
}

Basing on the value of key type (which is enum), the schema for property values is different.
Is there a way to create a conditional-like (like with anyOf) but selecting the proper schema basing on the value of type property?

@NicolaSpreafico NicolaSpreafico changed the title Using anyOf basing on value attribute Using oneOf basing on value attribute May 21, 2019
@handrews
Copy link
Contributor

@NicolaSpreafico I think you are looking for something like this (which assumes you are using draft-07, a.k.a. draft-handrews-json-schema-validation-01):

{
  "type": "object",
  "properties": {
    "id": {"type": "integer"},
    "values": {
      "type": "array",
      "items": {
        "required": ["type"],
        "oneOf": [
          {   
            "if": {"properties": {"type": {"const": "A"}}},
            "then": {"properties": {"foo": {"type": "string"}}}
          },  
          {   
            "if": {"properties": {"type": {"const": "B"}}},
            "then": {"properties": {"foo": {"type": "array"}}}
          },  
          {   
            "if": {"properties": {"type": {"const": "C"}}},
            "then": {"properties": {"foo": {"type": "object"}}}
          }   
        ]   
      }   
    }   
  }
}

@handrews
Copy link
Contributor

No further question or response for a couple months, so I'm assuming this was a good enough answer and closing.

@NicolaSpreafico
Copy link
Author

Yes, thank you very much for the reply

@btiernay
Copy link

Please see #1082 for related work in this area.

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

No branches or pull requests

3 participants