avancement planning
This commit is contained in:
+244
-81
@@ -13,10 +13,6 @@ import * as parse from "./parse.js";
|
||||
///////////////////////////////////////////
|
||||
///////////////////////////////////////////
|
||||
|
||||
export interface RefinementCtx<T = unknown> extends core.ParsePayload<T> {
|
||||
addIssue(arg: string | core.$ZodRawIssue | Partial<core.$ZodIssueCustom>): void;
|
||||
}
|
||||
|
||||
export interface ZodType<
|
||||
out Output = unknown,
|
||||
out Input = unknown,
|
||||
@@ -59,11 +55,32 @@ export interface ZodType<
|
||||
params?: core.ParseContext<core.$ZodIssue>
|
||||
) => Promise<parse.ZodSafeParseResult<core.output<this>>>;
|
||||
|
||||
// encoding/decoding
|
||||
encode(data: core.output<this>, params?: core.ParseContext<core.$ZodIssue>): core.input<this>;
|
||||
decode(data: core.input<this>, params?: core.ParseContext<core.$ZodIssue>): core.output<this>;
|
||||
encodeAsync(data: core.output<this>, params?: core.ParseContext<core.$ZodIssue>): Promise<core.input<this>>;
|
||||
decodeAsync(data: core.input<this>, params?: core.ParseContext<core.$ZodIssue>): Promise<core.output<this>>;
|
||||
safeEncode(
|
||||
data: core.output<this>,
|
||||
params?: core.ParseContext<core.$ZodIssue>
|
||||
): parse.ZodSafeParseResult<core.input<this>>;
|
||||
safeDecode(
|
||||
data: core.input<this>,
|
||||
params?: core.ParseContext<core.$ZodIssue>
|
||||
): parse.ZodSafeParseResult<core.output<this>>;
|
||||
safeEncodeAsync(
|
||||
data: core.output<this>,
|
||||
params?: core.ParseContext<core.$ZodIssue>
|
||||
): Promise<parse.ZodSafeParseResult<core.input<this>>>;
|
||||
safeDecodeAsync(
|
||||
data: core.input<this>,
|
||||
params?: core.ParseContext<core.$ZodIssue>
|
||||
): Promise<parse.ZodSafeParseResult<core.output<this>>>;
|
||||
|
||||
// refinements
|
||||
refine(check: (arg: core.output<this>) => unknown | Promise<unknown>, params?: string | core.$ZodCustomParams): this;
|
||||
/** @deprecated Use `.check()` instead. */
|
||||
superRefine(
|
||||
refinement: (arg: core.output<this>, ctx: RefinementCtx<core.output<this>>) => void | Promise<void>
|
||||
refinement: (arg: core.output<this>, ctx: core.$RefinementCtx<core.output<this>>) => void | Promise<void>
|
||||
): this;
|
||||
overwrite(fn: (x: core.output<this>) => core.output<this>): this;
|
||||
|
||||
@@ -72,7 +89,7 @@ export interface ZodType<
|
||||
nonoptional(params?: string | core.$ZodNonOptionalParams): ZodNonOptional<this>;
|
||||
nullable(): ZodNullable<this>;
|
||||
nullish(): ZodOptional<ZodNullable<this>>;
|
||||
default(def: core.output<this>): ZodDefault<this>;
|
||||
default(def: util.NoUndefined<core.output<this>>): ZodDefault<this>;
|
||||
default(def: () => util.NoUndefined<core.output<this>>): ZodDefault<this>;
|
||||
prefault(def: () => core.input<this>): ZodPrefault<this>;
|
||||
prefault(def: core.input<this>): ZodPrefault<this>;
|
||||
@@ -80,7 +97,7 @@ export interface ZodType<
|
||||
or<T extends core.SomeType>(option: T): ZodUnion<[this, T]>;
|
||||
and<T extends core.SomeType>(incoming: T): ZodIntersection<this, T>;
|
||||
transform<NewOut>(
|
||||
transform: (arg: core.output<this>, ctx: RefinementCtx<core.output<this>>) => NewOut | Promise<NewOut>
|
||||
transform: (arg: core.output<this>, ctx: core.$RefinementCtx<core.output<this>>) => NewOut | Promise<NewOut>
|
||||
): ZodPipe<this, ZodTransform<Awaited<NewOut>, core.output<this>>>;
|
||||
catch(def: core.output<this>): ZodCatch<this>;
|
||||
catch(def: (ctx: core.$ZodCatchCtx) => core.output<this>): ZodCatch<this>;
|
||||
@@ -123,21 +140,20 @@ export interface _ZodType<out Internals extends core.$ZodTypeInternals = core.$Z
|
||||
export const ZodType: core.$constructor<ZodType> = /*@__PURE__*/ core.$constructor("ZodType", (inst, def) => {
|
||||
core.$ZodType.init(inst, def);
|
||||
inst.def = def;
|
||||
inst.type = def.type;
|
||||
Object.defineProperty(inst, "_def", { value: def });
|
||||
|
||||
// base methods
|
||||
inst.check = (...checks) => {
|
||||
return inst.clone(
|
||||
{
|
||||
...def,
|
||||
util.mergeDefs(def, {
|
||||
checks: [
|
||||
...(def.checks ?? []),
|
||||
...checks.map((ch) =>
|
||||
typeof ch === "function" ? { _zod: { check: ch, def: { check: "custom" }, onattach: [] } } : ch
|
||||
),
|
||||
],
|
||||
}
|
||||
// { parent: true }
|
||||
})
|
||||
);
|
||||
};
|
||||
inst.clone = (def, params) => core.clone(inst, def, params);
|
||||
@@ -154,6 +170,16 @@ export const ZodType: core.$constructor<ZodType> = /*@__PURE__*/ core.$construct
|
||||
inst.safeParseAsync = async (data, params) => parse.safeParseAsync(inst, data, params);
|
||||
inst.spa = inst.safeParseAsync;
|
||||
|
||||
// encoding/decoding
|
||||
inst.encode = (data, params) => parse.encode(inst, data, params);
|
||||
inst.decode = (data, params) => parse.decode(inst, data, params);
|
||||
inst.encodeAsync = async (data, params) => parse.encodeAsync(inst, data, params);
|
||||
inst.decodeAsync = async (data, params) => parse.decodeAsync(inst, data, params);
|
||||
inst.safeEncode = (data, params) => parse.safeEncode(inst, data, params);
|
||||
inst.safeDecode = (data, params) => parse.safeDecode(inst, data, params);
|
||||
inst.safeEncodeAsync = async (data, params) => parse.safeEncodeAsync(inst, data, params);
|
||||
inst.safeDecodeAsync = async (data, params) => parse.safeDecodeAsync(inst, data, params);
|
||||
|
||||
// refinements
|
||||
inst.refine = (check, params) => inst.check(refine(check, params));
|
||||
inst.superRefine = (refinement) => inst.check(superRefine(refinement));
|
||||
@@ -226,6 +252,7 @@ export interface _ZodString<T extends core.$ZodStringInternals<unknown> = core.$
|
||||
normalize(form?: "NFC" | "NFD" | "NFKC" | "NFKD" | (string & {})): this;
|
||||
toLowerCase(): this;
|
||||
toUpperCase(): this;
|
||||
slugify(): this;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
@@ -255,6 +282,7 @@ export const _ZodString: core.$constructor<_ZodString> = /*@__PURE__*/ core.$con
|
||||
inst.normalize = (...args) => inst.check(checks.normalize(...args));
|
||||
inst.toLowerCase = () => inst.check(checks.toLowerCase());
|
||||
inst.toUpperCase = () => inst.check(checks.toUpperCase());
|
||||
inst.slugify = () => inst.check(checks.slugify());
|
||||
});
|
||||
|
||||
export interface ZodString extends _ZodString<core.$ZodStringInternals<string>> {
|
||||
@@ -454,6 +482,14 @@ export function url(params?: string | core.$ZodURLParams): ZodURL {
|
||||
return core._url(ZodURL, params);
|
||||
}
|
||||
|
||||
export function httpUrl(params?: string | Omit<core.$ZodURLParams, "protocol" | "hostname">): ZodURL {
|
||||
return core._url(ZodURL, {
|
||||
protocol: /^https?$/,
|
||||
hostname: core.regexes.domain,
|
||||
...util.normalizeParams(params),
|
||||
});
|
||||
}
|
||||
|
||||
// ZodEmoji
|
||||
export interface ZodEmoji extends ZodStringFormat<"emoji"> {
|
||||
_zod: core.$ZodEmojiInternals;
|
||||
@@ -580,6 +616,19 @@ export function ipv4(params?: string | core.$ZodIPv4Params): ZodIPv4 {
|
||||
return core._ipv4(ZodIPv4, params);
|
||||
}
|
||||
|
||||
// ZodMAC
|
||||
export interface ZodMAC extends ZodStringFormat<"mac"> {
|
||||
_zod: core.$ZodMACInternals;
|
||||
}
|
||||
export const ZodMAC: core.$constructor<ZodMAC> = /*@__PURE__*/ core.$constructor("ZodMAC", (inst, def) => {
|
||||
// ZodStringFormat.init(inst, def);
|
||||
core.$ZodMAC.init(inst, def);
|
||||
ZodStringFormat.init(inst, def);
|
||||
});
|
||||
export function mac(params?: string | core.$ZodMACParams): ZodMAC {
|
||||
return core._mac(ZodMAC, params);
|
||||
}
|
||||
|
||||
// ZodIPv6
|
||||
export interface ZodIPv6 extends ZodStringFormat<"ipv6"> {
|
||||
_zod: core.$ZodIPv6Internals;
|
||||
@@ -698,6 +747,27 @@ export function stringFormat<Format extends string>(
|
||||
return core._stringFormat(ZodCustomStringFormat, format, fnOrRegex, _params) as any;
|
||||
}
|
||||
|
||||
export function hostname(_params?: string | core.$ZodStringFormatParams): ZodCustomStringFormat<"hostname"> {
|
||||
return core._stringFormat(ZodCustomStringFormat, "hostname", core.regexes.hostname, _params) as any;
|
||||
}
|
||||
|
||||
export function hex(_params?: string | core.$ZodStringFormatParams): ZodCustomStringFormat<"hex"> {
|
||||
return core._stringFormat(ZodCustomStringFormat, "hex", core.regexes.hex, _params) as any;
|
||||
}
|
||||
|
||||
export function hash<Alg extends util.HashAlgorithm, Enc extends util.HashEncoding = "hex">(
|
||||
alg: Alg,
|
||||
params?: {
|
||||
enc?: Enc;
|
||||
} & core.$ZodStringFormatParams
|
||||
): ZodCustomStringFormat<`${Alg}_${Enc}`> {
|
||||
const enc = params?.enc ?? "hex";
|
||||
const format = `${alg}_${enc}` as const;
|
||||
const regex = core.regexes[format as keyof typeof core.regexes] as RegExp;
|
||||
if (!regex) throw new Error(`Unrecognized hash format: ${format}`);
|
||||
return core._stringFormat(ZodCustomStringFormat, format, regex, params) as any;
|
||||
}
|
||||
|
||||
// ZodNumber
|
||||
export interface _ZodNumber<Internals extends core.$ZodNumberInternals = core.$ZodNumberInternals>
|
||||
extends _ZodType<Internals> {
|
||||
@@ -737,6 +807,7 @@ export interface ZodNumber extends _ZodNumber<core.$ZodNumberInternals<number>>
|
||||
|
||||
export const ZodNumber: core.$constructor<ZodNumber> = /*@__PURE__*/ core.$constructor("ZodNumber", (inst, def) => {
|
||||
core.$ZodNumber.init(inst, def);
|
||||
|
||||
ZodType.init(inst, def);
|
||||
|
||||
inst.gt = (value, params) => inst.check(checks.gt(value, params));
|
||||
@@ -1039,13 +1110,23 @@ export function array<T extends core.SomeType>(element: T, params?: string | cor
|
||||
}
|
||||
|
||||
// .keyof
|
||||
export function keyof<T extends ZodObject>(schema: T): ZodLiteral<Exclude<keyof T["_zod"]["output"], symbol>> {
|
||||
export function keyof<T extends ZodObject>(schema: T): ZodEnum<util.KeysEnum<T["_zod"]["output"]>> {
|
||||
const shape = schema._zod.def.shape;
|
||||
return literal(Object.keys(shape)) as any;
|
||||
return _enum(Object.keys(shape)) as any;
|
||||
}
|
||||
|
||||
// ZodObject
|
||||
|
||||
export type SafeExtendShape<Base extends core.$ZodShape, Ext extends core.$ZodLooseShape> = {
|
||||
[K in keyof Ext]: K extends keyof Base
|
||||
? core.output<Ext[K]> extends core.output<Base[K]>
|
||||
? core.input<Ext[K]> extends core.input<Base[K]>
|
||||
? Ext[K]
|
||||
: never
|
||||
: never
|
||||
: Ext[K];
|
||||
};
|
||||
|
||||
export interface ZodObject<
|
||||
/** @ts-ignore Cast variance */
|
||||
out Shape extends core.$ZodShape = core.$ZodLooseShape,
|
||||
@@ -1069,22 +1150,14 @@ export interface ZodObject<
|
||||
/** This is the default behavior. This method call is likely unnecessary. */
|
||||
strip(): ZodObject<Shape, core.$strip>;
|
||||
|
||||
extend<U extends core.$ZodLooseShape & Partial<Record<keyof Shape, core.SomeType>>>(
|
||||
shape: U
|
||||
extend<U extends core.$ZodLooseShape>(shape: U): ZodObject<util.Extend<Shape, U>, Config>;
|
||||
|
||||
safeExtend<U extends core.$ZodLooseShape>(
|
||||
shape: SafeExtendShape<Shape, U> & Partial<Record<keyof Shape, core.SomeType>>
|
||||
): ZodObject<util.Extend<Shape, U>, Config>;
|
||||
|
||||
/**
|
||||
* @deprecated Use spread syntax and the `.shape` property to combine two object schemas:
|
||||
*
|
||||
* ```ts
|
||||
* const A = z.object({ a: z.string() });
|
||||
* const B = z.object({ b: z.number() });
|
||||
*
|
||||
* const C = z.object({
|
||||
* ...A.shape,
|
||||
* ...B.shape
|
||||
* });
|
||||
* ```
|
||||
* @deprecated Use [`A.extend(B.shape)`](https://zod.dev/api?id=extend) instead.
|
||||
*/
|
||||
merge<U extends ZodObject>(other: U): ZodObject<util.Extend<Shape, U["shape"]>, U["_zod"]["config"]>;
|
||||
|
||||
@@ -1134,14 +1207,16 @@ export interface ZodObject<
|
||||
}
|
||||
|
||||
export const ZodObject: core.$constructor<ZodObject> = /*@__PURE__*/ core.$constructor("ZodObject", (inst, def) => {
|
||||
core.$ZodObject.init(inst, def);
|
||||
core.$ZodObjectJIT.init(inst, def);
|
||||
ZodType.init(inst, def);
|
||||
|
||||
util.defineLazy(inst, "shape", () => def.shape);
|
||||
util.defineLazy(inst, "shape", () => {
|
||||
return def.shape;
|
||||
});
|
||||
|
||||
inst.keyof = () => _enum(Object.keys(inst._zod.def.shape)) as any;
|
||||
inst.catchall = (catchall) => inst.clone({ ...inst._zod.def, catchall: catchall as any as core.$ZodType }) as any;
|
||||
inst.passthrough = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
|
||||
// inst.nonstrict = () => inst.clone({ ...inst._zod.def, catchall: api.unknown() });
|
||||
inst.loose = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
|
||||
inst.strict = () => inst.clone({ ...inst._zod.def, catchall: never() });
|
||||
inst.strip = () => inst.clone({ ...inst._zod.def, catchall: undefined });
|
||||
@@ -1149,6 +1224,9 @@ export const ZodObject: core.$constructor<ZodObject> = /*@__PURE__*/ core.$const
|
||||
inst.extend = (incoming: any) => {
|
||||
return util.extend(inst, incoming);
|
||||
};
|
||||
inst.safeExtend = (incoming: any) => {
|
||||
return util.safeExtend(inst, incoming);
|
||||
};
|
||||
inst.merge = (other) => util.merge(inst, other);
|
||||
inst.pick = (mask) => util.pick(inst, mask);
|
||||
inst.omit = (mask) => util.omit(inst, mask);
|
||||
@@ -1162,10 +1240,7 @@ export function object<T extends core.$ZodLooseShape = Partial<Record<never, cor
|
||||
): ZodObject<util.Writeable<T>, core.$strip> {
|
||||
const def: core.$ZodObjectDef = {
|
||||
type: "object",
|
||||
get shape() {
|
||||
util.assignProp(this, "shape", { ...shape });
|
||||
return this.shape;
|
||||
},
|
||||
shape: shape ?? {},
|
||||
...util.normalizeParams(params),
|
||||
};
|
||||
return new ZodObject(def) as any;
|
||||
@@ -1179,10 +1254,7 @@ export function strictObject<T extends core.$ZodLooseShape>(
|
||||
): ZodObject<T, core.$strict> {
|
||||
return new ZodObject({
|
||||
type: "object",
|
||||
get shape() {
|
||||
util.assignProp(this, "shape", { ...shape });
|
||||
return this.shape;
|
||||
},
|
||||
shape,
|
||||
catchall: never(),
|
||||
...util.normalizeParams(params),
|
||||
}) as any;
|
||||
@@ -1196,10 +1268,7 @@ export function looseObject<T extends core.$ZodLooseShape>(
|
||||
): ZodObject<T, core.$loose> {
|
||||
return new ZodObject({
|
||||
type: "object",
|
||||
get shape() {
|
||||
util.assignProp(this, "shape", { ...shape });
|
||||
return this.shape;
|
||||
},
|
||||
shape,
|
||||
catchall: unknown(),
|
||||
...util.normalizeParams(params),
|
||||
}) as any;
|
||||
@@ -1229,10 +1298,13 @@ export function union<const T extends readonly core.SomeType[]>(
|
||||
}
|
||||
|
||||
// ZodDiscriminatedUnion
|
||||
export interface ZodDiscriminatedUnion<Options extends readonly core.SomeType[] = readonly core.$ZodType[]>
|
||||
extends ZodUnion<Options>,
|
||||
core.$ZodDiscriminatedUnion<Options> {
|
||||
_zod: core.$ZodDiscriminatedUnionInternals<Options>;
|
||||
export interface ZodDiscriminatedUnion<
|
||||
Options extends readonly core.SomeType[] = readonly core.$ZodType[],
|
||||
Disc extends string = string,
|
||||
> extends ZodUnion<Options>,
|
||||
core.$ZodDiscriminatedUnion<Options, Disc> {
|
||||
_zod: core.$ZodDiscriminatedUnionInternals<Options, Disc>;
|
||||
def: core.$ZodDiscriminatedUnionDef<Options, Disc>;
|
||||
}
|
||||
export const ZodDiscriminatedUnion: core.$constructor<ZodDiscriminatedUnion> = /*@__PURE__*/ core.$constructor(
|
||||
"ZodDiscriminatedUnion",
|
||||
@@ -1244,11 +1316,12 @@ export const ZodDiscriminatedUnion: core.$constructor<ZodDiscriminatedUnion> = /
|
||||
|
||||
export function discriminatedUnion<
|
||||
Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]],
|
||||
Disc extends string,
|
||||
>(
|
||||
discriminator: string,
|
||||
discriminator: Disc,
|
||||
options: Types,
|
||||
params?: string | core.$ZodDiscriminatedUnionParams
|
||||
): ZodDiscriminatedUnion<Types> {
|
||||
): ZodDiscriminatedUnion<Types, Disc> {
|
||||
// const [options, params] = args;
|
||||
return new ZodDiscriminatedUnion({
|
||||
type: "union",
|
||||
@@ -1360,9 +1433,11 @@ export function partialRecord<Key extends core.$ZodRecordKey, Value extends core
|
||||
valueType: Value,
|
||||
params?: string | core.$ZodRecordParams
|
||||
): ZodRecord<Key & core.$partial, Value> {
|
||||
const k = core.clone(keyType);
|
||||
k._zod.values = undefined;
|
||||
return new ZodRecord({
|
||||
type: "record",
|
||||
keyType: union([keyType, never()]),
|
||||
keyType: k,
|
||||
valueType: valueType as any,
|
||||
...util.normalizeParams(params),
|
||||
}) as any;
|
||||
@@ -1400,7 +1475,6 @@ export interface ZodSet<T extends core.SomeType = core.$ZodType>
|
||||
extends _ZodType<core.$ZodSetInternals<T>>,
|
||||
core.$ZodSet<T> {
|
||||
min(minSize: number, params?: string | core.$ZodCheckMinSizeParams): this;
|
||||
/** */
|
||||
nonempty(params?: string | core.$ZodCheckMinSizeParams): this;
|
||||
max(maxSize: number, params?: string | core.$ZodCheckMaxSizeParams): this;
|
||||
size(size: number, params?: string | core.$ZodCheckSizeEqualsParams): this;
|
||||
@@ -1583,7 +1657,11 @@ export const ZodTransform: core.$constructor<ZodTransform> = /*@__PURE__*/ core.
|
||||
ZodType.init(inst, def);
|
||||
|
||||
inst._zod.parse = (payload, _ctx) => {
|
||||
(payload as RefinementCtx).addIssue = (issue) => {
|
||||
if (_ctx.direction === "backward") {
|
||||
throw new core.$ZodEncodeError(inst.constructor.name);
|
||||
}
|
||||
|
||||
(payload as core.$RefinementCtx).addIssue = (issue) => {
|
||||
if (typeof issue === "string") {
|
||||
payload.issues.push(util.issue(issue, payload.value, def));
|
||||
} else {
|
||||
@@ -1594,7 +1672,7 @@ export const ZodTransform: core.$constructor<ZodTransform> = /*@__PURE__*/ core.
|
||||
_issue.code ??= "custom";
|
||||
_issue.input ??= payload.value;
|
||||
_issue.inst ??= inst;
|
||||
_issue.continue ??= true;
|
||||
// _issue.continue ??= true;
|
||||
payload.issues.push(util.issue(_issue));
|
||||
}
|
||||
};
|
||||
@@ -1696,7 +1774,7 @@ export function _default<T extends core.SomeType>(
|
||||
type: "default",
|
||||
innerType: innerType as any as core.$ZodType,
|
||||
get defaultValue() {
|
||||
return typeof defaultValue === "function" ? (defaultValue as Function)() : defaultValue;
|
||||
return typeof defaultValue === "function" ? (defaultValue as Function)() : util.shallowClone(defaultValue);
|
||||
},
|
||||
}) as any;
|
||||
}
|
||||
@@ -1724,7 +1802,7 @@ export function prefault<T extends core.SomeType>(
|
||||
type: "prefault",
|
||||
innerType: innerType as any as core.$ZodType,
|
||||
get defaultValue() {
|
||||
return typeof defaultValue === "function" ? (defaultValue as Function)() : defaultValue;
|
||||
return typeof defaultValue === "function" ? (defaultValue as Function)() : util.shallowClone(defaultValue);
|
||||
},
|
||||
}) as any;
|
||||
}
|
||||
@@ -1845,15 +1923,48 @@ export function pipe(in_: core.SomeType, out: core.SomeType) {
|
||||
});
|
||||
}
|
||||
|
||||
// ZodCodec
|
||||
export interface ZodCodec<A extends core.SomeType = core.$ZodType, B extends core.SomeType = core.$ZodType>
|
||||
extends ZodPipe<A, B>,
|
||||
core.$ZodCodec<A, B> {
|
||||
_zod: core.$ZodCodecInternals<A, B>;
|
||||
def: core.$ZodCodecDef<A, B>;
|
||||
}
|
||||
export const ZodCodec: core.$constructor<ZodCodec> = /*@__PURE__*/ core.$constructor("ZodCodec", (inst, def) => {
|
||||
ZodPipe.init(inst, def);
|
||||
core.$ZodCodec.init(inst, def);
|
||||
});
|
||||
|
||||
export function codec<const A extends core.SomeType, B extends core.SomeType = core.$ZodType>(
|
||||
in_: A,
|
||||
out: B,
|
||||
params: {
|
||||
decode: (value: core.output<A>, payload: core.ParsePayload<core.output<A>>) => core.util.MaybeAsync<core.input<B>>;
|
||||
encode: (value: core.input<B>, payload: core.ParsePayload<core.input<B>>) => core.util.MaybeAsync<core.output<A>>;
|
||||
}
|
||||
): ZodCodec<A, B> {
|
||||
return new ZodCodec({
|
||||
type: "pipe",
|
||||
in: in_ as any as core.$ZodType,
|
||||
out: out as any as core.$ZodType,
|
||||
transform: params.decode as any,
|
||||
reverseTransform: params.encode as any,
|
||||
}) as any;
|
||||
}
|
||||
|
||||
// ZodReadonly
|
||||
export interface ZodReadonly<T extends core.SomeType = core.$ZodType>
|
||||
extends _ZodType<core.$ZodReadonlyInternals<T>>,
|
||||
core.$ZodReadonly<T> {}
|
||||
core.$ZodReadonly<T> {
|
||||
unwrap(): T;
|
||||
}
|
||||
export const ZodReadonly: core.$constructor<ZodReadonly> = /*@__PURE__*/ core.$constructor(
|
||||
"ZodReadonly",
|
||||
(inst, def) => {
|
||||
core.$ZodReadonly.init(inst, def);
|
||||
ZodType.init(inst, def);
|
||||
|
||||
inst.unwrap = () => inst._zod.def.innerType;
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1927,6 +2038,71 @@ export function promise<T extends core.SomeType>(innerType: T): ZodPromise<T> {
|
||||
}) as any;
|
||||
}
|
||||
|
||||
// ZodFunction
|
||||
export interface ZodFunction<
|
||||
Args extends core.$ZodFunctionIn = core.$ZodFunctionIn,
|
||||
Returns extends core.$ZodFunctionOut = core.$ZodFunctionOut,
|
||||
> extends _ZodType<core.$ZodFunctionInternals<Args, Returns>>,
|
||||
core.$ZodFunction<Args, Returns> {
|
||||
_def: core.$ZodFunctionDef<Args, Returns>;
|
||||
_input: core.$InferInnerFunctionType<Args, Returns>;
|
||||
_output: core.$InferOuterFunctionType<Args, Returns>;
|
||||
|
||||
input<const Items extends util.TupleItems, const Rest extends core.$ZodFunctionOut = core.$ZodFunctionOut>(
|
||||
args: Items,
|
||||
rest?: Rest
|
||||
): ZodFunction<core.$ZodTuple<Items, Rest>, Returns>;
|
||||
input<NewArgs extends core.$ZodFunctionIn>(args: NewArgs): ZodFunction<NewArgs, Returns>;
|
||||
input(...args: any[]): ZodFunction<any, Returns>;
|
||||
|
||||
output<NewReturns extends core.$ZodType>(output: NewReturns): ZodFunction<Args, NewReturns>;
|
||||
}
|
||||
|
||||
export const ZodFunction: core.$constructor<ZodFunction> = /*@__PURE__*/ core.$constructor(
|
||||
"ZodFunction",
|
||||
(inst, def) => {
|
||||
core.$ZodFunction.init(inst, def);
|
||||
ZodType.init(inst, def);
|
||||
}
|
||||
);
|
||||
|
||||
export function _function(): ZodFunction;
|
||||
export function _function<const In extends ReadonlyArray<core.$ZodType>>(params: {
|
||||
input: In;
|
||||
}): ZodFunction<ZodTuple<In, null>, core.$ZodFunctionOut>;
|
||||
export function _function<
|
||||
const In extends ReadonlyArray<core.$ZodType>,
|
||||
const Out extends core.$ZodFunctionOut = core.$ZodFunctionOut,
|
||||
>(params: {
|
||||
input: In;
|
||||
output: Out;
|
||||
}): ZodFunction<ZodTuple<In, null>, Out>;
|
||||
export function _function<const In extends core.$ZodFunctionIn = core.$ZodFunctionIn>(params: {
|
||||
input: In;
|
||||
}): ZodFunction<In, core.$ZodFunctionOut>;
|
||||
export function _function<const Out extends core.$ZodFunctionOut = core.$ZodFunctionOut>(params: {
|
||||
output: Out;
|
||||
}): ZodFunction<core.$ZodFunctionIn, Out>;
|
||||
export function _function<
|
||||
In extends core.$ZodFunctionIn = core.$ZodFunctionIn,
|
||||
Out extends core.$ZodType = core.$ZodType,
|
||||
>(params?: {
|
||||
input: In;
|
||||
output: Out;
|
||||
}): ZodFunction<In, Out>;
|
||||
export function _function(params?: {
|
||||
output?: core.$ZodType;
|
||||
input?: core.$ZodFunctionArgs | Array<core.$ZodType>;
|
||||
}): ZodFunction {
|
||||
return new ZodFunction({
|
||||
type: "function",
|
||||
input: Array.isArray(params?.input) ? tuple(params?.input as any) : (params?.input ?? array(unknown())),
|
||||
output: params?.output ?? unknown(),
|
||||
});
|
||||
}
|
||||
|
||||
export { _function as function };
|
||||
|
||||
// ZodCustom
|
||||
export interface ZodCustom<O = unknown, I = unknown>
|
||||
extends _ZodType<core.$ZodCustomInternals<O, I>>,
|
||||
@@ -1962,28 +2138,16 @@ export function refine<T>(
|
||||
}
|
||||
|
||||
// superRefine
|
||||
export function superRefine<T>(fn: (arg: T, payload: RefinementCtx<T>) => void | Promise<void>): core.$ZodCheck<T> {
|
||||
const ch = check<T>((payload) => {
|
||||
(payload as RefinementCtx).addIssue = (issue) => {
|
||||
if (typeof issue === "string") {
|
||||
payload.issues.push(util.issue(issue, payload.value, ch._zod.def));
|
||||
} else {
|
||||
// for Zod 3 backwards compatibility
|
||||
const _issue: any = issue;
|
||||
if (_issue.fatal) _issue.continue = false;
|
||||
_issue.code ??= "custom";
|
||||
_issue.input ??= payload.value;
|
||||
_issue.inst ??= ch;
|
||||
_issue.continue ??= !ch._zod.def.abort;
|
||||
payload.issues.push(util.issue(_issue));
|
||||
}
|
||||
};
|
||||
|
||||
return fn(payload.value, payload as RefinementCtx<T>);
|
||||
});
|
||||
return ch;
|
||||
export function superRefine<T>(
|
||||
fn: (arg: T, payload: core.$RefinementCtx<T>) => void | Promise<void>
|
||||
): core.$ZodCheck<T> {
|
||||
return core._superRefine(fn);
|
||||
}
|
||||
|
||||
// Re-export describe and meta from core
|
||||
export const describe = core.describe;
|
||||
export const meta = core.meta;
|
||||
|
||||
type ZodInstanceOfParams = core.Params<
|
||||
ZodCustom,
|
||||
core.$ZodIssueCustom,
|
||||
@@ -2008,15 +2172,14 @@ function _instanceof<T extends typeof util.Class>(
|
||||
export { _instanceof as instanceof };
|
||||
|
||||
// stringbool
|
||||
export const stringbool: (
|
||||
_params?: string | core.$ZodStringBoolParams
|
||||
) => ZodPipe<ZodPipe<ZodString, ZodTransform<boolean, string>>, ZodBoolean> = (...args) =>
|
||||
export const stringbool: (_params?: string | core.$ZodStringBoolParams) => ZodCodec<ZodString, ZodBoolean> = (
|
||||
...args
|
||||
) =>
|
||||
core._stringbool(
|
||||
{
|
||||
Pipe: ZodPipe,
|
||||
Codec: ZodCodec,
|
||||
Boolean: ZodBoolean,
|
||||
String: ZodString,
|
||||
Transform: ZodTransform,
|
||||
},
|
||||
...args
|
||||
) as any;
|
||||
@@ -2047,7 +2210,7 @@ export function json(params?: string | core.$ZodCustomParams): ZodJSONSchema {
|
||||
|
||||
// /** @deprecated Use `z.pipe()` and `z.transform()` instead. */
|
||||
export function preprocess<A, U extends core.SomeType, B = unknown>(
|
||||
fn: (arg: B, ctx: RefinementCtx) => A,
|
||||
fn: (arg: B, ctx: core.$RefinementCtx) => A,
|
||||
schema: U
|
||||
): ZodPipe<ZodTransform<A, B>, U> {
|
||||
return pipe(transform(fn as any), schema as any) as any;
|
||||
|
||||
Reference in New Issue
Block a user