Skip to content

Commit 028b289

Browse files
committed
Add methods
1 parent e5085be commit 028b289

File tree

2 files changed

+75
-14
lines changed

2 files changed

+75
-14
lines changed

packages/zod/src/v4/classic/schemas.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ export interface ZodType<
5555
params?: core.ParseContext<core.$ZodIssue>
5656
) => Promise<parse.ZodSafeParseResult<core.output<this>>>;
5757

58+
// encoding/decoding
59+
encode(data: core.output<this>, params?: core.ParseContext<core.$ZodIssue>): core.input<this>;
60+
decode(data: core.input<this>, params?: core.ParseContext<core.$ZodIssue>): core.output<this>;
61+
encodeAsync(data: core.output<this>, params?: core.ParseContext<core.$ZodIssue>): Promise<core.input<this>>;
62+
decodeAsync(data: core.input<this>, params?: core.ParseContext<core.$ZodIssue>): Promise<core.output<this>>;
63+
safeEncode(
64+
data: core.output<this>,
65+
params?: core.ParseContext<core.$ZodIssue>
66+
): parse.ZodSafeParseResult<core.input<this>>;
67+
safeDecode(
68+
data: core.input<this>,
69+
params?: core.ParseContext<core.$ZodIssue>
70+
): parse.ZodSafeParseResult<core.output<this>>;
71+
safeEncodeAsync(
72+
data: core.output<this>,
73+
params?: core.ParseContext<core.$ZodIssue>
74+
): Promise<parse.ZodSafeParseResult<core.input<this>>>;
75+
safeDecodeAsync(
76+
data: core.input<this>,
77+
params?: core.ParseContext<core.$ZodIssue>
78+
): Promise<parse.ZodSafeParseResult<core.output<this>>>;
79+
5880
// refinements
5981
refine(check: (arg: core.output<this>) => unknown | Promise<unknown>, params?: string | core.$ZodCustomParams): this;
6082
superRefine(
@@ -150,6 +172,16 @@ export const ZodType: core.$constructor<ZodType> = /*@__PURE__*/ core.$construct
150172
inst.safeParseAsync = async (data, params) => parse.safeParseAsync(inst, data, params);
151173
inst.spa = inst.safeParseAsync;
152174

175+
// encoding/decoding
176+
inst.encode = (data, params) => parse.encode(inst, data, params);
177+
inst.decode = (data, params) => parse.decode(inst, data, params);
178+
inst.encodeAsync = async (data, params) => parse.encodeAsync(inst, data, params);
179+
inst.decodeAsync = async (data, params) => parse.decodeAsync(inst, data, params);
180+
inst.safeEncode = (data, params) => parse.safeEncode(inst, data, params);
181+
inst.safeDecode = (data, params) => parse.safeDecode(inst, data, params);
182+
inst.safeEncodeAsync = async (data, params) => parse.safeEncodeAsync(inst, data, params);
183+
inst.safeDecodeAsync = async (data, params) => parse.safeDecodeAsync(inst, data, params);
184+
153185
// refinements
154186
inst.refine = (check, params) => inst.check(refine(check, params));
155187
inst.superRefine = (refinement) => inst.check(superRefine(refinement));

play.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,46 @@ import * as z from "zod";
22

33
z;
44

5-
export const Field1Schema = z.strictObject({
6-
"field1.1": z.string(),
7-
"field1.2": z.number(),
8-
});
9-
10-
export const Field2Schema = z.strictObject({
11-
"field2.1": z.boolean(),
12-
"field2.2": z.date(),
13-
});
14-
15-
export const SomeSchema = z.strictObject({
16-
field1: Field1Schema as typeof Field1Schema,
17-
field2: Field2Schema as typeof Field2Schema,
18-
});
5+
const stringToDate = z.codec(
6+
z.iso.datetime(), // input schema: ISO date string
7+
z.date(), // output schema: Date object
8+
{
9+
decode: (isoString) => new Date(isoString), // ISO string → Date
10+
encode: (date) => date.toISOString(), // Date → ISO string
11+
}
12+
);
13+
14+
stringToDate.decode("2024-01-15T10:30:00.000Z");
15+
// => Date
16+
17+
stringToDate.encode(new Date())
18+
// => string
19+
20+
21+
stringToDate.decode(1234);
22+
// ^ Argument of type 'number' is not assignable
23+
// to parameter of type 'string'.ts(2345)
24+
25+
stringToDate.encode(1234);
26+
// ^ Argument of type 'number' is not assignable
27+
// to parameter of type 'Date'.ts(2345)
28+
29+
30+
31+
including:
32+
33+
stringToNumber
34+
stringToInt
35+
stringToBigInt
36+
numberToBigInt
37+
isoDatetimeToDate
38+
epochSecondsToDate
39+
epochMillisToDate
40+
jsonCodec
41+
utf8ToBytes
42+
bytesToUtf8
43+
base64ToBytes
44+
base64urlToBytes
45+
hexToBytes
46+
stringToURL
47+
uriComponent

0 commit comments

Comments
 (0)