Files
SnowLuma/.pnpm-store/v11/files/94/9c7d11dcabe153c888f0284f2cca7c58f63a789357626ed89fd163a965d38e13d1adc5fa003b7aad63e1750b13496b8d0f6f119e0cbde9dceb88ae6e475182
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

37 lines
2.1 KiB
Plaintext

import { clone, mergeDefs } from "../core/util.js";
import { visit } from "../core/visit.js";
import * as schemas from "./schemas.js";
/** Appends a pipe's own checks to the side that replaces it, mirroring how `.check()` clones. */
function withChecks(side, checks) {
if (!checks?.length)
return side;
const def = side._zod.def;
return clone(side, mergeDefs(def, { checks: [...(def.checks ?? []), ...checks] }), { parent: true });
}
/** The out side, carrying the pipe's own checks: those run against the decoded value, which is what `out` produces. */
function outSide(def) {
return withChecks(def.out, def.checks);
}
/** The in side. `z.preprocess` pipes a transform into a schema, and a bare transform validates nothing, so the schema it feeds is the real input side — the resolution `toJSONSchema` already makes for this case. */
function inSide(def) {
return def.in._zod.traits.has("$ZodTransform") ? outSide(def) : def.in;
}
/** Returns a copy of the schema with every pipe replaced by its input side. A codec's checks are dropped: they constrain the decoded value the input side never produces. */
export function input(schema) {
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),
});
}
/** Returns a copy of the schema with every pipe replaced by its output side, carrying over the pipe's own checks. */
export function output(schema) {
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),
});
}