Skip to content

Access the list of valid options inside the error message without typescript complaining #1621

Discussion options

You must be logged in to vote

I ended up figuring it out later.

I still can't seem to figure it out for a union of literal strings though.

const enumSchema = z.enum( [
    'item 1',
    'item 2',
    'item 3',
], {
    errorMap: ( issue, ctx ) => {
        if ( issue.code === 'invalid_enum_value' ) return ( {
            message: `${ ctx.data } is not a valid option.
            Please choose a valid option: ${ issue.options }`,
        } )
        return { message: ctx.defaultError }
    }
} )

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
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
1 participant