2019-07-25 15:52:30 +02:00
|
|
|
import {
|
|
|
|
|
get,
|
|
|
|
|
isBoolean,
|
|
|
|
|
isNaN,
|
|
|
|
|
isNumber,
|
|
|
|
|
isNull,
|
|
|
|
|
isArray,
|
|
|
|
|
isObject,
|
|
|
|
|
} from 'lodash';
|
2019-07-18 16:53:12 +02:00
|
|
|
import * as yup from 'yup';
|
|
|
|
|
|
|
|
|
|
const errorsTrads = {
|
|
|
|
|
email: 'components.Input.error.validation.email',
|
|
|
|
|
json: 'components.Input.error.validation.json',
|
|
|
|
|
max: 'components.Input.error.validation.max',
|
|
|
|
|
maxLength: 'components.Input.error.validation.maxLength',
|
|
|
|
|
min: 'components.Input.error.validation.min',
|
|
|
|
|
minLength: 'components.Input.error.validation.minLength',
|
|
|
|
|
regex: 'components.Input.error.validation.regex',
|
|
|
|
|
required: 'components.Input.error.validation.required',
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-19 09:32:36 +02:00
|
|
|
const getAttributes = data => get(data, ['schema', 'attributes'], {});
|
2019-07-18 16:53:12 +02:00
|
|
|
|
2019-07-19 09:32:36 +02:00
|
|
|
const createYupSchema = (model, { groups }) => {
|
|
|
|
|
const attributes = getAttributes(model);
|
2019-07-18 16:53:12 +02:00
|
|
|
|
2019-07-19 09:32:36 +02:00
|
|
|
return yup.object().shape(
|
|
|
|
|
Object.keys(attributes).reduce((acc, current) => {
|
|
|
|
|
const attribute = attributes[current];
|
|
|
|
|
if (attribute.type !== 'relation' && attribute.type !== 'group') {
|
|
|
|
|
const formatted = createYupSchemaAttribute(attribute.type, attribute);
|
|
|
|
|
acc[current] = formatted;
|
|
|
|
|
}
|
2019-07-18 16:53:12 +02:00
|
|
|
|
2019-07-19 09:32:36 +02:00
|
|
|
if (attribute.type === 'relation') {
|
|
|
|
|
acc[current] = [
|
|
|
|
|
'oneWay',
|
|
|
|
|
'oneToOne',
|
|
|
|
|
'manyToOne',
|
|
|
|
|
'oneToManyMorph',
|
|
|
|
|
'oneToOneMorph',
|
|
|
|
|
].includes(attribute.relationType)
|
2019-07-25 15:52:30 +02:00
|
|
|
? yup.object().nullable()
|
|
|
|
|
: yup.array().nullable();
|
2019-07-19 09:32:36 +02:00
|
|
|
}
|
2019-07-18 16:53:12 +02:00
|
|
|
|
2019-07-19 09:32:36 +02:00
|
|
|
if (attribute.type === 'group') {
|
|
|
|
|
let groupSchema = createYupSchema(groups[attribute.group], {
|
|
|
|
|
groups,
|
|
|
|
|
});
|
|
|
|
|
groupSchema =
|
|
|
|
|
attribute.repeatable === true
|
|
|
|
|
? yup.array().of(groupSchema)
|
|
|
|
|
: groupSchema;
|
|
|
|
|
groupSchema =
|
|
|
|
|
attribute.required === true ? groupSchema.required() : groupSchema;
|
|
|
|
|
acc[current] = groupSchema;
|
|
|
|
|
}
|
|
|
|
|
return acc;
|
|
|
|
|
}, {})
|
2019-07-18 16:53:12 +02:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
const createYupSchemaAttribute = (type, validations) => {
|
|
|
|
|
let schema = yup.mixed();
|
|
|
|
|
if (['string', 'text', 'email', 'password', 'enumeration'].includes(type)) {
|
|
|
|
|
schema = yup.string();
|
|
|
|
|
}
|
|
|
|
|
if (type === 'json') {
|
2019-07-25 15:52:30 +02:00
|
|
|
schema = yup
|
|
|
|
|
.mixed(errorsTrads.json)
|
|
|
|
|
.test('isJSON', errorsTrads.json, value => {
|
|
|
|
|
try {
|
|
|
|
|
if (
|
|
|
|
|
isObject(value) ||
|
|
|
|
|
isBoolean(value) ||
|
|
|
|
|
isNumber(value) ||
|
|
|
|
|
isArray(value) ||
|
|
|
|
|
isNaN(value) ||
|
|
|
|
|
isNull(value)
|
|
|
|
|
) {
|
|
|
|
|
JSON.parse(JSON.stringify(value));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.nullable();
|
2019-07-18 16:53:12 +02:00
|
|
|
}
|
2019-07-25 15:52:30 +02:00
|
|
|
|
2019-07-18 16:53:12 +02:00
|
|
|
if (type === 'email') {
|
|
|
|
|
schema = schema.email(errorsTrads.email);
|
|
|
|
|
}
|
|
|
|
|
if (type === 'number') {
|
|
|
|
|
schema = yup
|
|
|
|
|
.number()
|
|
|
|
|
.transform(cv => (isNaN(cv) ? undefined : cv))
|
|
|
|
|
.typeError();
|
|
|
|
|
}
|
|
|
|
|
if (['date', 'datetime'].includes(type)) {
|
|
|
|
|
schema = yup.date().typeError();
|
|
|
|
|
}
|
|
|
|
|
Object.keys(validations).forEach(validation => {
|
|
|
|
|
const validationValue = validations[validation];
|
|
|
|
|
if (
|
|
|
|
|
!!validationValue ||
|
|
|
|
|
((!isBoolean(validationValue) &&
|
|
|
|
|
Number.isInteger(Math.floor(validationValue))) ||
|
|
|
|
|
validationValue === 0)
|
|
|
|
|
) {
|
|
|
|
|
switch (validation) {
|
|
|
|
|
case 'required':
|
|
|
|
|
schema = schema.required(errorsTrads.required);
|
|
|
|
|
break;
|
|
|
|
|
case 'max':
|
|
|
|
|
schema = schema.max(validationValue, errorsTrads.max);
|
|
|
|
|
break;
|
|
|
|
|
case 'maxLength':
|
|
|
|
|
schema = schema.max(validationValue, errorsTrads.maxLength);
|
|
|
|
|
break;
|
|
|
|
|
case 'min':
|
|
|
|
|
schema = schema.min(validationValue, errorsTrads.min);
|
|
|
|
|
break;
|
|
|
|
|
case 'minLength':
|
|
|
|
|
schema = schema.min(validationValue, errorsTrads.minLength);
|
|
|
|
|
break;
|
|
|
|
|
case 'regex':
|
|
|
|
|
schema = schema.matches(validationValue, errorsTrads.regex);
|
|
|
|
|
break;
|
|
|
|
|
case 'lowercase':
|
|
|
|
|
if (['text', 'textarea', 'email', 'string'].includes(type)) {
|
|
|
|
|
schema = schema.strict().lowercase();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'uppercase':
|
|
|
|
|
if (['text', 'textarea', 'email', 'string'].includes(type)) {
|
|
|
|
|
schema = schema.strict().uppercase();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'positive':
|
|
|
|
|
if (
|
|
|
|
|
['number', 'integer', 'bigint', 'float', 'decimal'].includes(type)
|
|
|
|
|
) {
|
|
|
|
|
schema = schema.positive();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'negative':
|
|
|
|
|
if (
|
|
|
|
|
['number', 'integer', 'bigint', 'float', 'decimal'].includes(type)
|
|
|
|
|
) {
|
|
|
|
|
schema = schema.negative();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2019-07-18 18:14:29 +02:00
|
|
|
schema = schema.nullable();
|
2019-07-18 16:53:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return schema;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default createYupSchema;
|