import type { ParseContext, SchemaNode } from "../types.js"; import { BaseSchema, ABSENT } from "./base.js"; export class NullableSchema< T extends BaseSchema = BaseSchema, > extends BaseSchema { /** @internal */ _inner: T; constructor(inner: T) { super(); this._inner = inner; } _validate(input: unknown, ctx: ParseContext): unknown { if (input === null) { return null; } return this._inner._validate(input, ctx); } _runPipeline(input: unknown, ctx: ParseContext): unknown { if (input === null) { return null; } if (this._metadata?.sensitive === true && ctx.sensitiveMode) { return super._runPipeline(input, ctx); } if ((input === undefined || input === ABSENT) && this._defaultValue !== ABSENT) { return super._runPipeline(input, ctx); } return this._inner._runPipeline(input, ctx); } _children(): BaseSchema[] { return [this._inner]; } _toNode(): SchemaNode { const node = { kind: "nullable" as const, inner: this._inner._toNode(), }; this._addDefault(node as unknown as SchemaNode); return node as unknown as SchemaNode; } }