From 95b80d91bb73331f6e3e470984f1e13918b795cc Mon Sep 17 00:00:00 2001 From: Edmund Hung Date: Sun, 3 Sep 2023 13:42:51 +0200 Subject: [PATCH] fix(conform-zod): avoid using preprocess --- examples/remix/package.json | 2 +- package-lock.json | 20 ++++---- packages/conform-zod/coercion.ts | 82 ++++++++++++++++++------------- packages/conform-zod/package.json | 2 +- 4 files changed, 59 insertions(+), 47 deletions(-) diff --git a/examples/remix/package.json b/examples/remix/package.json index cc125325..bbb651f3 100644 --- a/examples/remix/package.json +++ b/examples/remix/package.json @@ -18,7 +18,7 @@ "isbot": "latest", "react": "^18.2.0", "react-dom": "^18.2.0", - "zod": "^3.21.0" + "zod": "^3.22.2" }, "devDependencies": { "@remix-run/dev": "^1.19.3", diff --git a/package-lock.json b/package-lock.json index d6e39f3a..7731a381 100644 --- a/package-lock.json +++ b/package-lock.json @@ -250,7 +250,7 @@ "isbot": "latest", "react": "^18.2.0", "react-dom": "^18.2.0", - "zod": "^3.21.0" + "zod": "^3.22.2" }, "devDependencies": { "@remix-run/dev": "^1.19.3", @@ -30031,9 +30031,9 @@ } }, "node_modules/zod": { - "version": "3.21.0", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.21.0.tgz", - "integrity": "sha512-UYdykTcVxB6lfdyLzAqLyyYAlOcpoluECvjsdoaqfQmz9p+3LRaIqYcNiL/J2kFYp66fBM8wwBvIGVEjq7KtZw==", + "version": "3.22.2", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.22.2.tgz", + "integrity": "sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg==", "funding": { "url": "https://github.com/sponsors/colinhacks" } @@ -30086,7 +30086,7 @@ "version": "0.8.0", "license": "MIT", "devDependencies": { - "zod": "^3.21.0" + "zod": "^3.22.2" }, "peerDependencies": { "@conform-to/dom": "0.8.0", @@ -32495,7 +32495,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "typescript": "^4.9.5", - "zod": "^3.21.0" + "zod": "^3.22.2" } }, "@conform-to/dom": { @@ -32545,7 +32545,7 @@ "@conform-to/zod": { "version": "file:packages/conform-zod", "requires": { - "zod": "^3.21.0" + "zod": "^3.22.2" } }, "@csstools/normalize.css": { @@ -51268,9 +51268,9 @@ } }, "zod": { - "version": "3.21.0", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.21.0.tgz", - "integrity": "sha512-UYdykTcVxB6lfdyLzAqLyyYAlOcpoluECvjsdoaqfQmz9p+3LRaIqYcNiL/J2kFYp66fBM8wwBvIGVEjq7KtZw==" + "version": "3.22.2", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.22.2.tgz", + "integrity": "sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg==" }, "zwitch": { "version": "2.0.2", diff --git a/packages/conform-zod/coercion.ts b/packages/conform-zod/coercion.ts index cc8f45ca..994802f3 100644 --- a/packages/conform-zod/coercion.ts +++ b/packages/conform-zod/coercion.ts @@ -24,7 +24,7 @@ import { ZodOptional, ZodDefault, lazy, - preprocess, + any, } from 'zod'; /** @@ -116,17 +116,22 @@ export function enableTypeCoercion( type instanceof ZodEnum || type instanceof ZodNativeEnum ) { - schema = preprocess((value) => coerceString(value), type); + schema = any() + .transform((value) => coerceString(value)) + .pipe(type); } else if (type instanceof ZodNumber) { - schema = preprocess((value) => coerceString(value, Number), type); + schema = any() + .transform((value) => coerceString(value, Number)) + .pipe(type); } else if (type instanceof ZodBoolean) { - schema = preprocess( - (value) => coerceString(value, (text) => (text === 'on' ? true : text)), - type, - ); + schema = any() + .transform((value) => + coerceString(value, (text) => (text === 'on' ? true : text)), + ) + .pipe(type); } else if (type instanceof ZodDate) { - schema = preprocess( - (value) => + schema = any() + .transform((value) => coerceString(value, (timestamp) => { const date = new Date(timestamp); @@ -139,13 +144,15 @@ export function enableTypeCoercion( return date; }), - type, - ); + ) + .pipe(type); } else if (type instanceof ZodBigInt) { - schema = preprocess((value) => coerceString(value, BigInt), type); + schema = any() + .transform((value) => coerceString(value, BigInt)) + .pipe(type); } else if (type instanceof ZodArray) { - schema = preprocess( - (value) => { + schema = any() + .transform((value) => { // No preprocess needed if the value is already an array if (Array.isArray(value)) { return value; @@ -160,12 +167,13 @@ export function enableTypeCoercion( // Wrap it in an array otherwise return [value]; - }, - new ZodArray({ - ...type._def, - type: enableTypeCoercion(type.element, cache), - }), - ); + }) + .pipe( + new ZodArray({ + ...type._def, + type: enableTypeCoercion(type.element, cache), + }), + ); } else if (type instanceof ZodObject) { const shape = Object.fromEntries( Object.entries(type.shape).map(([key, def]) => [ @@ -180,7 +188,9 @@ export function enableTypeCoercion( }); } else if (type instanceof ZodEffects) { if (isFileSchema(type)) { - schema = preprocess((value) => coerceFile(value), type); + schema = any() + .transform((value) => coerceFile(value)) + .pipe(type); } else { schema = new ZodEffects({ ...type._def, @@ -188,21 +198,23 @@ export function enableTypeCoercion( }); } } else if (type instanceof ZodOptional) { - schema = preprocess( - (value) => coerceFile(coerceString(value)), - new ZodOptional({ - ...type._def, - innerType: enableTypeCoercion(type.unwrap(), cache), - }), - ); + schema = any() + .transform((value) => coerceFile(coerceString(value))) + .pipe( + new ZodOptional({ + ...type._def, + innerType: enableTypeCoercion(type.unwrap(), cache), + }), + ); } else if (type instanceof ZodDefault) { - schema = preprocess( - (value) => coerceFile(coerceString(value)), - new ZodDefault({ - ...type._def, - innerType: enableTypeCoercion(type.removeDefault(), cache), - }), - ); + schema = any() + .transform((value) => coerceFile(coerceString(value))) + .pipe( + new ZodDefault({ + ...type._def, + innerType: enableTypeCoercion(type.removeDefault(), cache), + }), + ); } else if (type instanceof ZodIntersection) { schema = new ZodIntersection({ ...type._def, diff --git a/packages/conform-zod/package.json b/packages/conform-zod/package.json index 6458b145..86c96325 100644 --- a/packages/conform-zod/package.json +++ b/packages/conform-zod/package.json @@ -29,7 +29,7 @@ "zod": "^3.21.0" }, "devDependencies": { - "zod": "^3.21.0" + "zod": "^3.22.2" }, "keywords": [ "constraint-validation",