Files
SnowLuma/.pnpm-store/v11/files/74/4281b8b4aee27371f5a0caaec8dd666ebf4f9a08781a45fd178fc27af03d8e320da367e2e69b2e2543704452df20cb46b55a9d1e9cd703e746ee17e557dfb5
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

46 lines
2.1 KiB
Plaintext

import type * as core from "../core/index.js";
import { clone, mergeDefs } from "../core/util.js";
import { visit } from "../core/visit.js";
import * as schemas from "./schemas.js";
// See `classic/in-out.ts` for why these aliases exist.
export type input<T> = core.input<T>;
export type output<T> = core.output<T>;
/** See `classic/in-out.ts`. */
function withChecks(side: core.$ZodType, checks: core.$ZodTypeDef["checks"]): core.$ZodType {
if (!checks?.length) return side;
const def = side._zod.def;
return clone(side, mergeDefs(def, { checks: [...(def.checks ?? []), ...checks] }), { parent: true });
}
/** See `classic/in-out.ts`. */
function outSide(def: core.$ZodPipeDef): core.$ZodType {
return withChecks(def.out, def.checks);
}
/** See `classic/in-out.ts`. */
function inSide(def: core.$ZodPipeDef): core.$ZodType {
return def.in._zod.traits.has("$ZodTransform") ? outSide(def) : def.in;
}
/** See `classic/in-out.ts`. */
export function input<T extends core.$ZodType>(schema: T): schemas.ZodMiniType<core.input<T>, core.input<T>> {
return visit(schema, {
pipe: (s) => inSide(s._zod.def),
// A default value belongs to the output side, so a rewritten inner type leaves it stranded. `.default()` widens the declared input type with `undefined`, and `optional` is what carries that across.
default: (s, rewritten) => (rewritten ? schemas.optional(s._zod.def.innerType) : s),
// A catch value is output-side too, but `.catch()` leaves the declared input type alone, so the inner schema stands on its own.
catch: (s, rewritten) => (rewritten ? s._zod.def.innerType : s),
}) as schemas.ZodMiniType<core.input<T>, core.input<T>>;
}
/** See `classic/in-out.ts`. */
export function output<T extends core.$ZodType>(schema: T): schemas.ZodMiniType<core.output<T>, core.output<T>> {
return visit(schema, {
pipe: (s) => outSide(s._zod.def),
// A prefault value is fed through the schema, which makes it input-side, so a rewritten inner type leaves it stranded.
prefault: (s, rewritten) => (rewritten ? s._zod.def.innerType : s),
}) as schemas.ZodMiniType<core.output<T>, core.output<T>>;
}