Skip to content

Commit

Permalink
feat: support zod 3.24.x (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 authored Dec 11, 2024
1 parent 995ed37 commit 35fd23b
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/dezerialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ const dezerializers = {
if ("kind" in shape) {
if (shape.kind == "ip") {
s = s.ip({ version: shape.version });
} else if (shape.kind == "cidr") {
s = s.cidr({ version: shape.version });
} else if (shape.kind == "datetime") {
s = s.datetime({
offset: shape.offset,
Expand Down
7 changes: 7 additions & 0 deletions lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,20 @@ test.each([

p(z.string().date(), { type: "string", kind: "date" }),
p(z.string().duration(), { type: "string", kind: "duration" }),
p(z.string().cidr(), { type: "string", kind: "cidr" }),
p(z.string().base64(), { type: "string", kind: "base64" }),
p(z.string().base64url(), { type: "string", kind: "base64url" }),

p(z.string().ip({ version: "v4" }), {
type: "string",
kind: "ip",
version: "v4",
}),
p(z.string().cidr({ version: "v4" }), {
type: "string",
kind: "cidr",
version: "v4",
}),

p(z.string().datetime({ offset: true, precision: 3 }), {
type: "string",
Expand Down
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"dependencies": {
"type-fest": "^4.26.0",
"zod": "^3.23.8"
"zod": "^3.24.1"
},
"devDependencies": {
"@vitest/coverage-v8": "^2.0.5",
Expand Down
18 changes: 17 additions & 1 deletion lib/schema.zodex.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@
}
}
},
{
"type": "object",
"description": "CIDR type",
"properties": {
"kind": {
"type": "literal",
"value": "cidr"
},
"version": {
"type": "enum",
"values": ["v4", "v6"],
"isOptional": true
}
}
},
{
"type": "object",
"description": "Regex",
Expand Down Expand Up @@ -132,7 +147,8 @@
"ulid",
"date",
"duration",
"base64"
"base64",
"base64url"
]
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const STRING_KINDS = new Set([
"date",
"duration",
"base64",
"base64url",
] as const);

export type SzString = {
Expand All @@ -56,6 +57,7 @@ export type SzString = {
(
| object
| { kind: "ip"; version?: "v4" | "v6" }
| { kind: "cidr"; version?: "v4" | "v6" }
| { regex: string; flags?: string }
| {
kind: "time";
Expand Down Expand Up @@ -130,7 +132,7 @@ export type SzDiscriminatedUnionOption<Discriminator extends string> = {
} & SzType;
export type SzDiscriminatedUnion<
Discriminator extends string = string,
Options extends SzDiscriminatedUnionOption<Discriminator>[] = []
Options extends readonly SzDiscriminatedUnionOption<Discriminator>[] = []
> = {
type: "discriminatedUnion";
discriminator: Discriminator;
Expand Down
2 changes: 2 additions & 0 deletions lib/zerialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ const zerializers = {
}
: check.kind == "ip"
? { kind: "ip", version: check.version }
: check.kind == "cidr"
? { kind: "cidr", version: check.version }
: check.kind == "time"
? {
kind: "time",
Expand Down
9 changes: 7 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 35fd23b

Please sign in to comment.