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): multi select coercion #447

Merged
merged 1 commit into from
Feb 12, 2024
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
2 changes: 1 addition & 1 deletion packages/conform-zod/coercion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export function enableTypeCoercion<Schema extends ZodTypeAny>(

if (
typeof value === 'undefined' ||
typeof coerceFile(value) === 'undefined'
typeof coerceFile(coerceString(value)) === 'undefined'
) {
return [];
}
Expand Down
24 changes: 18 additions & 6 deletions tests/conform-zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,21 @@ describe('conform-zod', () => {
error: { test: ['min'] },
reply: expect.any(Function),
});
// Scenario: Multiple select (default option is empty string)
expect(
parseWithZod(createFormData([['test', '']]), {
schema: createSchema(),
}),
).toEqual({
status: 'error',
payload: {
test: '',
},
error: {
test: ['min'],
},
reply: expect.any(Function),
});
// Scenario: Checkbox group (Checked only one item)
expect(
parseWithZod(createFormData([['test', 'a']]), {
Expand Down Expand Up @@ -630,12 +645,12 @@ describe('conform-zod', () => {
});
// Scenario: Only one input with the specific name
expect(
parseWithZod(createFormData([['test', '']]), {
parseWithZod(createFormData([['test[0]', '']]), {
schema: createSchema(),
}),
).toEqual({
status: 'error',
payload: { test: '' },
payload: { test: [''] },
error: {
'test[0]': ['required'],
},
Expand Down Expand Up @@ -807,10 +822,7 @@ describe('conform-zod', () => {
c: undefined,
d: undefined,
e: undefined,
f: [undefined],
// Ideally, this should be `[undefined]` as well similar to `f` above.
// However, the preprocess on optional array casts it to undefined before the array preprocess is called
// As it is still unclear when we wants an optional array, we are not going to fix this for now.
f: [],
g: undefined,
},
reply: expect.any(Function),
Expand Down
Loading