CTB: Improve UI validation for enum values

Co-authored-by: omacovei <omacovei@users.noreply.github.com>
This commit is contained in:
Gustav Hansen 2022-07-07 13:43:05 +02:00
parent 92558c8238
commit b582e770b7
2 changed files with 4 additions and 5 deletions

View File

@ -4,7 +4,6 @@ import { translatedErrors as errorsTrads } from '@strapi/helper-plugin';
import getTrad from '../../../utils/getTrad'; import getTrad from '../../../utils/getTrad';
import getRelationType from '../../../utils/getRelationType'; import getRelationType from '../../../utils/getRelationType';
import toRegressedEnumValue from '../../../utils/toRegressedEnumValue'; import toRegressedEnumValue from '../../../utils/toRegressedEnumValue';
import startsWithANumber from '../../../utils/startsWithANumber';
import { import {
alreadyUsedAttributeNames, alreadyUsedAttributeNames,
createTextShape, createTextShape,
@ -169,9 +168,9 @@ const types = {
test: values => !values.some(val => val === ''), test: values => !values.some(val => val === ''),
}) })
.test({ .test({
name: 'doesNotStartWithANumber', name: 'doesMatchRegex',
message: getTrad('error.validation.enum-number'), message: getTrad('error.validation.enum-regex'),
test: values => !values.some(startsWithANumber), test: values => values.map(toRegressedEnumValue).every(value => ENUM_REGEX.test(value)),
}), }),
enumName: yup.string().nullable(), enumName: yup.string().nullable(),
}; };

View File

@ -60,7 +60,7 @@
"error.contentTypeName.reserved-name": "This name cannot be used in your project as it might break other functionalities", "error.contentTypeName.reserved-name": "This name cannot be used in your project as it might break other functionalities",
"error.validation.enum-duplicate": "Duplicate values are not allowed", "error.validation.enum-duplicate": "Duplicate values are not allowed",
"error.validation.enum-empty-string": "Empty strings are not allowed", "error.validation.enum-empty-string": "Empty strings are not allowed",
"error.validation.enum-number": "Values cannot start with a number", "error.validation.enum-regex": "At least one value contains invalid characters. Non alphanumerical characters should not be followed by numbers at the beginning of a value.",
"error.validation.minSupMax": "Can't be superior", "error.validation.minSupMax": "Can't be superior",
"error.validation.positive": "Must be a positive number", "error.validation.positive": "Must be a positive number",
"error.validation.regex": "Regex pattern is invalid", "error.validation.regex": "Regex pattern is invalid",