-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
Combining 'allOf' with 'If' / 'then' checks Do not resolve correctly in TS #167
Comments
Hi @jfarre90 and thanks for the reach out ! This is a tricky one: What So it compute the intersection of those schemas:
{
type: "object",
properties: {},
required: [],
additionalProperties: true,
if: ... (cat),
then ... (cat)
}
{
type: "object",
properties: {},
required: [],
additionalProperties: true,
if: ... (dog),
then ... (dog)
}
{
type: "object",
properties: {
animal,
...
},
required: ["animal",...]
} The pb is that the first two schemas, taken independently, are not mutually exclusive because the If you add it to the child schemas, it will actually work! const configTest = {
type: "object",
properties: {
animal: { $ref: "#/$defs/animal" },
dynamicProperty: {},
},
additionalProperties: false,
allOf: [
{
properties: {
animal: { $ref: "#/$defs/animal" },
},
if: {
properties: {
animal: { const: "cat" },
},
},
then: {
properties: {
dynamicProperty: {
type: "object",
properties: {
key1: { type: "string" },
},
required: ["key1"],
additionalProperties: false,
},
},
},
},
{
properties: {
animal: { $ref: "#/$defs/animal" },
},
if: {
properties: {
animal: { const: "dog" },
},
},
then: {
properties: {
dynamicProperty: {
type: "null",
},
},
},
},
],
required: ["animal", "dynamicProperty"],
$defs: {
animal: { enum: ["cat", "dog"] },
},
} as const;
type CONFIGTEST = FromSchema<
typeof configTest,
{ parseIfThenElseKeywords: true }
>; What I'm sure of is that properties cannot naively be merged from child to parent schemas. Still, I'm not sure resetting all properties is the best approach 🤷♂️ WDYT ? |
Hi @ThomasAribart! Thanks for the reply and info shared, much appreciated. I agree with your points and it makes sense. In our use case we ended up defining 2 separate schemas, one for the "dog" and another for the "cat" in this example. And within our logic we had a switch/case that used one schema or the other for some validation. As for the type generated, we just applied the condition from our end like...
Which for our use case ended up working nicely, and we didn't have to use the However, we are still refactoring a big part of our application to TS, so the info above is helpful in case we face a similar scenario down the line. Thanks again for the great work! Finding this library very useful 🎉 |
The following schema:
Generates this type:
I would expect this to generate a type that looks something like this:
Why does the logic include the last optional type? Is this a bug?
The text was updated successfully, but these errors were encountered: