mirror of
https://github.com/strapi/strapi.git
synced 2025-10-18 11:32:42 +00:00
Add validations for biginteger
This commit is contained in:
parent
a54d207340
commit
aafe92653b
@ -14,7 +14,6 @@ const getInputType = (type = '') => {
|
|||||||
switch (toLower(type)) {
|
switch (toLower(type)) {
|
||||||
case 'boolean':
|
case 'boolean':
|
||||||
return 'bool';
|
return 'bool';
|
||||||
case 'biginteger':
|
|
||||||
case 'decimal':
|
case 'decimal':
|
||||||
case 'float':
|
case 'float':
|
||||||
case 'integer':
|
case 'integer':
|
||||||
|
@ -196,6 +196,10 @@ const EditViewDataManagerProvider = ({
|
|||||||
inputValue = null;
|
inputValue = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type === 'biginteger' && value === '') {
|
||||||
|
inputValue = null;
|
||||||
|
}
|
||||||
|
|
||||||
dispatch({
|
dispatch({
|
||||||
type: 'ON_CHANGE',
|
type: 'ON_CHANGE',
|
||||||
keys: name.split('.'),
|
keys: name.split('.'),
|
||||||
|
@ -7,6 +7,7 @@ import {
|
|||||||
isArray,
|
isArray,
|
||||||
isEmpty,
|
isEmpty,
|
||||||
isNaN,
|
isNaN,
|
||||||
|
toNumber,
|
||||||
} from 'lodash';
|
} from 'lodash';
|
||||||
import * as yup from 'yup';
|
import * as yup from 'yup';
|
||||||
import { translatedErrors as errorsTrads } from 'strapi-helper-plugin';
|
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 getAttributes = data => get(data, ['schema', 'attributes'], {});
|
||||||
|
|
||||||
const createYupSchema = (model, { components }) => {
|
const createYupSchema = (model, { components }) => {
|
||||||
@ -146,6 +175,7 @@ const createYupSchema = (model, { components }) => {
|
|||||||
|
|
||||||
const createYupSchemaAttribute = (type, validations) => {
|
const createYupSchemaAttribute = (type, validations) => {
|
||||||
let schema = yup.mixed();
|
let schema = yup.mixed();
|
||||||
|
|
||||||
if (
|
if (
|
||||||
['string', 'text', 'richtext', 'email', 'password', 'enumeration'].includes(
|
['string', 'text', 'richtext', 'email', 'password', 'enumeration'].includes(
|
||||||
type
|
type
|
||||||
@ -153,6 +183,7 @@ const createYupSchemaAttribute = (type, validations) => {
|
|||||||
) {
|
) {
|
||||||
schema = yup.string();
|
schema = yup.string();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'json') {
|
if (type === 'json') {
|
||||||
schema = yup
|
schema = yup
|
||||||
.mixed(errorsTrads.json)
|
.mixed(errorsTrads.json)
|
||||||
@ -183,16 +214,22 @@ const createYupSchemaAttribute = (type, validations) => {
|
|||||||
if (type === 'email') {
|
if (type === 'email') {
|
||||||
schema = schema.email(errorsTrads.email);
|
schema = schema.email(errorsTrads.email);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (['number', 'integer', 'biginteger', 'float', 'decimal'].includes(type)) {
|
if (['number', 'integer', 'biginteger', 'float', 'decimal'].includes(type)) {
|
||||||
schema = yup
|
schema = yup
|
||||||
.number()
|
.number()
|
||||||
.transform(cv => (isNaN(cv) ? undefined : cv))
|
.transform(cv => (isNaN(cv) ? undefined : cv))
|
||||||
.typeError();
|
.typeError();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (['date', 'datetime'].includes(type)) {
|
if (['date', 'datetime'].includes(type)) {
|
||||||
schema = yup.date();
|
schema = yup.date();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (type === 'biginteger') {
|
||||||
|
schema = yup.string().matches(/^\d*$/);
|
||||||
|
}
|
||||||
|
|
||||||
Object.keys(validations).forEach(validation => {
|
Object.keys(validations).forEach(validation => {
|
||||||
const validationValue = validations[validation];
|
const validationValue = validations[validation];
|
||||||
if (
|
if (
|
||||||
@ -205,15 +242,25 @@ const createYupSchemaAttribute = (type, validations) => {
|
|||||||
case 'required':
|
case 'required':
|
||||||
schema = schema.required(errorsTrads.required);
|
schema = schema.required(errorsTrads.required);
|
||||||
break;
|
break;
|
||||||
case 'max':
|
case 'max': {
|
||||||
schema = schema.max(validationValue, errorsTrads.max);
|
if (type === 'biginteger') {
|
||||||
|
schema = schema.isInferior(errorsTrads.max, validationValue);
|
||||||
|
} else {
|
||||||
|
schema = schema.max(validationValue, errorsTrads.max);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case 'maxLength':
|
case 'maxLength':
|
||||||
schema = schema.max(validationValue, errorsTrads.maxLength);
|
schema = schema.max(validationValue, errorsTrads.maxLength);
|
||||||
break;
|
break;
|
||||||
case 'min':
|
case 'min': {
|
||||||
schema = schema.min(validationValue, errorsTrads.min);
|
if (type === 'biginteger') {
|
||||||
|
schema = schema.isSuperior(errorsTrads.min, validationValue);
|
||||||
|
} else {
|
||||||
|
schema = schema.min(validationValue, errorsTrads.min);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case 'minLength':
|
case 'minLength':
|
||||||
schema = schema.min(validationValue, errorsTrads.minLength);
|
schema = schema.min(validationValue, errorsTrads.minLength);
|
||||||
break;
|
break;
|
||||||
@ -249,6 +296,7 @@ const createYupSchemaAttribute = (type, validations) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return schema;
|
return schema;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user