json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

Support for boolean JSON Schemas

Open notaphplover opened this issue 3 years ago • 6 comments

Context:

Boolean JSON schemas are true and false:

  • true passes the validation of any input.
  • false fails the validation of any input.

Consider the specs as references:

  • https://datatracker.ietf.org/doc/html/draft-wright-json-schema-00#section-4.4
  • https://datatracker.ietf.org/doc/html/draft-wright-json-schema-01#section-4.4
  • https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-00#section-4.3.1
  • https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-01#section-4.3.1
  • https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-02#section-4.3.2
  • https://json-schema.org/draft/2020-12/json-schema-core.html#name-boolean-json-schemas

How do we apply this in the library:

I would suggest the following changes:

  • Whenever a true JSON Schema is transpiled to a type, the transpiled type should be any or unknown (depending on the config).
  • Whenever a false JSON Schema is transpiled to a type, the transpiled type should be never

Examples:

{
     "$schema": "http://json-schema.org/draft/2020-12/schema",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "color": true,
        "kind": true,
        "number": false
      },
      "required": ["color, kind", "number"]
}

should be transpiled to something like:

export interface Foo {
  color: unknown;
  kind: unknown;
  number: never;
}

I know, number: never feels ugly, but never is probably the type which works nicely not only in this simple example but also in more complex ones.

Current behavior:

Boolean JSON schemas are transpiled into their boolean types:

{
  "$schema": "http://json-schema.org/draft/2020-12/schema",
  "title": "Example Schema",
  "type": "object",
  "properties": {
    "foo": true
  },
  "required": ["foo"]
}

is transpiled into:

export interface ExampleSchema {
  foo: true
  [k: string]: unknown
}

instead of

export interface ExampleSchema {
  foo: unknown
  [k: string]: unknown
}

notaphplover avatar Nov 22 '22 16:11 notaphplover

Hey @bcherny , I assume I'm free to go for it, I'll try to do a good job :heart: .

Edit: It seems there's already #503 , I'll add some notes I consider relevant.

notaphplover avatar Jan 22 '23 17:01 notaphplover

@bcherny I'll try to submit a PR since this one seems to be a little bit stuck. Having said that, I'm afraid after having a look at the source code, I think I've detected a non trivial issue which blocks us from acomplishing this fix.

Long story short: the parser always asumes it's parsing a JSONSchema, but not every part of a JSONSchema should be parsed as a JSONSchema but as a JSON literal instead. Parsing enum values is an excellent example of this. I'll submit a proper issue tomorrow, hopefully it's not too hard to be solved (I might have an idea of how to solve it :smiley: )

notaphplover avatar Feb 05 '23 23:02 notaphplover

As discussed, this is the issue it's currently blocking us: #508

notaphplover avatar Feb 06 '23 18:02 notaphplover

The issue blocking us is solved! Since #503 seems not to be updated, I'll be on my way to submit a PR :)

notaphplover avatar Feb 23 '23 23:02 notaphplover

#515 was submitted :), I'll do my best to pass all the quality checks and @bcherny suggestions in order to merge it.

notaphplover avatar Feb 23 '23 23:02 notaphplover

As #515 is merged, should this issue be closed? 🤔

tukusejssirs avatar Apr 25 '24 06:04 tukusejssirs