-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
Enums without default values #341
Comments
It's a matter of type-safety - if the type is I'm considering adding an |
Thanks ! I’ll wait to see how you handle this 👍 |
To make it work with all validation libraries, you have to be able to map the inferred schema type T to a type that extends certain fields in it with for example undefined or even an empty string. But I don't think that's the whole solution, as you need to specify the default (empty) values somehow too. The best workaround I can come up with is to modify the adapter defaults. const schema = z.object({
dir: z.enum(['north', 'south', 'east', 'west'])
});
const adapter = zod(schema);
adapter.defaults.dir = '' as 'north';
const form = await superValidate(adapter); |
Don’t you think that it may run into the same issue ? I’m not sure to understand the role of the adapter. If If the user click on submit he won’t have an HTML alert with a tooltip on the select field, but he’s going to have a Zod error message telling the value doesn’t fit the enum. |
Modifying the adapter defaults makes it possible to have a required enum field, while still having a default value that isn't part of the enum.
|
Looks like this (at least the specific issue I was encountering) may have been fixed in 2.6.1 |
This also works in the server, but it would be very nice if it was handled natively by superForms: // +page.server.ts
const form = await superValidate(zod(mySchema));
form.data.dir = undefined as unknown as Direction;
return { form } |
Our app has giant forms with dozens of required select inputs, so having to do this manually for each form is a pain. Also, we have a handful of large forms that will autosave a draft using a partial schema and validate a full schema and publish a record on submit. I will let you know what that mess looks like once I start migrating those larger forms over. |
I suggest making a helper function/wrapper for |
Best workaround currently, which keeps things in the schema and with the same type, so it won't cause a native type error: const schema = z.object({
dir: z.enum(['north', 'south', 'east', 'west']).default('' as 'north')
}); |
Thanks but this is dirty x) |
You can't have it both ways, if you want type-safety you must add |
This is type-safe: const schema = z.object({
dir: z
.enum(['', 'north', 'south', 'east', 'west'])
.refine((dir) => dir != '', 'No direction specified')
}); |
Thanks for repairing #333
But would it be possible to not have default values for enums ?
I would like to have my select fields empty when the page load.
Instead I’m forced to set values to undefined with typescript errors in the script tags.
Severity : Minor
The text was updated successfully, but these errors were encountered: