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

Changing whether a field is optional (?) changes the schema #583

Open
samuelcolvin opened this issue Dec 13, 2023 · 1 comment
Open

Changing whether a field is optional (?) changes the schema #583

samuelcolvin opened this issue Dec 13, 2023 · 1 comment

Comments

@samuelcolvin
Copy link

samuelcolvin commented Dec 13, 2023

Great library, thanks so much.

When I have a required field which has type of a union which is itself defined as a type, the field is just a ref to that type, when I make the field optional with ?, I get an anyOf, not a $ref.

If this is intentional and therefore won't be changed, is there a way to change behaviour to be consistent?

Example - field required

type FooBar = number | string

export interface Demo {
  thing: FooBar
}

Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "FooBar": {
      "type": ["string", "number"]
    }
  },
  "properties": {
    "thing": {
      "$ref": "#/definitions/FooBar"
    }
  },
  "required": ["thing"],
  "type": "object"
}

Example - field not required

type FooBar = number | string

export interface Demo {
  thing?: FooBar
}

Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
    "thing": {
      "type": ["string", "number"]
    }
  },
  "type": "object"
}
@domoritz
Copy link
Collaborator

Have you tried https://github.com/vega/ts-json-schema-generator? It's the generator I actively maintain.

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

2 participants