union
Use this schema to validate union options by trying each option until one matches.
union is an alias of or.
import {union, string, number} from '@livon/schema';
const StringOrNumber = union({
options: [
string(),
number(),
] as const,
});
const a = StringOrNumber.parse('hello');
const b = StringOrNumber.parse(42);
union.options can use any value schema from this section.
API schemas (api) are not valid as union options.
Parameters
name(string, optional): explicit union schema name override. If omitted, LIVON derives a deterministic name from option schema names.options(readonly Schema[], required): candidate schemas.discriminator((input, ctx) => Schema | undefined, optional): picks one schema before fallback matching.doc(SchemaDoc, optional): schema metadata attached to AST/doc output.
Chain API
- No schema-specific chain methods.
- Shared methods on current type
T = union(options):optional(): Schema<T | undefined>,nullable(): Schema<T | null>,describe(doc: SchemaDoc): Schema<T>,refine(input): Schema<T>,before(hook): Schema<T>,after<U>(hook): Schema<U>,and<U>(other: Schema<U>, options?: {name?: string}): Schema<T & U>.