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

Support nested unions when parsing FormData #430

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

fehnomenal
Copy link

I have a schema that looks roughly like this

const weekDaySchema = z.enum(['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']);

const timeSpanSchema = z.object({ from: timeSchema, to: timeSchema });

const weekSchema = z.record(weekDaySchema, z.union([z.literal('default'), z.array(timeSpanSchema)]));

This nested union was not supported for parsing FormData entries.

This PR fixes this use case by trying each union option until the first succeeds or throws an error describing the failed types.

I'm not sure if this breaks anything else. Tests pass and I didn't notice anything off.

Copy link

vercel bot commented May 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
superforms ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 15, 2024 8:52am

@fehnomenal
Copy link
Author

I noticed it does not work correctly when the union is at the top-level of an object. Working on it.

@fehnomenal fehnomenal changed the title Support nested unions Support nested unions when parsing FormData May 15, 2024
@fehnomenal
Copy link
Author

This works now, if the union or exactly on of its options has a default value.

@fehnomenal fehnomenal marked this pull request as ready for review May 15, 2024 08:53
@ciscoheat
Copy link
Owner

Thank you, but I'm not sure when this will be used, since your schema contains nested data and that should not be handled by the form data parser?

If you still think it's needed, can you add some test cases as well, so I get a better idea of what's happening? :)

@ciscoheat ciscoheat added the question Further information is requested label May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants