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

Missing '"nullable": true' in resulting jsonschema

Open oddball opened this issue 3 years ago • 7 comments

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?

oddball avatar May 06 '21 15:05 oddball

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

frontendphil avatar May 07 '21 09:05 frontendphil

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'] }

nicolabello avatar Jun 28 '21 11:06 nicolabello

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

CloudPower97 avatar Jul 17 '21 20:07 CloudPower97

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

sadnessOjisan avatar Dec 14 '21 04:12 sadnessOjisan

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"    
},

fxalgrain avatar Feb 11 '22 16:02 fxalgrain

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

wma-dev avatar Feb 07 '23 18:02 wma-dev

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"
        },

Wavewash avatar Feb 06 '24 20:02 Wavewash