turkerdev / fastify-type-provider-zod

MIT License
303 stars 19 forks source link

Validation on `.nullish()` or `.nullable()` doesn't show `null` as a valid option. #87

Open gjstreicher opened 2 months ago

gjstreicher commented 2 months ago

Dependency versions

Node version: 21.7.3 Fastify version: 4.26.2 Zod version: 3.22.4 Fastify-Type-Provider-Zod version: 1.1.9

Issue description

A Zod schema used to validate a JSON object containing a property that can be null, by using the Zod .nullish() or .nullable() methods on the given object schema property, doesn't indicate that the value can be the JSON value of null.

This means that users of an API endpoint with this JSON as it's body won't be able to discover that they can pass null as a valid option.

This is important because null indicates something different to undefined. For example, assigning the value of null to a property of a resource on an update endpoint can indicate that the given property (or attribute) needs to be deleted for the given resource, whilst assigning the value of undefined to the same property (or simply leaving out the property completely) can indicate that the update should leave the given property (or attribute) in tact and unchanged.

JonRC commented 3 weeks ago

Are you talking about the generated open API doc? Are null and undefined not being parsed correctly there?

gjstreicher-ims commented 3 weeks ago

Are you talking about the generated open API doc? Are null and undefined not being parsed correctly there?

Nope, I'm just talking about the error that is returned from the validation of the passed data failing against the given schema.

JonRC commented 3 weeks ago

Could you share a code example that throws this error?