Closed vikasg603 closed 2 years ago
Hi @vikasg603 ! Good catch, I validate that the schema extends the JSONSchemaV6 definition given by DefinitelyTyped.
However, the if/then/else
keywords seem to make it only in the JSONSchemaV7 one: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/json-schema/index.d.ts#L678
It seemed to me that the if/then/else
keywords were present in the v6, so I'll dig more into it. If I'm right, then I'll make a PR on DefinitelyTyped. If I'm wrong, then I was planning to rename FromSchema
to FromV6Schema
and create a parallel FromV7Schema
anyway, so now might be a good time to do it.
Meanwhile, if the computed type seems correct to you, you can safely ts-ignore
the error for now.
Is there any update on this?
I seem to have a similar error when using something like the attached schema. After some debugging I think the issue is related to having the if/then/else inside the allOf. If the if/then/else instead is at the "root" level the type is accepted.
This won't work:
const Test = {
type: "object",
properties: {
condition: {
type: "string",
enum: ["A", "B"]
},
"other": {
type: "string"
}
},
required: ["condition"],
allOf: [{
if: {
properties: {
condition: { const: "A" }
}
},
then: {
required: ["other"]
}
}],
additionalProperties: false
} as const
type TestSchema = FromSchema<typeof Test>
This will work:
const Test = {
type: "object",
properties: {
condition: {
type: "string",
enum: ["A", "B"]
},
"other": {
type: "string"
}
},
required: ["condition"],
if: {
properties: {
condition: { const: "A" }
}
},
then: {
required: ["other"]
},
additionalProperties: false
} as const
type TestSchema = FromSchema<typeof Test>
In this reduced example they two schemas are equivalent, but it's not always possible to do so (i.e. when you need multiple if/then/else you need to wrap them in the allOf)
Hi @paolochiodi !
Can you try it with v2.0.1 ?
It seems to work fine now:
const Test = {
type: "object",
properties: {
condition: {
type: "string",
enum: ["A", "B"],
},
other: {
type: "string",
},
},
required: ["condition"],
allOf: [
{
if: {
properties: {
condition: { const: "A" },
},
},
then: {
required: ["other"],
},
},
],
additionalProperties: false,
} as const;
type TestSchema = FromSchema<typeof Test, { parseIfThenElseKeywords: true }>;
type ResultType = {
condition: "A";
other: string;
} | {
other?: string | undefined;
condition: "A" | "B";
}
This is my schema:
When used FromSchema, Typescript gives some long error, unable to understand the exact error, but after removing allOf it's working properly.
Does anyone of you have any workaround for this issue?