I'm working with yup and react-hook-forms to validate a user submission. I'm creating an application that sends text messages, emails, and push notifications. The user can specify where they would like to be contacted via a set of checkboxes. These 3 checkboxes update a single stateful object that looks like this by default:
const [checkboxes, setCheckboxes] = useState({
phone: false,
email: false,
push: false,
});
I need to validate that at least one of these is changed to true before the form can be submitted and if not, I'd like to throw an error with a message. To that end I found the .test function of yup and am trying the following:
fieldset: yup
.object()
.shape({
phone: yup.bool(),
email: yup.bool(),
push: yup.bool(),
})
.required()
.test(
"comms-Selected",
"Please specify at least one means to be contacted",
(value) =>
value.phone === true || value.email === true || value.push === true
),
I'm not sure about the syntax of this validator function and the yup documentation is making my head spin. I know that it's not working because I can submit the form with all fields unchecked. Can someone help me to understand how to properly write this custom validator?