zod 4.1.0-canary.20250821T014930 → 4.1.0-canary.20250823T071040
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/index.cjs +17 -7
- package/package.json +1 -1
- package/src/v4/classic/external.ts +0 -1
- package/src/v4/classic/parse.ts +49 -0
- package/src/v4/classic/schemas.ts +145 -7
- package/src/v4/classic/tests/catch.test.ts +25 -0
- package/src/v4/classic/tests/codec-examples.test.ts +538 -0
- package/src/v4/classic/tests/codec.test.ts +532 -0
- package/src/v4/classic/tests/continuability.test.ts +1 -1
- package/src/v4/classic/tests/default.test.ts +32 -0
- package/src/v4/classic/tests/firstparty.test.ts +4 -0
- package/src/v4/classic/tests/function.test.ts +31 -31
- package/src/v4/classic/tests/hash.test.ts +68 -0
- package/src/v4/classic/tests/nonoptional.test.ts +15 -0
- package/src/v4/classic/tests/object.test.ts +31 -0
- package/src/v4/classic/tests/pipe.test.ts +25 -5
- package/src/v4/classic/tests/prefault.test.ts +25 -0
- package/src/v4/classic/tests/preprocess.test.ts +1 -6
- package/src/v4/classic/tests/refine.test.ts +76 -3
- package/src/v4/classic/tests/string-formats.test.ts +16 -0
- package/src/v4/classic/tests/string.test.ts +82 -1
- package/src/v4/classic/tests/stringbool.test.ts +40 -0
- package/src/v4/classic/tests/template-literal.test.ts +1 -1
- package/src/v4/classic/tests/to-json-schema.test.ts +21 -2
- package/src/v4/classic/tests/transform.test.ts +7 -0
- package/src/v4/classic/tests/union.test.ts +1 -1
- package/src/v4/core/api.ts +25 -35
- package/src/v4/core/core.ts +7 -26
- package/src/v4/core/index.ts +0 -1
- package/src/v4/core/json-schema.ts +1 -0
- package/src/v4/core/parse.ts +101 -0
- package/src/v4/core/regexes.ts +40 -1
- package/src/v4/core/schemas.ts +521 -129
- package/src/v4/core/to-json-schema.ts +43 -8
- package/src/v4/core/util.ts +73 -0
- package/src/v4/mini/external.ts +0 -1
- package/src/v4/mini/parse.ts +14 -1
- package/src/v4/mini/schemas.ts +153 -12
- package/src/v4/mini/tests/codec.test.ts +499 -0
- package/src/v4/mini/tests/object.test.ts +9 -0
- package/src/v4/mini/tests/string.test.ts +16 -0
- package/v3/index.cjs +17 -7
- package/v4/classic/coerce.cjs +17 -7
- package/v4/classic/compat.cjs +17 -7
- package/v4/classic/errors.cjs +17 -7
- package/v4/classic/external.cjs +18 -9
- package/v4/classic/external.d.cts +1 -1
- package/v4/classic/external.d.ts +1 -1
- package/v4/classic/external.js +1 -1
- package/v4/classic/index.cjs +17 -7
- package/v4/classic/iso.cjs +17 -7
- package/v4/classic/parse.cjs +27 -8
- package/v4/classic/parse.d.cts +8 -0
- package/v4/classic/parse.d.ts +8 -0
- package/v4/classic/parse.js +9 -0
- package/v4/classic/schemas.cjs +76 -11
- package/v4/classic/schemas.d.cts +48 -2
- package/v4/classic/schemas.d.ts +48 -2
- package/v4/classic/schemas.js +51 -3
- package/v4/core/api.cjs +36 -35
- package/v4/core/api.d.cts +3 -4
- package/v4/core/api.d.ts +3 -4
- package/v4/core/api.js +19 -24
- package/v4/core/checks.cjs +17 -7
- package/v4/core/core.cjs +8 -1
- package/v4/core/core.d.cts +3 -0
- package/v4/core/core.d.ts +3 -0
- package/v4/core/core.js +6 -0
- package/v4/core/errors.cjs +17 -7
- package/v4/core/index.cjs +17 -8
- package/v4/core/index.d.cts +0 -1
- package/v4/core/index.d.ts +0 -1
- package/v4/core/index.js +0 -1
- package/v4/core/json-schema.d.cts +1 -0
- package/v4/core/json-schema.d.ts +1 -0
- package/v4/core/parse.cjs +62 -8
- package/v4/core/parse.d.cts +24 -0
- package/v4/core/parse.d.ts +24 -0
- package/v4/core/parse.js +36 -0
- package/v4/core/regexes.cjs +34 -2
- package/v4/core/regexes.d.cts +16 -0
- package/v4/core/regexes.d.ts +16 -0
- package/v4/core/regexes.js +32 -1
- package/v4/core/schemas.cjs +326 -84
- package/v4/core/schemas.d.cts +61 -3
- package/v4/core/schemas.d.ts +61 -3
- package/v4/core/schemas.js +308 -76
- package/v4/core/to-json-schema.cjs +42 -5
- package/v4/core/to-json-schema.d.cts +4 -3
- package/v4/core/to-json-schema.d.ts +4 -3
- package/v4/core/to-json-schema.js +42 -5
- package/v4/core/util.cjs +69 -0
- package/v4/core/util.d.cts +10 -0
- package/v4/core/util.d.ts +10 -0
- package/v4/core/util.js +62 -0
- package/v4/locales/ar.cjs +17 -7
- package/v4/locales/az.cjs +17 -7
- package/v4/locales/be.cjs +17 -7
- package/v4/locales/bg.cjs +17 -7
- package/v4/locales/ca.cjs +17 -7
- package/v4/locales/cs.cjs +17 -7
- package/v4/locales/da.cjs +17 -7
- package/v4/locales/de.cjs +17 -7
- package/v4/locales/en.cjs +17 -7
- package/v4/locales/eo.cjs +17 -7
- package/v4/locales/es.cjs +17 -7
- package/v4/locales/fa.cjs +17 -7
- package/v4/locales/fi.cjs +17 -7
- package/v4/locales/fr-CA.cjs +17 -7
- package/v4/locales/fr.cjs +17 -7
- package/v4/locales/he.cjs +17 -7
- package/v4/locales/hu.cjs +17 -7
- package/v4/locales/id.cjs +17 -7
- package/v4/locales/is.cjs +17 -7
- package/v4/locales/it.cjs +17 -7
- package/v4/locales/ja.cjs +17 -7
- package/v4/locales/kh.cjs +17 -7
- package/v4/locales/ko.cjs +17 -7
- package/v4/locales/mk.cjs +17 -7
- package/v4/locales/ms.cjs +17 -7
- package/v4/locales/nl.cjs +17 -7
- package/v4/locales/no.cjs +17 -7
- package/v4/locales/ota.cjs +17 -7
- package/v4/locales/pl.cjs +17 -7
- package/v4/locales/ps.cjs +17 -7
- package/v4/locales/pt.cjs +17 -7
- package/v4/locales/ru.cjs +17 -7
- package/v4/locales/sl.cjs +17 -7
- package/v4/locales/sv.cjs +17 -7
- package/v4/locales/ta.cjs +17 -7
- package/v4/locales/th.cjs +17 -7
- package/v4/locales/tr.cjs +17 -7
- package/v4/locales/ua.cjs +17 -7
- package/v4/locales/ur.cjs +17 -7
- package/v4/locales/vi.cjs +17 -7
- package/v4/locales/yo.cjs +17 -7
- package/v4/locales/zh-CN.cjs +17 -7
- package/v4/locales/zh-TW.cjs +17 -7
- package/v4/mini/coerce.cjs +17 -7
- package/v4/mini/external.cjs +18 -9
- package/v4/mini/external.d.cts +1 -1
- package/v4/mini/external.d.ts +1 -1
- package/v4/mini/external.js +1 -1
- package/v4/mini/index.cjs +17 -7
- package/v4/mini/iso.cjs +17 -7
- package/v4/mini/parse.cjs +9 -1
- package/v4/mini/parse.d.cts +1 -1
- package/v4/mini/parse.d.ts +1 -1
- package/v4/mini/parse.js +1 -1
- package/v4/mini/schemas.cjs +75 -10
- package/v4/mini/schemas.d.cts +49 -1
- package/v4/mini/schemas.d.ts +49 -1
- package/v4/mini/schemas.js +49 -2
- package/src/v4/core/function.ts +0 -176
- package/v4/core/function.cjs +0 -102
- package/v4/core/function.d.cts +0 -52
- package/v4/core/function.d.ts +0 -52
- package/v4/core/function.js +0 -75
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
import { expect, expectTypeOf, test } from "vitest";
|
|
2
|
+
import { en } from "zod/locales";
|
|
3
|
+
import * as z from "zod/mini";
|
|
4
|
+
|
|
5
|
+
z.config(en());
|
|
6
|
+
|
|
7
|
+
const isoDateCodec = z.codec(
|
|
8
|
+
z.iso.datetime(), // Input: ISO string (validates to string)
|
|
9
|
+
z.date(), // Output: Date object
|
|
10
|
+
{
|
|
11
|
+
decode: (isoString) => new Date(isoString), // Forward: ISO string → Date
|
|
12
|
+
encode: (date) => date.toISOString(), // Backward: Date → ISO string
|
|
13
|
+
}
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
test("instanceof", () => {
|
|
17
|
+
expect(isoDateCodec instanceof z.ZodMiniCodec).toBe(true);
|
|
18
|
+
expect(isoDateCodec instanceof z.ZodMiniPipe).toBe(true);
|
|
19
|
+
expect(isoDateCodec instanceof z.ZodMiniType).toBe(true);
|
|
20
|
+
expect(isoDateCodec instanceof z.core.$ZodCodec).toBe(true);
|
|
21
|
+
expect(isoDateCodec instanceof z.core.$ZodPipe).toBe(true);
|
|
22
|
+
expect(isoDateCodec instanceof z.core.$ZodType).toBe(true);
|
|
23
|
+
|
|
24
|
+
expectTypeOf(isoDateCodec.def).toEqualTypeOf<z.core.$ZodCodecDef<z.ZodMiniISODateTime, z.ZodMiniDate<Date>>>();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("codec basic functionality", () => {
|
|
28
|
+
// ISO string -> Date codec using z.iso.datetime() for input validation
|
|
29
|
+
|
|
30
|
+
const testIsoString = "2024-01-15T10:30:00.000Z";
|
|
31
|
+
const testDate = new Date("2024-01-15T10:30:00.000Z");
|
|
32
|
+
|
|
33
|
+
// Forward decoding (ISO string -> Date)
|
|
34
|
+
const decodedResult = z.decode(isoDateCodec, testIsoString);
|
|
35
|
+
expect(decodedResult).toBeInstanceOf(Date);
|
|
36
|
+
expect(decodedResult.toISOString()).toMatchInlineSnapshot(`"2024-01-15T10:30:00.000Z"`);
|
|
37
|
+
|
|
38
|
+
// Backward encoding (Date -> ISO string)
|
|
39
|
+
const encodedResult = z.encode(isoDateCodec, testDate);
|
|
40
|
+
expect(typeof encodedResult).toBe("string");
|
|
41
|
+
expect(encodedResult).toMatchInlineSnapshot(`"2024-01-15T10:30:00.000Z"`);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("codec round trip", () => {
|
|
45
|
+
const isoDateCodec = z.codec(z.iso.datetime(), z.date(), {
|
|
46
|
+
decode: (isoString) => new Date(isoString),
|
|
47
|
+
encode: (date) => date.toISOString(),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const original = "2024-12-25T15:45:30.123Z";
|
|
51
|
+
const toDate = z.decode(isoDateCodec, original);
|
|
52
|
+
const backToString = z.encode(isoDateCodec, toDate);
|
|
53
|
+
|
|
54
|
+
expect(backToString).toMatchInlineSnapshot(`"2024-12-25T15:45:30.123Z"`);
|
|
55
|
+
expect(toDate).toBeInstanceOf(Date);
|
|
56
|
+
expect(toDate.getTime()).toMatchInlineSnapshot(`1735141530123`);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("codec with refinement", () => {
|
|
60
|
+
const isoDateCodec = z
|
|
61
|
+
.codec(z.iso.datetime(), z.date(), {
|
|
62
|
+
decode: (isoString) => new Date(isoString),
|
|
63
|
+
encode: (date) => date.toISOString(),
|
|
64
|
+
})
|
|
65
|
+
.check(z.refine((val) => val.getFullYear() === 2024, { error: "Year must be 2024" }));
|
|
66
|
+
|
|
67
|
+
// Valid 2024 date
|
|
68
|
+
const validDate = z.decode(isoDateCodec, "2024-01-15T10:30:00.000Z");
|
|
69
|
+
expect(validDate.getFullYear()).toMatchInlineSnapshot(`2024`);
|
|
70
|
+
expect(validDate.getTime()).toMatchInlineSnapshot(`1705314600000`);
|
|
71
|
+
|
|
72
|
+
// Invalid year should fail safely
|
|
73
|
+
const invalidYearResult = z.safeDecode(isoDateCodec, "2023-01-15T10:30:00.000Z");
|
|
74
|
+
expect(invalidYearResult.success).toBe(false);
|
|
75
|
+
if (!invalidYearResult.success) {
|
|
76
|
+
expect(invalidYearResult.error.issues).toMatchInlineSnapshot(`
|
|
77
|
+
[
|
|
78
|
+
{
|
|
79
|
+
"code": "custom",
|
|
80
|
+
"message": "Year must be 2024",
|
|
81
|
+
"path": [],
|
|
82
|
+
},
|
|
83
|
+
]
|
|
84
|
+
`);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("safe codec operations", () => {
|
|
89
|
+
const isoDateCodec = z.codec(z.iso.datetime(), z.date(), {
|
|
90
|
+
decode: (isoString) => new Date(isoString),
|
|
91
|
+
encode: (date) => date.toISOString(),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Safe decode with invalid input
|
|
95
|
+
const safeDecodeResult = z.safeDecode(isoDateCodec, "invalid-date");
|
|
96
|
+
expect(safeDecodeResult.success).toBe(false);
|
|
97
|
+
if (!safeDecodeResult.success) {
|
|
98
|
+
expect(safeDecodeResult.error.issues).toMatchInlineSnapshot(`
|
|
99
|
+
[
|
|
100
|
+
{
|
|
101
|
+
"code": "invalid_format",
|
|
102
|
+
"format": "datetime",
|
|
103
|
+
"message": "Invalid ISO datetime",
|
|
104
|
+
"origin": "string",
|
|
105
|
+
"path": [],
|
|
106
|
+
"pattern": "/^(?:(?:\\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])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d+)?)?(?:Z))$/",
|
|
107
|
+
},
|
|
108
|
+
]
|
|
109
|
+
`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Safe decode with valid input
|
|
113
|
+
const safeDecodeValid = z.safeDecode(isoDateCodec, "2024-01-15T10:30:00.000Z");
|
|
114
|
+
expect(safeDecodeValid.success).toBe(true);
|
|
115
|
+
if (safeDecodeValid.success) {
|
|
116
|
+
expect(safeDecodeValid.data).toBeInstanceOf(Date);
|
|
117
|
+
expect(safeDecodeValid.data.getTime()).toMatchInlineSnapshot(`1705314600000`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Safe encode with valid input
|
|
121
|
+
const safeEncodeResult = z.safeEncode(isoDateCodec, new Date("2024-01-01"));
|
|
122
|
+
expect(safeEncodeResult.success).toBe(true);
|
|
123
|
+
if (safeEncodeResult.success) {
|
|
124
|
+
expect(safeEncodeResult.data).toMatchInlineSnapshot(`"2024-01-01T00:00:00.000Z"`);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("codec with different types", () => {
|
|
129
|
+
// String -> Number codec
|
|
130
|
+
const stringNumberCodec = z.codec(z.string(), z.number(), {
|
|
131
|
+
decode: (str) => Number.parseFloat(str),
|
|
132
|
+
encode: (num) => num.toString(),
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const decodedNumber = z.decode(stringNumberCodec, "42.5");
|
|
136
|
+
expect(decodedNumber).toMatchInlineSnapshot(`42.5`);
|
|
137
|
+
expect(typeof decodedNumber).toBe("number");
|
|
138
|
+
|
|
139
|
+
const encodedString = z.encode(stringNumberCodec, 42.5);
|
|
140
|
+
expect(encodedString).toMatchInlineSnapshot(`"42.5"`);
|
|
141
|
+
expect(typeof encodedString).toBe("string");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("async codec operations", async () => {
|
|
145
|
+
const isoDateCodec = z.codec(z.iso.datetime(), z.date(), {
|
|
146
|
+
decode: (isoString) => new Date(isoString),
|
|
147
|
+
encode: (date) => date.toISOString(),
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// Async decode
|
|
151
|
+
const decodedResult = await z.decodeAsync(isoDateCodec, "2024-01-15T10:30:00.000Z");
|
|
152
|
+
expect(decodedResult).toBeInstanceOf(Date);
|
|
153
|
+
expect(decodedResult.getTime()).toMatchInlineSnapshot(`1705314600000`);
|
|
154
|
+
|
|
155
|
+
// Async encode
|
|
156
|
+
const encodedResult = await z.encodeAsync(isoDateCodec, new Date("2024-01-15T10:30:00.000Z"));
|
|
157
|
+
expect(typeof encodedResult).toBe("string");
|
|
158
|
+
expect(encodedResult).toMatchInlineSnapshot(`"2024-01-15T10:30:00.000Z"`);
|
|
159
|
+
|
|
160
|
+
// Safe async operations
|
|
161
|
+
const safeDecodeResult = await z.safeDecodeAsync(isoDateCodec, "2024-01-15T10:30:00.000Z");
|
|
162
|
+
expect(safeDecodeResult.success).toBe(true);
|
|
163
|
+
if (safeDecodeResult.success) {
|
|
164
|
+
expect(safeDecodeResult.data.getTime()).toMatchInlineSnapshot(`1705314600000`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const safeEncodeResult = await z.safeEncodeAsync(isoDateCodec, new Date("2024-01-15T10:30:00.000Z"));
|
|
168
|
+
expect(safeEncodeResult.success).toBe(true);
|
|
169
|
+
if (safeEncodeResult.success) {
|
|
170
|
+
expect(safeEncodeResult.data).toMatchInlineSnapshot(`"2024-01-15T10:30:00.000Z"`);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("codec type inference", () => {
|
|
175
|
+
const codec = z.codec(z.string(), z.number(), {
|
|
176
|
+
decode: (str) => Number.parseInt(str),
|
|
177
|
+
encode: (num) => num.toString(),
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// These should compile without type errors
|
|
181
|
+
const decoded: number = z.decode(codec, "123");
|
|
182
|
+
const encoded: string = z.encode(codec, 123);
|
|
183
|
+
|
|
184
|
+
expect(decoded).toMatchInlineSnapshot(`123`);
|
|
185
|
+
expect(encoded).toMatchInlineSnapshot(`"123"`);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test("nested codec with object containing codec property", () => {
|
|
189
|
+
// Nested schema: object containing a codec as one of its properties, with refinements at all levels
|
|
190
|
+
const waypointSchema = z
|
|
191
|
+
.object({
|
|
192
|
+
name: z.string().check(z.minLength(1, "Waypoint name required")),
|
|
193
|
+
difficulty: z.enum(["easy", "medium", "hard"]),
|
|
194
|
+
coordinate: z
|
|
195
|
+
.codec(
|
|
196
|
+
z
|
|
197
|
+
.string()
|
|
198
|
+
.check(z.regex(/^-?\d+,-?\d+$/, "Must be 'x,y' format")), // Input: coordinate string
|
|
199
|
+
z
|
|
200
|
+
.object({ x: z.number(), y: z.number() })
|
|
201
|
+
.check(z.refine((coord) => coord.x >= 0 && coord.y >= 0, { error: "Coordinates must be non-negative" })), // Output: coordinate object
|
|
202
|
+
{
|
|
203
|
+
decode: (coordString: string) => {
|
|
204
|
+
const [x, y] = coordString.split(",").map(Number);
|
|
205
|
+
return { x, y };
|
|
206
|
+
},
|
|
207
|
+
encode: (coord: { x: number; y: number }) => `${coord.x},${coord.y}`,
|
|
208
|
+
}
|
|
209
|
+
)
|
|
210
|
+
.check(z.refine((coord) => coord.x <= 1000 && coord.y <= 1000, { error: "Coordinates must be within bounds" })),
|
|
211
|
+
})
|
|
212
|
+
.check(
|
|
213
|
+
z.refine((waypoint) => waypoint.difficulty !== "hard" || waypoint.coordinate.x >= 100, {
|
|
214
|
+
error: "Hard waypoints must be at least 100 units from origin",
|
|
215
|
+
})
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
// Test data
|
|
219
|
+
const inputWaypoint = {
|
|
220
|
+
name: "Summit Point",
|
|
221
|
+
difficulty: "medium" as const,
|
|
222
|
+
coordinate: "150,200",
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
// Forward decoding (object with string coordinate -> object with coordinate object)
|
|
226
|
+
const decodedWaypoint = z.decode(waypointSchema, inputWaypoint);
|
|
227
|
+
expect(decodedWaypoint).toMatchInlineSnapshot(`
|
|
228
|
+
{
|
|
229
|
+
"coordinate": {
|
|
230
|
+
"x": 150,
|
|
231
|
+
"y": 200,
|
|
232
|
+
},
|
|
233
|
+
"difficulty": "medium",
|
|
234
|
+
"name": "Summit Point",
|
|
235
|
+
}
|
|
236
|
+
`);
|
|
237
|
+
|
|
238
|
+
// Backward encoding (object with coordinate object -> object with string coordinate)
|
|
239
|
+
const encodedWaypoint = z.encode(waypointSchema, decodedWaypoint);
|
|
240
|
+
expect(encodedWaypoint).toMatchInlineSnapshot(`
|
|
241
|
+
{
|
|
242
|
+
"coordinate": "150,200",
|
|
243
|
+
"difficulty": "medium",
|
|
244
|
+
"name": "Summit Point",
|
|
245
|
+
}
|
|
246
|
+
`);
|
|
247
|
+
|
|
248
|
+
// Test refinements at all levels
|
|
249
|
+
// String validation (empty waypoint name)
|
|
250
|
+
const emptyNameResult = z.safeDecode(waypointSchema, {
|
|
251
|
+
name: "",
|
|
252
|
+
difficulty: "easy",
|
|
253
|
+
coordinate: "10,20",
|
|
254
|
+
});
|
|
255
|
+
expect(emptyNameResult.success).toBe(false);
|
|
256
|
+
if (!emptyNameResult.success) {
|
|
257
|
+
expect(emptyNameResult.error.issues).toMatchInlineSnapshot(`
|
|
258
|
+
[
|
|
259
|
+
{
|
|
260
|
+
"code": "too_small",
|
|
261
|
+
"inclusive": true,
|
|
262
|
+
"message": "Waypoint name required",
|
|
263
|
+
"minimum": 1,
|
|
264
|
+
"origin": "string",
|
|
265
|
+
"path": [
|
|
266
|
+
"name",
|
|
267
|
+
],
|
|
268
|
+
},
|
|
269
|
+
]
|
|
270
|
+
`);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Enum validation (invalid difficulty)
|
|
274
|
+
const invalidDifficultyResult = z.safeDecode(waypointSchema, {
|
|
275
|
+
name: "Test Point",
|
|
276
|
+
difficulty: "impossible" as any,
|
|
277
|
+
coordinate: "10,20",
|
|
278
|
+
});
|
|
279
|
+
expect(invalidDifficultyResult.success).toBe(false);
|
|
280
|
+
if (!invalidDifficultyResult.success) {
|
|
281
|
+
expect(invalidDifficultyResult.error.issues).toMatchInlineSnapshot(`
|
|
282
|
+
[
|
|
283
|
+
{
|
|
284
|
+
"code": "invalid_value",
|
|
285
|
+
"message": "Invalid option: expected one of "easy"|"medium"|"hard"",
|
|
286
|
+
"path": [
|
|
287
|
+
"difficulty",
|
|
288
|
+
],
|
|
289
|
+
"values": [
|
|
290
|
+
"easy",
|
|
291
|
+
"medium",
|
|
292
|
+
"hard",
|
|
293
|
+
],
|
|
294
|
+
},
|
|
295
|
+
]
|
|
296
|
+
`);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Codec string format validation (invalid coordinate format)
|
|
300
|
+
const invalidFormatResult = z.safeDecode(waypointSchema, {
|
|
301
|
+
name: "Test Point",
|
|
302
|
+
difficulty: "easy",
|
|
303
|
+
coordinate: "invalid",
|
|
304
|
+
});
|
|
305
|
+
expect(invalidFormatResult.success).toBe(false);
|
|
306
|
+
if (!invalidFormatResult.success) {
|
|
307
|
+
expect(invalidFormatResult.error.issues).toMatchInlineSnapshot(`
|
|
308
|
+
[
|
|
309
|
+
{
|
|
310
|
+
"code": "invalid_format",
|
|
311
|
+
"format": "regex",
|
|
312
|
+
"message": "Must be 'x,y' format",
|
|
313
|
+
"origin": "string",
|
|
314
|
+
"path": [
|
|
315
|
+
"coordinate",
|
|
316
|
+
],
|
|
317
|
+
"pattern": "/^-?\\d+,-?\\d+$/",
|
|
318
|
+
},
|
|
319
|
+
]
|
|
320
|
+
`);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Codec object refinement (negative coordinates)
|
|
324
|
+
const negativeCoordResult = z.safeDecode(waypointSchema, {
|
|
325
|
+
name: "Test Point",
|
|
326
|
+
difficulty: "easy",
|
|
327
|
+
coordinate: "-5,10",
|
|
328
|
+
});
|
|
329
|
+
expect(negativeCoordResult.success).toBe(false);
|
|
330
|
+
if (!negativeCoordResult.success) {
|
|
331
|
+
expect(negativeCoordResult.error.issues).toMatchInlineSnapshot(`
|
|
332
|
+
[
|
|
333
|
+
{
|
|
334
|
+
"code": "custom",
|
|
335
|
+
"message": "Coordinates must be non-negative",
|
|
336
|
+
"path": [
|
|
337
|
+
"coordinate",
|
|
338
|
+
],
|
|
339
|
+
},
|
|
340
|
+
]
|
|
341
|
+
`);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Codec-level refinement (coordinates out of bounds)
|
|
345
|
+
const outOfBoundsResult = z.safeDecode(waypointSchema, {
|
|
346
|
+
name: "Test Point",
|
|
347
|
+
difficulty: "easy",
|
|
348
|
+
coordinate: "1500,2000",
|
|
349
|
+
});
|
|
350
|
+
expect(outOfBoundsResult.success).toBe(false);
|
|
351
|
+
if (!outOfBoundsResult.success) {
|
|
352
|
+
expect(outOfBoundsResult.error.issues).toMatchInlineSnapshot(`
|
|
353
|
+
[
|
|
354
|
+
{
|
|
355
|
+
"code": "custom",
|
|
356
|
+
"message": "Coordinates must be within bounds",
|
|
357
|
+
"path": [
|
|
358
|
+
"coordinate",
|
|
359
|
+
],
|
|
360
|
+
},
|
|
361
|
+
]
|
|
362
|
+
`);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Object-level refinement (hard waypoint too close to origin)
|
|
366
|
+
const hardWaypointResult = z.safeDecode(waypointSchema, {
|
|
367
|
+
name: "Expert Point",
|
|
368
|
+
difficulty: "hard",
|
|
369
|
+
coordinate: "50,60", // x < 100, but hard waypoints need x >= 100
|
|
370
|
+
});
|
|
371
|
+
expect(hardWaypointResult.success).toBe(false);
|
|
372
|
+
if (!hardWaypointResult.success) {
|
|
373
|
+
expect(hardWaypointResult.error.issues).toMatchInlineSnapshot(`
|
|
374
|
+
[
|
|
375
|
+
{
|
|
376
|
+
"code": "custom",
|
|
377
|
+
"message": "Hard waypoints must be at least 100 units from origin",
|
|
378
|
+
"path": [],
|
|
379
|
+
},
|
|
380
|
+
]
|
|
381
|
+
`);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// Round trip test
|
|
385
|
+
const roundTripResult = z.encode(waypointSchema, z.decode(waypointSchema, inputWaypoint));
|
|
386
|
+
expect(roundTripResult).toMatchInlineSnapshot(`
|
|
387
|
+
{
|
|
388
|
+
"coordinate": "150,200",
|
|
389
|
+
"difficulty": "medium",
|
|
390
|
+
"name": "Summit Point",
|
|
391
|
+
}
|
|
392
|
+
`);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
test("mutating refinements", () => {
|
|
396
|
+
const A = z.codec(z.string(), z.string().check(z.trim()), {
|
|
397
|
+
decode: (val) => val,
|
|
398
|
+
encode: (val) => val,
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
expect(z.decode(A, " asdf ")).toMatchInlineSnapshot(`"asdf"`);
|
|
402
|
+
expect(z.encode(A, " asdf ")).toMatchInlineSnapshot(`"asdf"`);
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
test("codec type enforcement - correct encode/decode signatures", () => {
|
|
406
|
+
// Test that codec functions have correct type signatures
|
|
407
|
+
const stringToNumberCodec = z.codec(z.string(), z.number(), {
|
|
408
|
+
decode: (value: string) => Number(value), // core.output<A> -> core.input<B>
|
|
409
|
+
encode: (value: number) => String(value), // core.input<B> -> core.output<A>
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
// These should compile without errors - correct types
|
|
413
|
+
expectTypeOf<(value: string, payload: z.core.ParsePayload<string>) => number>(
|
|
414
|
+
stringToNumberCodec.def.transform
|
|
415
|
+
).toBeFunction();
|
|
416
|
+
expectTypeOf<(value: number, payload: z.core.ParsePayload<number>) => string>(
|
|
417
|
+
stringToNumberCodec.def.reverseTransform
|
|
418
|
+
).toBeFunction();
|
|
419
|
+
|
|
420
|
+
// Test that decode parameter type is core.output<A> (string)
|
|
421
|
+
const validDecode = (value: string) => Number(value);
|
|
422
|
+
expectTypeOf(validDecode).toMatchTypeOf<(value: string) => number>();
|
|
423
|
+
|
|
424
|
+
// Test that encode parameter type is core.input<B> (number)
|
|
425
|
+
const validEncode = (value: number) => String(value);
|
|
426
|
+
expectTypeOf(validEncode).toMatchTypeOf<(value: number) => string>();
|
|
427
|
+
|
|
428
|
+
z.codec(z.string(), z.number(), {
|
|
429
|
+
// @ts-expect-error - decode should NOT accept core.input<A> as parameter
|
|
430
|
+
decode: (value: never, _payload) => Number(value), // Wrong: should be string, not unknown
|
|
431
|
+
encode: (value: number, _payload) => String(value),
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
z.codec(z.string(), z.number(), {
|
|
435
|
+
decode: (value: string) => Number(value),
|
|
436
|
+
// @ts-expect-error - encode should NOT accept core.output<B> as parameter
|
|
437
|
+
encode: (value: never) => String(value), // Wrong: should be number, not unknown
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
z.codec(z.string(), z.number(), {
|
|
441
|
+
// @ts-expect-error - decode return type should be core.input<B>
|
|
442
|
+
decode: (value: string) => String(value), // Wrong: should return number, not string
|
|
443
|
+
encode: (value: number) => String(value),
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
z.codec(z.string(), z.number(), {
|
|
447
|
+
decode: (value: string) => Number(value),
|
|
448
|
+
// @ts-expect-error - encode return type should be core.output<A>
|
|
449
|
+
encode: (value: number) => Number(value), // Wrong: should return string, not number
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
test("codec type enforcement - complex types", () => {
|
|
454
|
+
type User = { id: number; name: string };
|
|
455
|
+
type UserInput = { id: string; name: string };
|
|
456
|
+
|
|
457
|
+
const userCodec = z.codec(
|
|
458
|
+
z.object({ id: z.string(), name: z.string() }),
|
|
459
|
+
z.object({ id: z.number(), name: z.string() }),
|
|
460
|
+
{
|
|
461
|
+
decode: (input: UserInput) => ({ id: Number(input.id), name: input.name }),
|
|
462
|
+
encode: (user: User) => ({ id: String(user.id), name: user.name }),
|
|
463
|
+
}
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
// Verify correct types are inferred
|
|
467
|
+
expectTypeOf<(input: UserInput, payload: z.core.ParsePayload<UserInput>) => User>(
|
|
468
|
+
userCodec.def.transform
|
|
469
|
+
).toBeFunction();
|
|
470
|
+
expectTypeOf<(user: User, payload: z.core.ParsePayload<User>) => UserInput>(
|
|
471
|
+
userCodec.def.reverseTransform
|
|
472
|
+
).toBeFunction();
|
|
473
|
+
|
|
474
|
+
z.codec(
|
|
475
|
+
z.object({
|
|
476
|
+
id: z.string(),
|
|
477
|
+
name: z.string(),
|
|
478
|
+
}),
|
|
479
|
+
z.object({ id: z.number(), name: z.string() }),
|
|
480
|
+
{
|
|
481
|
+
// @ts-expect-error - decode parameter should be UserInput, not User
|
|
482
|
+
decode: (input: User) => ({ id: Number(input.id), name: input.name }), // Wrong type
|
|
483
|
+
encode: (user: User) => ({ id: String(user.id), name: user.name }),
|
|
484
|
+
}
|
|
485
|
+
);
|
|
486
|
+
|
|
487
|
+
z.codec(
|
|
488
|
+
z.object({
|
|
489
|
+
id: z.string(),
|
|
490
|
+
name: z.string(),
|
|
491
|
+
}),
|
|
492
|
+
z.object({ id: z.number(), name: z.string() }),
|
|
493
|
+
{
|
|
494
|
+
decode: (input: UserInput) => ({ id: Number(input.id), name: input.name }),
|
|
495
|
+
// @ts-expect-error - encode parameter should be User, not UserInput
|
|
496
|
+
encode: (user: UserInput) => ({ id: String(user.id), name: user.name }), // Wrong type
|
|
497
|
+
}
|
|
498
|
+
);
|
|
499
|
+
});
|
|
@@ -116,6 +116,15 @@ test("z.extend", () => {
|
|
|
116
116
|
expect(z.safeParse(extendedSchema, { name: "John", age: 30, isAdmin: true }).success).toBe(true);
|
|
117
117
|
});
|
|
118
118
|
|
|
119
|
+
test("z.safeExtend", () => {
|
|
120
|
+
const extended = z.safeExtend(userSchema, { name: z.string() });
|
|
121
|
+
expect(z.safeParse(extended, { name: "John", age: 30 }).success).toBe(true);
|
|
122
|
+
type Extended = z.infer<typeof extended>;
|
|
123
|
+
expectTypeOf<Extended>().toEqualTypeOf<{ name: string; age: number; email?: string }>();
|
|
124
|
+
// @ts-expect-error
|
|
125
|
+
z.safeExtend(userSchema, { name: z.number() });
|
|
126
|
+
});
|
|
127
|
+
|
|
119
128
|
test("z.pick", () => {
|
|
120
129
|
const pickedSchema = z.pick(userSchema, { name: true, email: true });
|
|
121
130
|
type PickedUser = z.infer<typeof pickedSchema>;
|
|
@@ -297,3 +297,19 @@ test("z.jwt", () => {
|
|
|
297
297
|
// wrong type
|
|
298
298
|
expect(() => z.parse(a, 123)).toThrow();
|
|
299
299
|
});
|
|
300
|
+
|
|
301
|
+
test("z.hash generic format", () => {
|
|
302
|
+
expect(z.hash("sha256").parse("a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3")).toBe(
|
|
303
|
+
"a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3"
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
// --- Type-level checks (ensure the literal format string is encoded in the return type)
|
|
307
|
+
expectTypeOf(z.hash("md5")).toEqualTypeOf<z.ZodMiniCustomStringFormat<"md5_hex">>();
|
|
308
|
+
expectTypeOf(z.hash("sha1")).toEqualTypeOf<z.ZodMiniCustomStringFormat<"sha1_hex">>();
|
|
309
|
+
expectTypeOf(z.hash("sha256", { enc: "base64" as const })).toEqualTypeOf<
|
|
310
|
+
z.ZodMiniCustomStringFormat<"sha256_base64">
|
|
311
|
+
>();
|
|
312
|
+
expectTypeOf(z.hash("sha384", { enc: "base64url" as const })).toEqualTypeOf<
|
|
313
|
+
z.ZodMiniCustomStringFormat<"sha384_base64url">
|
|
314
|
+
>();
|
|
315
|
+
});
|
package/v3/index.cjs
CHANGED
|
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
36
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
37
|
};
|
package/v4/classic/coerce.cjs
CHANGED
|
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
36
|
exports.string = string;
|
|
27
37
|
exports.number = number;
|
package/v4/classic/compat.cjs
CHANGED
|
@@ -16,13 +16,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
16
16
|
}) : function(o, v) {
|
|
17
17
|
o["default"] = v;
|
|
18
18
|
});
|
|
19
|
-
var __importStar = (this && this.__importStar) || function (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
};
|
|
19
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
20
|
+
var ownKeys = function(o) {
|
|
21
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
22
|
+
var ar = [];
|
|
23
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
24
|
+
return ar;
|
|
25
|
+
};
|
|
26
|
+
return ownKeys(o);
|
|
27
|
+
};
|
|
28
|
+
return function (mod) {
|
|
29
|
+
if (mod && mod.__esModule) return mod;
|
|
30
|
+
var result = {};
|
|
31
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
+
__setModuleDefault(result, mod);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
26
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
37
|
exports.ZodFirstPartyTypeKind = exports.config = exports.$brand = exports.ZodIssueCode = void 0;
|
|
28
38
|
exports.setErrorMap = setErrorMap;
|
package/v4/classic/errors.cjs
CHANGED
|
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
36
|
exports.ZodRealError = exports.ZodError = void 0;
|
|
27
37
|
const core = __importStar(require("../core/index.cjs"));
|