janglad - Okay another question 😅 Why does t...
Okay another question 😅
Why does this return a ZodEffect with a possible unnkown?
18 Replies
I don't remember the signature there but I think that is the input type, which makes sense for preprocess
ooh huh yes you're right
this does indeed work as expected
altho somehow it does show up as unknown here, which is fixed by using that
as
that is commented right nowYeah the output type is string so the type of the parsed value is string
Every schema has an input and output type
oohhh yeah I am indeed using z.input
hmm this is somewhat annoying lol cause IIRC I do need to do that as there are also transforms
but then I'd need the output of that preprocess specifically 😄
altho actually no using
z.output
it still shows up as unknown there
the schema setup is a bit complex so I'll need to do some digging as to where that's happeningWhat's the schema for
street
here?that stringOrEmptyToNull
but yes in the end it is indeed caused by using z.input which makes sense, output was just giving a different error which also makes sense
but I guess I can just lie to TS here and use the as
Are you trying to construct a value and using the type as a type annotation without parsing it?
It's being used like this
Ahhh. Right. I wonder if instead of preprocess you should have a transform that checks for the empty string so that the input and output type is string | null still 🤔
that's not really ideal either. RHF sends an empty input as an empty string, if I then want to add on things like a min length I wouldn't be able to do say
z.string().min(5).nullable().transform(emptyToNull)
as when you pass it an empty string it complains about not being 5 long
and while I can check for that in a refine, those only get triggered after all the other fields are valid so that's not great UX eitherr
mind you I can't chain the min and such now either with the predefined variable but it is possible to do in a preprocess
You could do something like that maybe?
Or:
second one would still have the issue of only validating after all the rest right
but I had not thought of the first!
that might actually be the most elegant solutioN!
I'm not sure what you mean here?
transforms and refines only run once other fields are valid right
It will be run in this case as long as it's a string
maybe you're thinking about refines/transforms on an object (which requires that all of the properties pass their validations)?
yes I am indeed 😄
fwiw, I find the pipe implementation to be a bit more "accurate" to what you're trying to achieve in mapping an empty string to
null
and then adding addtional string constraints on any remaining strings, but both seem pretty readable.
fwiw, i feel like these days pipe
and coerce
pretty much replace most use cases for preprocess
.