Closed abastardi closed 1 year ago
That's a good catch. As a workaround until I've examined this better, does this work?
z.nativeEnum(State).default('' as State)
Nice idea -- yes, that works. Thanks.
Great! There's a note about this (ab)use of the type system here: https://superforms.vercel.app/default-values#changing-a-default-value
Closing this, I think the workaround is the only thing to do if the general type safety of the library is to be kept.
Note, it might be worth mentioning this issue and the workaround in the Tainted form check section of the docs, as it may not be obvious to users why they are getting the taintedMessage
alert unexpectedly (there is no clear indication that the <select>
is what is tainted nor why it has become tainted, so it won't be obvious that this workaround is even needed).
Yes, will add that to the docs.
I have an address form with the
state
property of the Zod schema defined like:In the form, the
state
field is aselect
element with the first option defined as:The problem is that
superValidate
generatesundefined
as the value of$form.state
, but when bound to theselect
element, that immediately gets changed to the empty string, which marks the form as tainted. This means even without touching the form, if the user attempts to navigate away, they will see thetaintedMessage
alert.To avoid this problem, it would be helpful to be able to specify the default value for enums as being the empty string rather than
undefined
(possibly via a configuration option). For example, the following schema works as expected:Of course, it's not ideal to have to specify the schema that way, as it is not obvious why it is written that way, and technically it would allow an empty string to be submitted even though that value should not be allowed.
Would adding an option to handle this case be possible, or is there a better solution?