const yup = require('yup');
let schema = yup
.object()
.shape({
// our "basic" requirements
emailAddress: yup.string().required().email(), // email will be validated by yup (required)
red: yup.boolean(),
orange: yup.boolean(),
green: yup.boolean()
})
;
// extended validation
schema = schema.test( // this test is added additional to any other (build-in) tests
'myCustomCheckboxTest',
null, // we'll return error message ourself if needed
(obj) => {
// only testing the checkboxes here
if ( obj.red || obj.orange || obj.green ) {
return true; // everything is fine
}
return new yup.ValidationError(
'❗ Check at least one checkbox',
null,
'myCustomFieldName'
);
}
);
const validateOptions = { abortEarly: false };
const validateMsgGood = '✔ Everything seems fine to me'; // for testing purpose
null;