union
Creates a union schema of the provided schemas.
Example​
import { union, string, number, literal } from "@sodd/core";
const stringOrNumberSchema = union([string(), number()]);
stringOrNumberSchema.parse("hello"); // ✅
stringOrNumberSchema.parse(123); // ✅
stringOrNumberSchema.parse(true); // 🚨
// discriminated unions works too!
const foodSchema = union([
object({
type: literal("pizza"),
toppings: array(string()),
}),
object({
type: literal("burger"),
withFries: boolean(),
}),
object({
type: literal("sodd"),
}),
]);
type Food = Infer<typeof foodSchema>;
// => { type: "pizza"; toppings: string[]; } | { type: "burger"; withFries: boolean; } | { type: "sodd"; }
foodSchema.parse({ type: "pizza", toppings: ["cheese", "tomato"] }); // ✅
foodSchema.parse({ type: "burger", withFries: true }); // ✅
foodSchema.parse({ type: "sodd" }); // ✅
foodSchema.parse({ type: "hotdog" }); // 🚨
Issue types​
InvalidUnionIssue
— if none of the schemas match the input. This issue contains all the issues from the schemas that were tried.