Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(conform-zod): zod default support #282

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions packages/conform-zod/coercion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,26 +180,29 @@ export function enableTypeCoercion<Type extends ZodTypeAny>(
});
} else if (type instanceof ZodEffects) {
if (isFileSchema(type)) {
return preprocess((value) => coerceFile(value), type);
schema = preprocess((value) => coerceFile(value), type);
} else {
schema = new ZodEffects({
...type._def,
schema: enableTypeCoercion(type.innerType(), cache),
});
}

schema = new ZodEffects({
...type._def,
schema: enableTypeCoercion(type.innerType(), cache),
});
} else if (type instanceof ZodOptional) {
schema = preprocess(
(value) => coerceString(coerceFile(value)),
(value) => coerceFile(coerceString(value)),
new ZodOptional({
...type._def,
innerType: enableTypeCoercion(type.unwrap(), cache),
}),
);
} else if (type instanceof ZodDefault) {
schema = new ZodDefault({
...type._def,
innerType: enableTypeCoercion(type.removeDefault(), cache),
});
schema = preprocess(
(value) => coerceFile(coerceString(value)),
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/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
type output,
type RefinementCtx,
type SafeParseReturnType,
type ZodCustomIssue,
type ZodTypeAny,
type ZodErrorMap,
type IssueData,
ZodIssueCode,
ZodCustomIssue,
} from 'zod';
import { enableTypeCoercion } from './coercion';

Expand Down
47 changes: 47 additions & 0 deletions tests/conform-zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,53 @@ test.describe('conform-zod', () => {
expect(() => parse(createFormData([]), { schema })).not.toThrow();
});

test('z.default', () => {
const defaultFile = new File(['hello', 'world'], 'example.txt');
const defaultDate = new Date(0);
const schema = z.object({
a: z.string().default('text'),
b: z.number().default(123),
c: z.boolean().default(true),
d: z.date().default(defaultDate),
e: z.instanceof(File).default(defaultFile),
f: z.array(z.string()).default(['foo', 'bar']),
});
const emptyFile = new File([], '');

expect(
parse(
createFormData([
['a', ''],
['b', ''],
['c', ''],
['d', ''],
['e', emptyFile],
['f', ''],
]),
{ schema },
),
).toEqual({
intent: 'submit',
payload: {
a: '',
b: '',
c: '',
d: '',
e: emptyFile,
f: '',
},
value: {
a: 'text',
b: 123,
c: true,
d: defaultDate,
e: defaultFile,
f: ['foo', 'bar'],
},
error: {},
});
});

test('z.lazy', () => {
const category = z.object({
name: z.string({ required_error: 'required' }),
Expand Down
Loading