Open kibertoad opened 1 year ago
@turkerdev finishing PR for https://github.com/turkerdev/fastify-type-provider-zod/issues/6, can try doing this afterwards, but I can't find any examples on how preprocess is supposed to be used, do you maybe have some knowledge about it?
I don't think i have used it before.
@turkerdev Here are my findings: https://github.com/colinhacks/zod/issues/1316
I don't think that this can/should be implemented on the type-provider side.
Dumb question: as outlined here https://www.fastify.io/docs/latest/Reference/Validation-and-Serialization/, fastify uses ajv, and fastify's ajv default in fact coerces types to whatever JSON Schema is ultimately put into fastify.
So is there even a need to do this at the zod (or this provider) level? Or am I missing something here?
When you are using zod, you are no longer using ajv, so yes
Hmm, but ultimately this package spits out JSON Schema which is then fed to fastify, no? I thought this package was the equivalent to "pre-compiling" zod to JSON Schema which then gets plugged into fastify, which would need ajv to handle it, no?
it uses zod's serializer and validator instead of ajv's
json schema conversion is only for openapi doc generation purposes
Ah, dang. (I'm continuing to hope here) what about here? https://github.com/turkerdev/fastify-type-provider-zod/blob/main/src/index.ts#L86
When fastify parses the raw body (a string) into a data object to be passed to the validatorCompiler
, isn't ajv involved there (given that fastify iirc does "fast parsing" which iirc is just JSON Schema stuffed into ajv)? Or is the data
here passed by fastify just a result of a JSON.parse
(or equivalent)?
note the schema.parse part. this is zod parsing
RIP
Note that we wrote zod coercion library at Lokalise, we will try to open source it ASAP
Is it this? https://github.com/lokalise/zod-extras/blob/main/docs/type-coercion.md
Furthermore, it seems that coercion is now "free" for 99% of use cases, at the zod schema level? https://github.com/colinhacks/zod#coercion-for-primitives
@JaneJeon yeah, that's the one. ours is replicating ajv behaviour in coercing, zod does its own thing, so you may pick whichever fits your use-case :)
ajv has this: https://ajv.js.org/coercion.html zod has this: https://github.com/colinhacks/zod#preprocess
I wonder if it is possible to build similar auto-coercion system on top of preprocess within the fastify-type-provider-zod validator.
I can try building it, but could use some help with that as well :)