Skip to content

Dynamic enum values based on other values in document #331

Closed
@colbywhite

Description

@colbywhite

Consider the following correct JSON:

{
  "things": [
    { "name": "A", "someOtherProp": 3 },
    { "name": "B", "someOtherProp": 9 }
   ],
  "schedule": ["A", "B", "B", "A"]
}

And consider the following JSON that one might want to be incorrect:

{
  "things": [
    { "name": "A", "someOtherProp": 3 },
    { "name": "B", "someOtherProp": 9 }
   ],
  "schedule": ["A", "B", "B", "A", "C"]
}

As far as I can tell - and I very well may just not be seeing it - but I don't think the current spec allows me to define that all items of the schedule property must be also present in things[*].name. The "C" in the second JSON should be invalid since it doesn't appear in things[*].name. I don't think you can use oneOf or enum in this scenario since you don't know beforehand what things[*].name are. They can be anything the user decides, in this case any string.

To further illustrate the point, the following schema validates both JSONs.

{
  "schema": "http://json-schema.org/schema#",
  "type": "object",
  "properties": {
    "things": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [ "name" ],
        "properties": {
          "name": { "type": "string" }
        }
      }
    },
    "schedule": {
      "type": "array",
      "items": { "type": "string" }
    }
  }
}

Assuming I'm not missing anything and the current spec doesn't support this, would this be a worthwhile addition? Or is it outside the scope of this? Is this related to the $data discussion that appears to be happening?

For the sake of brainstorming what that would look like using $ref & oneOf (which might not be the best option):

    "schedule": {
      "type": "array",
      "items": { 
        "type": "string", 
        "oneOf": "$ref/things/*/name"
      }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Closed

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions