Modifiers & Pipelines
Chain optional, nullable, defaults, prefaults, transforms, pipes, catch fallbacks, and branded nominal types.
Optional, Nullable & Nullish
Control acceptance of undefined and null values: • .optional(): Accepts undefined. Output: T | undefined. • .nullable(): Accepts null. Output: T | null. • .nullish(): Accepts null or undefined. Output: T | null | undefined.
const S1 = infer.string().optional();
const S2 = infer.number().nullable();
const S3 = infer.date().nullish();Defaults vs Prefaults
• .default(val): Replaces undefined input with default value after inner validation. • .prefault(val): Injects default value before inner validation runs, ensuring the default itself is validated.
const Port = infer.number().default(3000);
const Host = infer.string().prefault("localhost");
const Timestamp = infer.date().default(() => new Date());Transforms & Validation Pipes
Transforms alter output values; pipes compose multiple validation schemas in a sequential pipeline:
// Transform
const StrToDate = infer.string().transform((str) => new Date(str));
// Pipeline
const CleanEmail = infer.pipe(
infer.string().trim().toLowerCase(),
infer.string().email()
);Branded Types & Codecs
Nominal branding prevents assigning raw strings to domain-specific IDs. Codecs support bidirectional serialization/deserialization:
// Nominal Branding
type UserId = Brand<string, "UserId">;
const UserIdSchema = infer.brand(infer.uuid(), "UserId");
// Bidirectional Codec
const Base64Codec = infer.codec(
infer.string().transform((s) => Buffer.from(s, "base64")),
(buf: Buffer) => buf.toString("base64")
);