I'm working with yup validation and trying to build a conditional validation object.
My question is, How do I add the required and match objects to the Yup.string() object without hard coding it. Similar to how you might chain jQuery functions.
Here is an example of what i'm trying to achieve:
if (field.required) {
valSchema[id] = Yup.string().required(errorText[id].default);
}
if (field.validation) {
valSchema[id] = Yup.string().matches(re, field.validation[0].message);
}
if (field.otherValidation) {
valSchema[id] = Yup.string().matches(re, field.validation[1].message);
}
Obviously this isn't going to work because the last condition which is true will overwrite the previous condition.
Then the end result of what would be like this if all the conditions are true.
valSchema[id] = Yup.string()
.required(errorText[id].default)
.matches(reExp, field.validation[0].message);
.matches(reExp1, field.validation[1].message);
any ideas?
Thanks.