Add validations for biginteger

This commit is contained in:
soupette 2019-12-18 17:35:05 +01:00
parent a54d207340
commit aafe92653b
3 changed files with 56 additions and 5 deletions

View File

@ -14,7 +14,6 @@ const getInputType = (type = '') => {
switch (toLower(type)) {
case 'boolean':
return 'bool';
case 'biginteger':
case 'decimal':
case 'float':
case 'integer':

View File

@ -196,6 +196,10 @@ const EditViewDataManagerProvider = ({
inputValue = null;
}
if (type === 'biginteger' && value === '') {
inputValue = null;
}
dispatch({
type: 'ON_CHANGE',
keys: name.split('.'),

View File

@ -7,6 +7,7 @@ import {
isArray,
isEmpty,
isNaN,
toNumber,
} from 'lodash';
import * as yup from 'yup';
import { translatedErrors as errorsTrads } from 'strapi-helper-plugin';
@ -28,6 +29,34 @@ yup.addMethod(yup.array, 'notEmptyMin', function(min) {
});
});
yup.addMethod(yup.string, 'isInferior', function(message, max) {
return this.test('isInferior', message, function(value) {
if (!value) {
return true;
}
if (Number.isNaN(toNumber(value))) {
return true;
}
return toNumber(max) >= toNumber(value);
});
});
yup.addMethod(yup.string, 'isSuperior', function(message, min) {
return this.test('isSuperior', message, function(value) {
if (!value) {
return true;
}
if (Number.isNaN(toNumber(value))) {
return true;
}
return toNumber(value) >= toNumber(min);
});
});
const getAttributes = data => get(data, ['schema', 'attributes'], {});
const createYupSchema = (model, { components }) => {
@ -146,6 +175,7 @@ const createYupSchema = (model, { components }) => {
const createYupSchemaAttribute = (type, validations) => {
let schema = yup.mixed();
if (
['string', 'text', 'richtext', 'email', 'password', 'enumeration'].includes(
type
@ -153,6 +183,7 @@ const createYupSchemaAttribute = (type, validations) => {
) {
schema = yup.string();
}
if (type === 'json') {
schema = yup
.mixed(errorsTrads.json)
@ -183,16 +214,22 @@ const createYupSchemaAttribute = (type, validations) => {
if (type === 'email') {
schema = schema.email(errorsTrads.email);
}
if (['number', 'integer', 'biginteger', 'float', 'decimal'].includes(type)) {
schema = yup
.number()
.transform(cv => (isNaN(cv) ? undefined : cv))
.typeError();
}
if (['date', 'datetime'].includes(type)) {
schema = yup.date();
}
if (type === 'biginteger') {
schema = yup.string().matches(/^\d*$/);
}
Object.keys(validations).forEach(validation => {
const validationValue = validations[validation];
if (
@ -205,15 +242,25 @@ const createYupSchemaAttribute = (type, validations) => {
case 'required':
schema = schema.required(errorsTrads.required);
break;
case 'max':
schema = schema.max(validationValue, errorsTrads.max);
case 'max': {
if (type === 'biginteger') {
schema = schema.isInferior(errorsTrads.max, validationValue);
} else {
schema = schema.max(validationValue, errorsTrads.max);
}
break;
}
case 'maxLength':
schema = schema.max(validationValue, errorsTrads.maxLength);
break;
case 'min':
schema = schema.min(validationValue, errorsTrads.min);
case 'min': {
if (type === 'biginteger') {
schema = schema.isSuperior(errorsTrads.min, validationValue);
} else {
schema = schema.min(validationValue, errorsTrads.min);
}
break;
}
case 'minLength':
schema = schema.min(validationValue, errorsTrads.minLength);
break;
@ -249,6 +296,7 @@ const createYupSchemaAttribute = (type, validations) => {
}
}
});
return schema;
};