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

Support for boolean JSON Schemas #496

Open
notaphplover opened this issue Nov 22, 2022 · 6 comments
Open

Support for boolean JSON Schemas #496

notaphplover opened this issue Nov 22, 2022 · 6 comments

Comments

@notaphplover
Copy link
Contributor

notaphplover commented Nov 22, 2022

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:

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
Copy link
Contributor Author

notaphplover commented Jan 22, 2023

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

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

@notaphplover
Copy link
Contributor Author

@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 😃 )

@notaphplover
Copy link
Contributor Author

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

@notaphplover
Copy link
Contributor Author

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

@notaphplover
Copy link
Contributor Author

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

@tukusejssirs
Copy link

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

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

No branches or pull requests

3 participants