import { StandardSchemaV1 } from "../standard-schema/types.mjs"; import { Schema } from "./schema-types.mjs"; import { BaseValidator } from "../validators/base-validator.mjs"; import { AnyValidator } from "../validators/any-validator.mjs"; import { ArrayValidator } from "../validators/array-validator.mjs"; import { BooleanValidator } from "../validators/boolean-validator.mjs"; import { ComputedValidator } from "../validators/computed-validator.mjs"; import { DateValidator } from "../validators/date-validator.mjs"; import { ObjectValidator } from "../validators/object-validator.mjs"; import { DiscriminatedUnionValidator } from "../validators/discriminated-union-validator.mjs"; import { FloatValidator } from "../validators/float-validator.mjs"; import { InstanceOfValidator } from "../validators/instanceof-validator.mjs"; import { IntValidator } from "../validators/int-validator.mjs"; import { LazyValidator } from "../validators/lazy-validator.mjs"; import { LiteralValidator } from "../validators/literal-validator.mjs"; import { ManagedValidator } from "../validators/managed-validator.mjs"; import { ScalarValidator } from "../validators/scalar-validator.mjs"; import { StringValidator } from "../validators/string-validator.mjs"; //#region ../@warlock.js/seal/src/types/inference-types.d.ts /** * Two inference shapes describe the two halves of the validation pipeline: * * - **`Infer.Input`** — what the *caller* is allowed to send before * validation runs. `.optional()`, `.default()`, and `.catch()` all make a * key optional from the caller's perspective: any of them means "you don't * have to supply this". * * - **`Infer.Output`** — what `validData` contains *after* validation * passes. Reflects runtime reality: `.default()` and `.catch()` guarantee a * value, so fields with those brands are non-optional in the output even * when chained with `.optional()`. * * **Bare `Infer` defaults to `Infer.Input`.** Use the explicit form * (`Infer.Output`) when typing validated data — Cascade `Model<>` type * params, the right-hand-side of a `validate()` call, anywhere downstream * of validation that wants the post-default shape. * * @example * ```ts * const schema = v.object({ * name: v.string(), * email: v.string().email().optional(), * deletedAt: v.date().nullable(), * status: v.string().optional().default("active"), * }); * * type In = Infer.Input; * // { * // name: string; * // email?: string; * // deletedAt: Date | null; * // status?: string; ← caller may omit; default fires * // } * * type Out = Infer.Output; * // { * // name: string; * // email?: string; * // deletedAt: Date | null; * // status: string; ← validData always has it * // } * * type Default = Infer; * // equivalent to Infer.Input * ``` * * The walker reads four type-level brands that validators attach via their * chain methods: * * - `{ isOptional: true }` — set by `.optional()` * - `{ isNullable: true }` — set by `.nullable()` * - `{ hasDefault: true }` — set by `.default()` * - `{ hasCatch: true }` — set by `.catch()` * * Fields marked with `.omit()` / `.exclude()` are excluded from the inferred * type via the `ComputedValidator` / runtime omission path. Computed and * managed validators surface their declared `TResult` directly. */ /** * True when the validator's type carries a brand that guarantees a value * will always be present (`.default()` or `.catch()`). */ type IsGuaranteed = V extends { hasDefault: true; } ? true : V extends { hasCatch: true; } ? true : false; /** * Whether a field should appear as an optional key in the parent object's * *output* shape. Optional ONLY when explicitly `.optional()` AND not * guaranteed by `.default()` or `.catch()` — runtime always has a value * for guaranteed fields, so the output type marks them required. */ type IsOutputOptionalKey = V extends { isOptional: true; } ? IsGuaranteed extends true ? false : true : false; /** * Whether a field should appear as an optional key in the parent object's * *input* shape. Optional whenever `.optional()`, `.default()`, or `.catch()` * is present — any of those means the caller doesn't have to supply a value * (it'll be filled, defaulted, or rescued). */ type IsInputOptionalKey = V extends { isOptional: true; } | { hasDefault: true; } | { hasCatch: true; } ? true : false; /** * Apply `| null` widening when the validator type carries `{ isNullable: true }`. * * Designed to be applied INSIDE each Infer branch (after type-pattern matching) * rather than as a top-level wrapper, because top-level wrapping creates a * class-recursive evaluation in `ObjectValidator`'s base class type parameter. */ type WithNullable = V extends { isNullable: true; } ? T | null : T; /** * Compute the inferred *output* object shape for a Schema. * * Exported so `ObjectValidator` can use this in its class definition without * going through the full `Infer.Output<>` walker — referencing the full * walker on the parent class's type parameter creates a recursive class-base * reference when combined with brand-checks. */ type InferOutputObjectShape = { [K in keyof S as IsOutputOptionalKey extends true ? K : never]?: Infer.Output } & { [K in keyof S as IsOutputOptionalKey extends true ? never : K]: Infer.Output }; /** * Compute the inferred *input* object shape for a Schema. Mirror of * `InferOutputObjectShape` but uses `IsInputOptionalKey` — fields with * `.default()` or `.catch()` (or `.optional()`) are all marked optional in * the input shape. */ type InferInputObjectShape = { [K in keyof S as IsInputOptionalKey extends true ? K : never]?: Infer.Input } & { [K in keyof S as IsInputOptionalKey extends true ? never : K]: Infer.Input }; /** * Bare `Infer` defaults to `Infer.Input`. Use `Infer.Output` when * typing validated data (Cascade `Model<>` params, validate() returns). */ type Infer = Infer.Input; declare namespace Infer { /** * Inferred *input* shape — what callers send before validation. Optional * keys whenever `.optional()`, `.default()`, or `.catch()` is present. */ type Input = T extends LazyValidator ? Input : T extends DiscriminatedUnionValidator ? Branches extends ReadonlyArray ? B extends BaseValidator ? WithNullable> : never : never : T extends ObjectValidator ? WithNullable> : T extends ArrayValidator ? T extends { validator: infer V extends BaseValidator; } ? WithNullable>> : never : T extends LiteralValidator ? WithNullable : T extends InstanceOfValidator ? WithNullable : T extends ComputedValidator ? R : T extends ManagedValidator ? R : T extends StandardSchemaV1 ? WithNullable : T extends StringValidator ? WithNullable : T extends IntValidator ? WithNullable : T extends FloatValidator ? WithNullable : T extends BooleanValidator ? WithNullable : T extends DateValidator ? WithNullable : T extends ScalarValidator ? WithNullable : T extends AnyValidator ? any : unknown; /** * Inferred *output* shape — what `validData` contains after validation. * Required keys whenever `.default()` or `.catch()` guarantees a value, * even when chained with `.optional()`. */ type Output = T extends LazyValidator ? Output : T extends DiscriminatedUnionValidator ? Branches extends ReadonlyArray ? B extends BaseValidator ? WithNullable> : never : never : T extends ObjectValidator ? WithNullable> : T extends ArrayValidator ? T extends { validator: infer V extends BaseValidator; } ? WithNullable>> : never : T extends LiteralValidator ? WithNullable : T extends InstanceOfValidator ? WithNullable : T extends ComputedValidator ? R : T extends ManagedValidator ? R : T extends StandardSchemaV1 ? WithNullable : T extends StringValidator ? WithNullable : T extends IntValidator ? WithNullable : T extends FloatValidator ? WithNullable : T extends BooleanValidator ? WithNullable : T extends DateValidator ? WithNullable : T extends ScalarValidator ? WithNullable : T extends AnyValidator ? any : unknown; } /** * @deprecated Use `Infer.Input` instead. Kept as an alias for one or two * minor versions to ease migration; removed thereafter. */ type InferInput = Infer.Input; /** * @deprecated Use `Infer.Output` instead. Kept as an alias for one or two * minor versions to ease migration; removed thereafter. */ type InferOutput = Infer.Output; /** * @deprecated Use `InferOutputObjectShape` instead. Kept for back-compat. */ type InferObjectShape = InferOutputObjectShape; /** * @deprecated Use `IsOutputOptionalKey` instead. Kept for back-compat. */ type IsOptionalKey = IsOutputOptionalKey; //#endregion export { Infer, InferInput, InferInputObjectShape, InferObjectShape, InferOutput, InferOutputObjectShape, IsInputOptionalKey, IsOptionalKey, IsOutputOptionalKey }; //# sourceMappingURL=inference-types.d.mts.map