Z
Zod2mo ago
Haddok

Haddok - hi , i have defined a schema where i a...

hi , i have defined a schema where i am accessing a few fields using shape for some specific scenario i tried using myschema.shape[fieldname].safeparse(value) this throws error that myschema.shape[fieldname] is undefined is there any way to access shapes with passed value ? for context myschema.shape.name.safeparse(value) works fine, i just want to create a generic function for different fields of the form
2 Replies
janglad
janglad2mo ago
With an error, I assume you mean a Typescript one? Cause yea you’ll have to type these as being a key off shape and possibly do some casting
tristinDLC
tristinDLC2mo ago
Are you trying to do something like:
import { z } from 'zod'

const object = z.object({
test: z.string(),
})

console.log(object.shape.test.safeParse('Fizz Buzz'))
import { z } from 'zod'

const object = z.object({
test: z.string(),
})

console.log(object.shape.test.safeParse('Fizz Buzz'))
Want results from more Discord servers?
Add your server