Search code examples
javascriptreactjstypescriptzod

How to add Zod validation for array elements in react typescript


I am using Zod validation to verify whether the given value is email or not and also checking the minimum length. But I am facing an issue, once the field is empty and clicking on submit button shows the "required" error message but when I add values into the field the error message is still there. I don't know how to remove it.

email: z.string({required_error: "email is required"})
        .email({ message: 'Must be a valid email'})
        .array().length(1, { message: 'Cannot be empty' })

Solution

  • const objScheme = z.object({
    ...
    email: z.string().email()
    ...
    })
    

    This should be enough, as an email can't be valid if the field is completely empty.

    Your .array() introduced an error in your code.