Let’s say I got a “string” that represents a list of comma separated emails. I need to validate each email individually. I thought that I can do a transformation (from string to array) and then apply the “email” constraint on individual entries. But it doesn’t work…
const schema = yup.object().shape({
emails: yup
.string()
.transform(function (value, originalValue) {
console.log('transform', value);
if (this.isType(value) && value !== null) {
return value;
}
return originalValue ? originalValue.split(/[s,]+/) : [];
})
.array()
.of(yup.string().email(({ value }) => `${value} is not a valid email!`)),
});
The above “array” call doesn’t work (obviously). Please note that the input is of “string” type and this cannot be changed. How can I validate individual entries against a given constraint using Yup?
Any help would be appreciated.