From 35fd23b6b82c66079cf72199b85ef836e5989320 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 11 Dec 2024 20:51:46 +0800 Subject: [PATCH] feat: support zod 3.24.x (#47) --- lib/dezerialize.ts | 2 ++ lib/index.test.ts | 7 +++++++ lib/package.json | 2 +- lib/schema.zodex.json | 18 +++++++++++++++++- lib/types.ts | 4 +++- lib/zerialize.ts | 2 ++ pnpm-lock.yaml | 9 +++++++-- 7 files changed, 39 insertions(+), 5 deletions(-) diff --git a/lib/dezerialize.ts b/lib/dezerialize.ts index 9a38b19..c6b0677 100644 --- a/lib/dezerialize.ts +++ b/lib/dezerialize.ts @@ -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, diff --git a/lib/index.test.ts b/lib/index.test.ts index 8ab60b1..2ef3671 100644 --- a/lib/index.test.ts +++ b/lib/index.test.ts @@ -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", diff --git a/lib/package.json b/lib/package.json index fc80f7b..37aa1c1 100644 --- a/lib/package.json +++ b/lib/package.json @@ -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", diff --git a/lib/schema.zodex.json b/lib/schema.zodex.json index 4a243f6..3b1c6dc 100644 --- a/lib/schema.zodex.json +++ b/lib/schema.zodex.json @@ -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", @@ -132,7 +147,8 @@ "ulid", "date", "duration", - "base64" + "base64", + "base64url" ] } } diff --git a/lib/types.ts b/lib/types.ts index 2571265..4f2c8cf 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -33,6 +33,7 @@ export const STRING_KINDS = new Set([ "date", "duration", "base64", + "base64url", ] as const); export type SzString = { @@ -56,6 +57,7 @@ export type SzString = { ( | object | { kind: "ip"; version?: "v4" | "v6" } + | { kind: "cidr"; version?: "v4" | "v6" } | { regex: string; flags?: string } | { kind: "time"; @@ -130,7 +132,7 @@ export type SzDiscriminatedUnionOption = { } & SzType; export type SzDiscriminatedUnion< Discriminator extends string = string, - Options extends SzDiscriminatedUnionOption[] = [] + Options extends readonly SzDiscriminatedUnionOption[] = [] > = { type: "discriminatedUnion"; discriminator: Discriminator; diff --git a/lib/zerialize.ts b/lib/zerialize.ts index 76adfd6..2d1f763 100644 --- a/lib/zerialize.ts +++ b/lib/zerialize.ts @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d7bb7f4..588a64e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -82,8 +82,8 @@ importers: specifier: ^4.26.0 version: 4.26.0 zod: - specifier: ^3.23.8 - version: 3.23.8 + specifier: ^3.24.1 + version: 3.24.1 devDependencies: '@vitest/coverage-v8': specifier: ^2.0.5 @@ -2149,6 +2149,9 @@ packages: zod@3.23.8: resolution: {integrity: sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==} + zod@3.24.1: + resolution: {integrity: sha512-muH7gBL9sI1nciMZV67X5fTKKBLtwpZ5VBp1vsOQzj1MhrBZ4wlVCm3gedKZWLp0Oyel8sIGfeiz54Su+OVT+A==} + snapshots: '@ampproject/remapping@2.3.0': @@ -4117,3 +4120,5 @@ snapshots: yocto-queue@1.1.1: {} zod@3.23.8: {} + + zod@3.24.1: {}