Skip to content

Tuple items with minItems and additionalItems #218

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

Closed
epferrari opened this issue Feb 1, 2019 · 1 comment · Fixed by #242
Closed

Tuple items with minItems and additionalItems #218

epferrari opened this issue Feb 1, 2019 · 1 comment · Fixed by #242

Comments

@epferrari
Copy link

epferrari commented Feb 1, 2019

We have a use case I don't think is currently covered. We have an array where 2 items must be defined, a max of 3, and the first one has an additional constraint. JsonSchema allows for this, but the generated type isn't what we'd expect.

schema:

{
  "title": "Foo",
  "type": "array",
  "minItems": 2,
  "maxItems": 3,
  "definitions": {
    "foo_0": {
      "title": "Foo Item",
      "type": "integer",
      "exclusiveMinimum": 0
    },
    "foo_n": {
      "title": "Foo Item",
      "type": "integer"
    }
  },
  "items": [{
    "$ref": "#/definitions/foo_0"
  }, {
    "$ref": "#/definitions/foo_n"
  }],
  "additionalItems": {
    "$ref": "#/definitions/foo_n"
  }
}

expected generated ts:

export type Foo = [FooItem, FooItem1, FooItem1?];
export type FooItem = number;
export type FooItem1 = number;

actual generated ts:

export type Foo = [FooItem, FooItem1];
export type FooItem = number;
export type FooItem1 = number;

using version 6.1.1

@bcherny
Copy link
Owner

bcherny commented Feb 4, 2019

This is a fun one :)

  • Bug Ensure that code complies with official JSON-schema test suite #1 is when we infer a tuple type, we should look to additionalItems as the spread property. In your example, the correct type to generate is [FooItem, FooItem1, ...FooItem1[]].
  • Enhancement Support custom JSON-schema extensions #2 is we should take minLength and maxLength into account when specifying tuple types. As a first pass:
    1. When minLength !== items.length, we should throw a validation error
    2. When maxLength is defined, we should generate optional tuple properties (eg. FooItem1?) instead of a rest property (eg. ...FooItem1).

Want to take a shot at Bug 1?

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

Successfully merging a pull request may close this issue.

2 participants