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 5 commits into
base: main
Choose a base branch
from

Conversation

fehnomenal
Copy link

@fehnomenal fehnomenal commented May 15, 2024

I have a schema that looks roughly like this (updated)

const morningSchema = z.literal('morning').transform(() => ({ from: '06:00', to: '11:00' }));

const noonSchema = z.literal('noon').transform(() => ({ from: '11:00', to: '14:00' }));

const eveningSchema = z.literal('evening').transform(() => ({ from: '14:00', to: '19:00' }));

const schema = z.object({
	time: z.union([morningSchema, noonSchema, eveningSchema])
});

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.


My reasoning for having this is to get validation errors directly in one go. Otherwise I would have to parse it as a string, run a second pass and forward the issues to the main form.

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 Dec 12, 2024 1:32pm

@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
@fehnomenal
Copy link
Author

Oh, I'm sorry for getting back at you so late.

While simplifying the example schema I skipped some stuff. I will create some tests but the gist is that the schema will build more complex objects out of the posted strings.

@fehnomenal
Copy link
Author

OK, I updated the first post, added a playground example and also some tests. Please have another look @ciscoheat

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.

2 participants