turkerdev / fastify-type-provider-zod

MIT License
297 stars 19 forks source link

type coercion support #7

Open kibertoad opened 1 year ago

kibertoad commented 1 year ago

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 :)

kibertoad commented 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?

turkerdev commented 1 year ago

I don't think i have used it before.

kibertoad commented 1 year ago

@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.

JaneJeon commented 1 year ago

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?

kibertoad commented 1 year ago

When you are using zod, you are no longer using ajv, so yes

JaneJeon commented 1 year ago

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?

kibertoad commented 1 year ago

it uses zod's serializer and validator instead of ajv's

json schema conversion is only for openapi doc generation purposes

JaneJeon commented 1 year ago

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)?

kibertoad commented 1 year ago

note the schema.parse part. this is zod parsing

JaneJeon commented 1 year ago

RIP

kibertoad commented 1 year ago

Note that we wrote zod coercion library at Lokalise, we will try to open source it ASAP

JaneJeon commented 1 year ago

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

kibertoad commented 1 year ago

@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 :)