Z
Zod•14mo ago
Valency

i ve got a question about `pipe` and

i've got a question about pipe and coerce. the shape of my data is a bit funky so sorry if it's a bit confusing. the input for the schema is an array of strings, that will either a number like ["7"] or an iso date (without time data), like ["2023-01-01", "2023-02-02"]. i've currently got the schema as: array(string().pipe(union([coerce.number(), coerce.date()]))) with the intention that it will accept a string, then coerce it to either a number or a date. the coercion itself is working fine, however the inferred type of this schema is a bit puzzling to me, as it's telling me the type of the schema is number[] | Date[], when i would have thought it would be string[] because i'm explicitly feeding string() into array(). i'm assuming this is intended behaviour but i guess i'm just curious why it isn't string[] when i'm explicitly stating that it should be
5 Replies
Valency
Valency•14mo ago
No description
Valency
Valency•14mo ago
trying with transform i get a similar result. my thinking must be incorrect 😅 maybe someone knows how i should be approaching this
No description
Svish
Svish•14mo ago
Not sure I understand, but try to use z.input instead of z.infer (which just another name for z.output)?
Valency
Valency•14mo ago
the infer is irrelevant here, sorry i shouldn't have included it in my screenshot edit: it was extremely relevant 😅 aha! i see what you're saying i wasn't aware that z.input existed, this is exactly what i want. thank you very much 😄
Svish
Svish•14mo ago
There you go Have to say I kind of wish zod would've just not included z.infer to begin with, as z.input and z.output is just so much clearer about what it does. maybe z.input and z.output actually wasn't added until later though, so maybe z.infer is the original and therefore has to be kept for backwards compatibility, but yeah