zod 4.2.1 → 4.3.0-canary.20251216T031750
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/v4/classic/tests/function.test.ts +52 -0
- package/src/v4/classic/tests/string.test.ts +2 -0
- package/src/v4/core/regexes.ts +2 -1
- package/src/v4/core/schemas.ts +1 -1
- package/v4/core/regexes.cjs +2 -1
- package/v4/core/regexes.js +2 -1
- package/v4/core/schemas.d.cts +1 -1
- package/v4/core/schemas.d.ts +1 -1
package/package.json
CHANGED
|
@@ -274,6 +274,58 @@ test("function with async refinements", async () => {
|
|
|
274
274
|
expect(results).toEqual(["fail", "success"]);
|
|
275
275
|
});
|
|
276
276
|
|
|
277
|
+
test("implement async with transforms", async () => {
|
|
278
|
+
const typeGuard = (data: string): data is "1234" => data === "1234";
|
|
279
|
+
const codeSchema = z.string().transform((data, ctx) => {
|
|
280
|
+
if (typeGuard(data)) {
|
|
281
|
+
return data;
|
|
282
|
+
} else {
|
|
283
|
+
ctx.addIssue({
|
|
284
|
+
code: z.ZodIssueCode.custom,
|
|
285
|
+
message: "Invalid code",
|
|
286
|
+
});
|
|
287
|
+
return z.NEVER;
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
const inputSchema = z.object({
|
|
291
|
+
code: codeSchema,
|
|
292
|
+
});
|
|
293
|
+
const outputSchema = z.object({
|
|
294
|
+
data: z.array(z.string()).default([]),
|
|
295
|
+
});
|
|
296
|
+
const fnImplementation = async (data: z.infer<typeof inputSchema>): Promise<z.infer<typeof outputSchema>> => {
|
|
297
|
+
return {
|
|
298
|
+
data: [data.code],
|
|
299
|
+
};
|
|
300
|
+
};
|
|
301
|
+
const schema = z.function().input([inputSchema]).output(outputSchema);
|
|
302
|
+
|
|
303
|
+
const func = schema.implementAsync(fnImplementation);
|
|
304
|
+
type TheInterface = {
|
|
305
|
+
myFunction: (data: z.infer<typeof inputSchema>) => Promise<z.infer<typeof outputSchema>>;
|
|
306
|
+
};
|
|
307
|
+
const theImplementation: TheInterface = {
|
|
308
|
+
myFunction: func,
|
|
309
|
+
};
|
|
310
|
+
const results = [];
|
|
311
|
+
try {
|
|
312
|
+
await theImplementation.myFunction({
|
|
313
|
+
code: "1234",
|
|
314
|
+
});
|
|
315
|
+
results.push("success");
|
|
316
|
+
} catch (_) {
|
|
317
|
+
results.push("fail");
|
|
318
|
+
}
|
|
319
|
+
try {
|
|
320
|
+
await func({ data: "asdflkjasdflkjsf" } as any);
|
|
321
|
+
results.push("success");
|
|
322
|
+
} catch (_) {
|
|
323
|
+
results.push("fail");
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
expect(results).toEqual(["success", "fail"]);
|
|
327
|
+
});
|
|
328
|
+
|
|
277
329
|
test("non async function with async refinements should fail", async () => {
|
|
278
330
|
const func = z
|
|
279
331
|
.function()
|
|
@@ -1023,6 +1023,8 @@ test("E.164 validation", () => {
|
|
|
1023
1023
|
"+1 555 555 555", // space after plus sign
|
|
1024
1024
|
"+1555 555 555", // space between numbers
|
|
1025
1025
|
"+1555+555", // multiple plus signs
|
|
1026
|
+
"+0000000", // leading zero country code
|
|
1027
|
+
"+0123456789", // leading zero with more digits
|
|
1026
1028
|
"+1555555555555555", // too long
|
|
1027
1029
|
"+115abc55", // non numeric characters in number part
|
|
1028
1030
|
"+1555555 ", // space after number
|
package/src/v4/core/regexes.ts
CHANGED
|
@@ -82,7 +82,8 @@ export const hostname: RegExp =
|
|
|
82
82
|
export const domain: RegExp = /^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
|
|
83
83
|
|
|
84
84
|
// https://blog.stevenlevithan.com/archives/validate-phone-number#r4-3 (regex sans spaces)
|
|
85
|
-
|
|
85
|
+
// E.164: leading digit must be 1-9; total digits (excluding '+') between 7-15
|
|
86
|
+
export const e164: RegExp = /^\+[1-9]\d{6,14}$/;
|
|
86
87
|
|
|
87
88
|
// const dateSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`;
|
|
88
89
|
const dateSource = `(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))`;
|
package/src/v4/core/schemas.ts
CHANGED
|
@@ -4056,7 +4056,7 @@ export type $InferOuterFunctionType<Args extends $ZodFunctionIn, Returns extends
|
|
|
4056
4056
|
|
|
4057
4057
|
export type $InferOuterFunctionTypeAsync<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (
|
|
4058
4058
|
...args: $ZodFunctionIn extends Args ? never[] : core.input<Args>
|
|
4059
|
-
) =>
|
|
4059
|
+
) => Promise<core.output<Returns>>;
|
|
4060
4060
|
|
|
4061
4061
|
export interface $ZodFunctionDef<
|
|
4062
4062
|
In extends $ZodFunctionIn = $ZodFunctionIn,
|
package/v4/core/regexes.cjs
CHANGED
|
@@ -85,7 +85,8 @@ exports.base64url = /^[A-Za-z0-9_-]*$/;
|
|
|
85
85
|
exports.hostname = /^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/;
|
|
86
86
|
exports.domain = /^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
|
|
87
87
|
// https://blog.stevenlevithan.com/archives/validate-phone-number#r4-3 (regex sans spaces)
|
|
88
|
-
|
|
88
|
+
// E.164: leading digit must be 1-9; total digits (excluding '+') between 7-15
|
|
89
|
+
exports.e164 = /^\+[1-9]\d{6,14}$/;
|
|
89
90
|
// const dateSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`;
|
|
90
91
|
const dateSource = `(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))`;
|
|
91
92
|
exports.date = new RegExp(`^${dateSource}$`);
|
package/v4/core/regexes.js
CHANGED
|
@@ -53,7 +53,8 @@ export const base64url = /^[A-Za-z0-9_-]*$/;
|
|
|
53
53
|
export const hostname = /^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/;
|
|
54
54
|
export const domain = /^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
|
|
55
55
|
// https://blog.stevenlevithan.com/archives/validate-phone-number#r4-3 (regex sans spaces)
|
|
56
|
-
|
|
56
|
+
// E.164: leading digit must be 1-9; total digits (excluding '+') between 7-15
|
|
57
|
+
export const e164 = /^\+[1-9]\d{6,14}$/;
|
|
57
58
|
// const dateSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`;
|
|
58
59
|
const dateSource = `(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))`;
|
|
59
60
|
export const date = /*@__PURE__*/ new RegExp(`^${dateSource}$`);
|
package/v4/core/schemas.d.cts
CHANGED
|
@@ -1054,7 +1054,7 @@ export type $ZodFunctionOut = $ZodType;
|
|
|
1054
1054
|
export type $InferInnerFunctionType<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.output<Args>) => core.input<Returns>;
|
|
1055
1055
|
export type $InferInnerFunctionTypeAsync<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.output<Args>) => util.MaybeAsync<core.input<Returns>>;
|
|
1056
1056
|
export type $InferOuterFunctionType<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.input<Args>) => core.output<Returns>;
|
|
1057
|
-
export type $InferOuterFunctionTypeAsync<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.input<Args>) =>
|
|
1057
|
+
export type $InferOuterFunctionTypeAsync<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.input<Args>) => Promise<core.output<Returns>>;
|
|
1058
1058
|
export interface $ZodFunctionDef<In extends $ZodFunctionIn = $ZodFunctionIn, Out extends $ZodFunctionOut = $ZodFunctionOut> extends $ZodTypeDef {
|
|
1059
1059
|
type: "function";
|
|
1060
1060
|
input: In;
|
package/v4/core/schemas.d.ts
CHANGED
|
@@ -1054,7 +1054,7 @@ export type $ZodFunctionOut = $ZodType;
|
|
|
1054
1054
|
export type $InferInnerFunctionType<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.output<Args>) => core.input<Returns>;
|
|
1055
1055
|
export type $InferInnerFunctionTypeAsync<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.output<Args>) => util.MaybeAsync<core.input<Returns>>;
|
|
1056
1056
|
export type $InferOuterFunctionType<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.input<Args>) => core.output<Returns>;
|
|
1057
|
-
export type $InferOuterFunctionTypeAsync<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.input<Args>) =>
|
|
1057
|
+
export type $InferOuterFunctionTypeAsync<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.input<Args>) => Promise<core.output<Returns>>;
|
|
1058
1058
|
export interface $ZodFunctionDef<In extends $ZodFunctionIn = $ZodFunctionIn, Out extends $ZodFunctionOut = $ZodFunctionOut> extends $ZodTypeDef {
|
|
1059
1059
|
type: "function";
|
|
1060
1060
|
input: In;
|