Skip to content

Commit

Permalink
fix(conform-zod): avoid using preprocess (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung authored Sep 3, 2023
1 parent 2333cd4 commit 7d6a589
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 47 deletions.
2 changes: 1 addition & 1 deletion examples/remix/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

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

82 changes: 47 additions & 35 deletions packages/conform-zod/coercion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
ZodOptional,
ZodDefault,
lazy,
preprocess,
any,
} from 'zod';

/**
Expand Down Expand Up @@ -116,17 +116,22 @@ export function enableTypeCoercion<Type extends ZodTypeAny>(
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);

Expand All @@ -139,13 +144,15 @@ export function enableTypeCoercion<Type extends ZodTypeAny>(

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;
Expand All @@ -160,12 +167,13 @@ export function enableTypeCoercion<Type extends ZodTypeAny>(

// 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]) => [
Expand All @@ -180,29 +188,33 @@ export function enableTypeCoercion<Type extends ZodTypeAny>(
});
} 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,
schema: enableTypeCoercion(type.innerType(), cache),
});
}
} 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,
Expand Down
2 changes: 1 addition & 1 deletion packages/conform-zod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"zod": "^3.21.0"
},
"devDependencies": {
"zod": "^3.21.0"
"zod": "^3.22.2"
},
"keywords": [
"constraint-validation",
Expand Down

0 comments on commit 7d6a589

Please sign in to comment.