You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently evaluating a move from TypeBox to Valibot for form validation because dealing with the validation of unions in TypeBox is complex, and potentially maintaining two schemas for input and output is undesirable. We use Value.Create(Schema) to obtain the initial state of our form from our "single source of truth" about the shape of the form state object. I was attempting to re-create this with Valibot.
In React + MUI, we cannot (or should not) set the initial value of our form fields to undefined unless we intend to operate them uncontrolled, which is discouraged.
Workaround
Using a function to supply the default value renders the desired result.
The text was updated successfully, but these errors were encountered:
dboune
changed the title
getDefaults returns undefined for optional with a default of empty string
getDefaults returns undefined for optional with a default of empty string or false
Jan 7, 2024
Thanks for reporting this bug. I will change the if-statement to schema.default !== undefined and release this change with the next version. Until then, a function can be used as a workaround when passing empty strings.
Problem
getDefaults
returns undefined for anoptional
with a default set to an empty string orfalse
. I believe it should return the value supplied.Note: This same issue applies to
getFallbacks
withfallback
. In all cases,parse
does the right thing.Example:
I believe the reason for the current behavior may be here:
https://github.com/fabian-hiller/valibot/blob/6578c3304062e6c2e5cb14fc0f4d6dbb1e6eaffb/library/src/methods/getDefaults/getDefaults.ts#L34C3-L34C24
Rationale
I'm currently evaluating a move from TypeBox to Valibot for form validation because dealing with the validation of unions in TypeBox is complex, and potentially maintaining two schemas for input and output is undesirable. We use
Value.Create(Schema)
to obtain the initial state of our form from our "single source of truth" about the shape of the form state object. I was attempting to re-create this with Valibot.In React + MUI, we cannot (or should not) set the initial value of our form fields to undefined unless we intend to operate them uncontrolled, which is discouraged.
Workaround
Using a function to supply the default value renders the desired result.
Misc
Valibot looks very nice overall. Thank you!
The text was updated successfully, but these errors were encountered: