-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Inner discriminated union #1647
Labels
closeable?
This might be ready for closing
Comments
Hi @Zimtir , the following will work under the latest commit of #1618. Feel free to verify by checking out the commit and putting this in the const carScheme = z.object({
name: z.string(),
settings: z.discriminatedUnion("model", [
z.object({ model: z.literal("AI-controlled"), copilot: z.boolean() }),
z.object({
model: z.literal("Human-controlled"),
copilot: z.literal(false),
}),
z.discriminatedUnion("network", [
z.object({
model: z.literal("Human-controlled-2"),
network: z.literal("Wi-Fi"),
}),
z.object({
model: z.literal("Human-controlled-2"),
network: z.literal("Bluetooth"),
}),
]),
]),
});
carScheme.parse({
name: "valid",
settings: {
model: "AI-controlled",
copilot: true,
},
});
carScheme.parse({
name: "valid",
settings: {
model: "Human-controlled",
copilot: false,
},
});
carScheme.parse({
name: "valid",
settings: {
model: "Human-controlled-2",
network: "Bluetooth",
},
});
// note: this will not work
carScheme.parse({
name: "valid",
settings: {
model: "Human-controlled-2",
network: "Wi-Fi",
},
});
console.log("success"); |
@the-homeless-god I'd like to close this issue if there are no further questions. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Issue statement:
As developer, I want to be able to create sub-unions under union without requirement to change parent-union key or within
Playground
Expected behaviour:
Current behaviour:
Repro code:
The text was updated successfully, but these errors were encountered: