here is my object.
food:{
veg:{
name:, price:
},
non_veg:{
name:, price:
}
}
how to validate the object, as there is options for array validation only.
Asked
Active
Viewed 1,664 times
1 Answers
1
You can use yup like this:
let schema = yup.object().shape({
food: yup.object().shape({
veg:yup.object().shape({
name:yup.string().required(),
price:yup.number().required(),
}),
non_veg:yup.object().shape({
name:yup.string().required(),
price:yup.number().required(),
})
})
});
And you can build your dynamic object like:
let dynamic = yup.object().shape({
food: yup.object().shape({
[dynamicString]:yup.object().shape({
name:yup.string().required(),
price:yup.number().required(),
}),
[dynamicString]:yup.object().shape({
name:yup.string().required(),
price:yup.number().required(),
})
})
});

Yagizhan Avci
- 44
- 3
-
thanks, what if the object inside the food is dynamic. i.e. veg, non_veg. – suvop Sep 07 '20 at 09:49
-
I have edited my answer according to your needs. Don't forget to store it in your state. – Yagizhan Avci Sep 07 '20 at 09:56
-
Also you can use this link https://stackoverflow.com/a/61971503/12779513 to get more information. – Yagizhan Avci Sep 07 '20 at 09:57