mirror of
https://github.com/strapi/strapi.git
synced 2025-09-06 15:22:59 +00:00
28 lines
638 B
JavaScript
28 lines
638 B
JavaScript
![]() |
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;
|