Skip to content

Alter schema dynamically at runtime #956

Discussion options

You must be logged in to vote

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

const isFormDirty = () => { /* implement */ }

const zodSchema = z.object( {
    someField: z.string().optional().refine( input =>
        isFormDirty() ? z.string().min( 1 ).safeParse( input ).success : true
    )
} )

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@JacobWeisenburger
Comment options

Answer selected by JacobWeisenburger
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants