Closed MarijnMensinga closed 8 months ago
Thank you for the report!
mongoose
throws a "parameter is required" error when its value is set to null
and the field is required, that's why in mongoose-zod
nullish fields are turning in optional fields.
In this particular case, not only I should've marked a nullable field as optional, but also add null
to the list of possible values (turn z.string()
into something like z.union([z.null(), z.string()])
which, would also make a mongoose schema type become Mixed
rather than String
).
In other words, a proper null
support is difficult. Here, I would probably recommend saving an empty string ''
rather than null
(or even undefined
, i.e. an empty field), is that is possible and makes sense.
thanks @andreww2012 for now we made a hacky zod type that fixes this for us.
@MarijnMensinga out of curiosity, mind sharing the "hack"?
@andreww2012 sure!
export const optionalPrimitive = <T extends z.ZodTypeAny>(zObject: T) => {
//TODO: this does not support optional Boolean, must some day be fixed
return zObject
.transform((val) => val || undefined)
.innerType()
.optional()
}
in conjunction with:
export const SomeZodSchema = z.object({
...
// contextId: z.string().nullable().optional(),
contextId: optionalPrimitive(z.string()),
// dateEnd: z.date().nullable().optional(),
dateStart: z.date(),
...
})
Should be fixed in 0.1.2.
It seems that mongoose-zod does not respect the
z.nullable()
(https://zod.dev/?id=nullables) When I use this schema:And create a record like this:
await SomeModel.create({someParam: null})
I receive the error:ValidationError: SomeModel validation failed: someParam: Path 'someParam' is required.
Alternatively if I change the definition of
someParam
to:z.string().nullable().optional()
and create the record with the same code I get the following error:I think the culprit is this line of code, but I'm not entirely sure. https://github.com/andreww2012/mongoose-zod/blob/main/src/to-mongoose.ts#L88
Could you point out if I'm doing something wrong or what is needed to respect the
.nullable()
option?