28 lines
638 B
JavaScript
Raw Normal View History

2020-01-06 16:01:42 +01:00
import * as yup from 'yup';
const createYupSchema = (type, validations, translatedErrors = {}) => {
let schema = yup.mixed();
if (['text'].includes(type)) {
schema = yup.string(translatedErrors.string);
}
Object.keys(validations).forEach(validation => {
const validationValue = validations[validation];
switch (validation) {
case 'required':
schema = schema.required(translatedErrors.required);
break;
case 'regex':
schema = schema.matches(validationValue, translatedErrors.regex);
break;
default:
}
});
return schema;
};
export default createYupSchema;