YousefED / typescript-json-schema

Generate json-schema from your Typescript sources
BSD 3-Clause "New" or "Revised" License
3.17k stars 323 forks source link

Missing '"nullable": true' in resulting jsonschema #419

Open oddball opened 3 years ago

oddball commented 3 years ago

I am using https://www.graphql-code-generator.com/ to generate types from graphql schemas. I am generating json schemas from those types, but fields that are nullable does not come out as nullable.

test.ts:

export type Maybe<T> = T | null;

export type Scalars = {
    String: string;
};

export type CreateTransactionInput = {
    readonly brokerTradeId?: Maybe<Scalars["String"]>;
};
> npx typescript-json-schema --noExtraProps test.ts CreateTransactionInput
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "additionalProperties": false,
    "properties": {
        "brokerTradeId": {
            "type": "string"
        }
    },
    "type": "object"
}

In my mind, the result is missing below on property brokerTradeId

"nullable": true,

What am I doing wrong?

frontendphil commented 3 years ago

Came here for exactly that.

Some more of my context. I've defined a type:

type A = string | null

I know that when I set --strictNullCheck I get a type: ['string', 'null'] in the resulting schema. However, the typescript support of ajv complains that this is not valid JSON schema syntax and that the property should rather have nullable: true set.

So, I have no idea who is right on this one :D

nicolabello commented 3 years ago

In my case, I'm using the programmatic version.

For: export interface Data { name: string | null; }

When I execute: TJS.generateSchema(program, 'Data', { ref: false, required: true, strictNullChecks: true });

I get: { 'type': 'object', 'properties': { 'name': { 'type': 'string' } }, 'required': ['name'], '$schema': 'http://json-schema.org/draft-07/schema#', }

It should be { 'type': ['string', 'null'] }

CloudPower97 commented 3 years ago

I have the same exact problem as described by @frontendphil Did you manage to resolve this issue somehow?

sadnessOjisan commented 2 years ago

I have same question about https://github.com/YousefED/typescript-json-schema/issues/419#issuecomment-834220793. Is there workaround to solve it?

fxalgrain commented 2 years ago

You can manage it using validationKeywords option. I'm using the programmatic version too. So I set:

  await TJS.exec(`${projectFolder}/tsconfig.json`, "*", {
    ...TJS.getDefaultArgs(),
    required: true,
    noExtraProps: true,
    defaultNumberType: "integer",
    include: ["**/*.dto.ts"],
    validationKeywords: ["example", "nullable"],
    out,
  });

I get in my output :

"title": {
  "description": "title of the item.",
  "example": "'My title'",
 "nullable": true,
 "type": "string"    
},
wma-dev commented 1 year ago

I know that when I set --strictNullCheck I get a type: ['string', 'null'] in the resulting schema. However, the typescript support of ajv complains that this is not valid JSON schema syntax and that the property should rather have nullable: true set.

This works, but it is --strictNullChecks (with an s) now

Wavewash commented 9 months ago

Ran into this issue as well - we had a project that couldn't have strict mode on; Adding the work around that worked for us:

The strictNullChecks option isn't respected unless you also have "strict: true" set in your tsconfig also. If you can not have "strict: true" set in your tsconfig (or on by default) you can force the null value to show in the schema by adding the @nullable annotation to your property:

export interface KeyframeData {
    /**
     * @nullable true
     */
    draglock?: boolean;
}

produces:

KeyframeData": {
            "properties": {
                "draglock": {
                    "type": [
                        "boolean",
                        "null"
                    ]
                }
            }
            "type": "object"
        },