Alter schema dynamically at runtime #956
-
Through a series of unfortunate events, it seems that the previous question with this same title was deleted by accident. I am very sorry about this. If you were the person that posted that discussion, could you repost what you had and I will answer it again? Again, very sorry for this inconvenience. The previous link was: https://github.com/colinhacks/zod/discussions/943. I hope this helps. Maybe there is someway to get the info from your browser cache. My browser didn't have this cached, so I was unable to. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Answer was const isFormDirty = () => { /* implement */ }
const zodSchema = z.object( {
someField: z.string().optional().refine( input =>
isFormDirty() ? z.string().min( 1 ).safeParse( input ).success : true
)
} ) |
Beta Was this translation helpful? Give feedback.
-
in reference to #938 which is still available, but with the difference of having an outside condition for deciding which schema to use. |
Beta Was this translation helpful? Give feedback.
Answer was
•if the form is dirty, someField should be z.string().min( 1 ) (not empty string)
•if the form is not dirty, someField should be optional