plumbe0
Explore posts from serversZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
so i keep the default invalid option "please choose ..." always selected and then if there is a previous valid selection it will automatically override, otherwise it will fallback to the default disabled option
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
fixed with god's intended language: HTML. multiple options can be selected but only the last one will be truly selected (its text will appear in the UI and its value will be sent)
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
Ah, I'm sorry, I'm using the validator middleware of Hono web framework. It uses .spa() under the hood and i think returns the input as result.data even if result.success === false. I guess I'm on my own for this one, as zod does not return a "partial" parsed object
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
It seems to work for my initial testing... But I'm having some trouble with the UI: my form page backend uses
safeParse()
and if !result.success
returns the form page pre-populated with result.data
which is... inconsistent, because i can check the checkbox for haveRoom
and not choose any valid option from the <select>
associated to roomNumber
, in which case the backend receives an incosistent object with any of the schemas defined. Is it a common problem? How is it usually solved? Thanks again for the incredible patience and help you've given me72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
That's wild, thank you! I'll try to integrate this huge help into my code. I would gladly read your blog post about this 🙂
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
I'm assuming typescript's undefined means the property might be missing on the input data so the equivalent of zod's
.optional()
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
sqlite does not have boolean, uses 0 or 1 integer. so z.coerce.boolean() was working fine.
so all in all
haveX: "on" | 0 | 1 | undefined
so zod should parse true if value is "on" or 1 , false if 0 or undefined72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
in short the input comes either from a form or from a sqlite db.
haveA, haveB, haveC are checkboxes so as per HTML standard if they're not checked they are not sent on form submit; if they're checked the key/value pair is sent and the default value is the string "on" if not otherwise specified. I'll send some input examples as soon as i get back to my computer
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
thank you very much for support. the problem with using
z.literal()
instead of z.coerce.boolean()
is that the haveX properties come from parsing FormData checkboxes, and they come with the default "on" value... so maybe it's better to z.literal("on").pipe(z.literal(true))
to transform them?72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
sorry for the huge link, I don't know if discord supports markdown
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
Thanks, i get this at runtime though:
error: A discriminator value for key
haveA
could not be extracted from all schema options72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
@Scott Trinh sorry to bother you again, how would i pipe this to the discriminated union(s?) to get the better error reporting? Thanks
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
This worked at last:
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
would this work for more with/without cases? i can just extend BaseSchema more times and then union all of those?
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
This will work nice until v4 comes out 🙂
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
that's it! Thank you very much! You've helped me immensely
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
I expected this to work, but it doesn't:
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
just tried, this still doesn't work 😢
72 replies
ZZod
•Created by plumbe0 on 9/5/2024 in #questions
Discriminated union default value on the discriminator
If i was to only use union like you suggested here, how would i do it?
72 replies