From 59be13de07143e546284c75d00ea98c9159aa573 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 5 Feb 2026 13:45:40 +0000 Subject: Enhance checkbox-list custom field functionality - Added CheckboxListEnumInput component for handling enumeration inputs. - Updated CheckboxListDefaultInput to support new options structure. - Integrated validation for checkbox list options using Yup. - Modified package.json and package-lock.json to include new dependencies. - Improved admin interface with enhanced input handling and validation feedback. --- admin/src/utils/checkboxListValidator.ts | 74 ++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 admin/src/utils/checkboxListValidator.ts (limited to 'admin/src/utils/checkboxListValidator.ts') diff --git a/admin/src/utils/checkboxListValidator.ts b/admin/src/utils/checkboxListValidator.ts new file mode 100644 index 0000000..771531f --- /dev/null +++ b/admin/src/utils/checkboxListValidator.ts @@ -0,0 +1,74 @@ +import slugify from '@sindresorhus/slugify'; +import { translatedErrors } from '@strapi/admin/strapi-admin'; +import * as yup from 'yup'; + +const GRAPHQL_ENUM_REGEX = /^[_A-Za-z][_0-9A-Za-z]*$/; + +const toRegressedEnumValue = (value?: string) => { + if (!value) { + return ''; + } + + return slugify(value, { + decamelize: false, + lowercase: false, + separator: '_', + }); +}; + +const hasUniqueValues = (values: string[]) => { + const seen = new Set(); + + for (const value of values) { + if (seen.has(value)) { + return false; + } + + seen.add(value); + } + + return true; +}; + +export const checkboxListOptionsValidator = () => ({ + enum: yup + .array() + .of(yup.string()) + .min(1, translatedErrors.min.id) + .test({ + name: 'areEnumValuesUnique', + message: 'content-type-builder.error.validation.enum-duplicate', + test(values) { + if (!values) { + return false; + } + + const normalizedValues = values.map(toRegressedEnumValue); + + return hasUniqueValues(normalizedValues); + }, + }) + .test({ + name: 'doesNotHaveEmptyValues', + message: 'content-type-builder.error.validation.enum-empty-string', + test(values) { + if (!values) { + return false; + } + + return !values.map(toRegressedEnumValue).some((value) => value === ''); + }, + }) + .test({ + name: 'doesMatchRegex', + message: 'content-type-builder.error.validation.enum-regex', + test(values) { + if (!values) { + return false; + } + + return values.map(toRegressedEnumValue).every((value) => GRAPHQL_ENUM_REGEX.test(value)); + }, + }), + enumName: yup.string().nullable(), +}); -- cgit v1.2.3