Skip to content

How to customize the error message of z.coerce.date()? #1851

Answered by JacobWeisenburger
tordans asked this question in Q&A
Discussion options

You must be logged in to vote

Is this what you are looking for?

const schema = z.coerce.date( {
    errorMap: ( { code }, { defaultError } ) => {
        if ( code == 'invalid_date' ) return { message: 'Wrong date format.' }
        return { message: defaultError }
    }
} )

const result = schema.safeParse( '' )
!result.success && console.log( result.error.issues[ 0 ] )
// { code: 'invalid_date', path: [], message: 'Wrong date format.' }

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@tordans
Comment options

@JacobWeisenburger
Comment options

@lsa-jose-israel
Comment options

Answer selected by JacobWeisenburger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants