I'm trying to validate an object conditionally on a Select Option that's been chosen by a user, the issue is I'm rendering a currency list and having immense difficulty trying to make it a required field, as I have to pass in an empty object to start with.
My code stack is React, Formik and Yup (all recent versions).
The Object Schema
category: 'A',
details: {
name: '',
price: 0,
stock: 0,
currency: {
label: '',
code: '',
symbol: '',
alpha_2: '',
}
}
The Yup Schema
category: Yup.string().required('You must pick a category'),
details: Yup.object().when('category', {
is: 'A',
then: Yup.object({
label: Yup.string().required(`Select the currency you're retailing in`),
code: Yup.string().required(`Select the currency you're retailing in`),
symbol: Yup.string().required(`Select the currency you're retailing in`),
alpha_2: Yup.string().required(`Select the currency you're retailing in`),
}),
})
With the above code the form is passing validation and the currency object has a list of empty values ''
, which is an undesired outcome.
How do you make the schema trigger validation?