Open oddball opened 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
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'] }
I have the same exact problem as described by @frontendphil Did you manage to resolve this issue somehow?
I have same question about https://github.com/YousefED/typescript-json-schema/issues/419#issuecomment-834220793. Is there workaround to solve it?
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"
},
I know that when I set
--strictNullCheck
I get atype: ['string', 'null']
in the resulting schema. However, the typescript support ofajv
complains that this is not valid JSON schema syntax and that the property should rather havenullable: true
set.
This works, but it is --strictNullChecks
(with an s
) now
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"
},
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:
In my mind, the result is missing below on property brokerTradeId
What am I doing wrong?