Haddok
Haddok
ZZod
Created by Haddok on 6/6/2024 in #questions
Haddok - Hi everyone , i have to use zod in my ...
13 replies
ZZod
Created by Haddok on 6/6/2024 in #questions
Haddok - Hi everyone , i have to use zod in my ...
oh so if i am not transforming anything , then the shape would work directly ?
13 replies
ZZod
Created by Haddok on 6/6/2024 in #questions
Haddok - Hi everyone , i have to use zod in my ...
yes, my schema is basically adressschema where i have addressline1 2 and 3 , then type , country , state, city ,zip i have a superRefine to validate zip based on countrycode .
13 replies
ZZod
Created by Haddok on 6/6/2024 in #questions
Haddok - Hi everyone , i have to use zod in my ...
Hi there , thank you for your answer So basically i have a schema like this
const testSchema = z.object({
hello: z.string(),
there: z.number(),
this : z.string(),
is: z.number()
})
const testSchema = z.object({
hello: z.string(),
there: z.number(),
this : z.string(),
is: z.number()
})
what i want to do is use the schema to validate the data before i submit the form , which works fine , but also make each key validate on change of their respective input field. i wrote something like this - >
const handleOnchangeInput = (fieldname, value) => {
console.log(testSchema.shape[fieldname].safeParse(value))
setTest({...test, [fieldname] : value})
}
const handleOnchangeInput = (fieldname, value) => {
console.log(testSchema.shape[fieldname].safeParse(value))
setTest({...test, [fieldname] : value})
}
it has to be javascript. but here the error comes that shape is undefined . EDIT: After a few trial and error i found out , that for javascript what i had to change was add a sourcetype call before the shape call --
const handleOnchangeInput = (fieldname, value) => {
console.log(testSchema.sourceType().shape[fieldname].safeParse(value))
setTest({...test, [fieldname] : value})
}
const handleOnchangeInput = (fieldname, value) => {
console.log(testSchema.sourceType().shape[fieldname].safeParse(value))
setTest({...test, [fieldname] : value})
}
Thanks @janglad
13 replies