fastify-type-provider-zod icon indicating copy to clipboard operation
fastify-type-provider-zod copied to clipboard

type coercion support

Open kibertoad opened this issue 3 years ago • 13 comments
trafficstars

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 avatar Aug 05 '22 12:08 kibertoad

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

kibertoad avatar Aug 05 '22 12:08 kibertoad

I don't think i have used it before.

turkerdev avatar Aug 05 '22 14:08 turkerdev

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

kibertoad avatar Aug 05 '22 15:08 kibertoad

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?

JaneJeon avatar Nov 05 '22 23:11 JaneJeon

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

kibertoad avatar Nov 05 '22 23:11 kibertoad

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?

JaneJeon avatar Nov 05 '22 23:11 JaneJeon

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

json schema conversion is only for openapi doc generation purposes

kibertoad avatar Nov 05 '22 23:11 kibertoad

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

JaneJeon avatar Nov 05 '22 23:11 JaneJeon

note the schema.parse part. this is zod parsing

kibertoad avatar Nov 05 '22 23:11 kibertoad

RIP

JaneJeon avatar Nov 05 '22 23:11 JaneJeon

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

kibertoad avatar Nov 05 '22 23:11 kibertoad

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 avatar May 25 '23 23:05 JaneJeon

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

kibertoad avatar May 26 '23 05:05 kibertoad