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

Positional and optional item in array ? #29

Open
alfredking1988 opened this issue Apr 18, 2020 · 2 comments
Open

Positional and optional item in array ? #29

alfredking1988 opened this issue Apr 18, 2020 · 2 comments

Comments

@alfredking1988
Copy link

Currently, the JSON Schema seems only to allow rear optional items through additionalItems.

Is it possible to allow positional optional items?

Such as this imaginary schema:

{
  "type": "array",
  "items": [
    {
      // user must provide a nickname
      "type": "string"
    },
    {
      // the age is personal, so it is OPTIONAL
      // "optional": true,
      "type": "number"
    },
    {
      // user must provide a email
      "type": "string"
    }
  ]
}

Given the above imaginary schema, the following values are both valid:
['Tom', 30, 'tom@example.com']: Tom provides all, including the optional age.
['Alice', 'alice@example.com']: Alice does not provide the optional age.

Of course, the schema author must ensure the mutually exclusive between the positional-optional item and the following item. In the example above, the positional-optional age's type is number, the email's type is string, they are mutually exclusive, so the schema is unambiguous.

@notEthan
Copy link

notEthan commented Apr 18, 2020

positions in a json schema items array are fixed to the corresponding numeric index, so you'll need different schemas for different permutations of the indices. something like (yaml):

oneOf:
  - items:
      - {type: string, description: "name"}
      - {type: number, description: "age"}
      - {type: string, description: "email"}
  - items:
      - {type: string, description: "name"}
      - {type: string, description: "email"}

that should use $refs to avoid duplication, but even still, it would rapidly get out of hand if you have multiple optional elements. arrays where elements can appear at varying indices are not a kind of structure that json-schema is all that well-suited to describe.

@handrews
Copy link
Contributor

This is the kind of complex edge case keyword that a few people need but most people do not, that is great for implementing as a (3rd-party) extension keyword. So I'm transferring it to our repository that holds those sorts of ideas.

@handrews handrews transferred this issue from json-schema-org/json-schema-spec Apr 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants