tristinDLC
tristinDLC
ZZod
Created by unnreaal on 8/27/2024 in #questions
unreal - Hello, i have this schema tsconst som...
@Scott Trinh Wow, I misread OP. I missed the curly braces originally and understood the problem to be that only null was being sent. That's my mistake. Looks like I need a nap haha
9 replies
ZZod
Created by unnreaal on 8/27/2024 in #questions
unreal - Hello, i have this schema tsconst som...
The issue is that the .nullable() and .optional() methods are being applied to the value associated with the "description" key only. The original z.object() that's wrapping your keys is still required to be provided to properly validate. Here is the code that works based on your schema above:
import { z } from 'zod'

const someSchema = z.object({
description: z
.string()
.min(20, 'Min description length is 20')
.max(50, 'Max description length is 50')
.nullable()
.optional(),
})

someSchema.parse({ description: null }) // { description: null }
import { z } from 'zod'

const someSchema = z.object({
description: z
.string()
.min(20, 'Min description length is 20')
.max(50, 'Max description length is 50')
.nullable()
.optional(),
})

someSchema.parse({ description: null }) // { description: null }
9 replies