I have to an array of payment_methods. I want to validation each item with different validaiton for example. I have method_type, account number and upi handle. If method type is UPI, then upi handle is required, if its Bank and then account number is required. How can I implement this.
So far I tried various methods. Like when and then in yup but it give branch is not a function error.
To implement the validation logic you described using Yup, you can make use of the .when() function to conditionally apply validations based on the value of another field. However, it's important to correctly structure your Yup schema to avoid errors like the one you encountered with .branch() not being a function, which suggests there might have been a syntax issue or a misunderstanding of the Yup API.
Below is an example of how you can structure your Yup schema to validate an array of payment methods, where each payment method requires different fields based on the method_type value:
This setup should allow you to validate each item in your array of payment methods according to the rules you've specified, without encountering the .branch() error. Remember to install Yup in your project if you haven't already, and adjust the schema as needed to fit your exact validation requirements.