fix: apply feedback

This commit is contained in:
yassinedorbozgithub 2025-06-10 18:14:22 +01:00
parent c71d750bdc
commit f4ca6613c7
4 changed files with 9 additions and 15 deletions

View File

@ -49,10 +49,10 @@ export class ContentType extends BaseSchema {
* when `runValidators: true` is set. * when `runValidators: true` is set.
*/ */
validator(fields: ContentField[]): boolean { validator(fields: ContentField[]): boolean {
return validateUniqueFields(fields, 'name'); return validateUniqueFields(fields, 'label');
}, },
message: message:
'Each element in "fields" must have a unique "name" (duplicate detected)', 'Each element in "fields" must have a unique "label" (duplicate detected)',
}, },
}) })
fields: ContentField[]; fields: ContentField[];

View File

@ -20,10 +20,10 @@ export class UniqueFieldNamesConstraint
implements ValidatorConstraintInterface implements ValidatorConstraintInterface
{ {
validate(fields: ContentField[], _args: ValidationArguments) { validate(fields: ContentField[], _args: ValidationArguments) {
return validateUniqueFields(fields, 'name'); return validateUniqueFields(fields, 'label');
} }
defaultMessage(args: ValidationArguments) { defaultMessage(args: ValidationArguments) {
return `${args.property} contains duplicate "name" values; each field.name must be unique`; return `${args.property} contains duplicate "label" values; each field.name must be unique`;
} }
} }

View File

@ -64,7 +64,7 @@ const contentTypes: TContentTypeFixtures['values'][] = [
}, },
{ {
name: 'subtitle', name: 'subtitle',
label: 'Image', label: 'Subtitle',
type: FieldType.file, type: FieldType.file,
}, },
], ],

View File

@ -52,16 +52,10 @@ export const ContentTypeForm: FC<ComponentFormProps<IContentType>> = ({
const { append, fields, remove } = useFieldArray({ const { append, fields, remove } = useFieldArray({
name: "fields", name: "fields",
rules: { rules: {
validate: (value) => { validate: (fields) => {
const labelCounts = value.reduce((acc, field) => { const hasDuplicatedLabels =
if (!field.label.trim()) return acc; new Set(fields.map((f) => f["label"] as string)).size ===
acc[field.label] = (acc[field.label] || 0) + 1; fields.length;
return acc;
}, {} as Record<string, number>);
const hasDuplicatedLabels = Object.values(labelCounts).some(
(count: number) => count > 1,
);
if (hasDuplicatedLabels) { if (hasDuplicatedLabels) {
toast.error(t("message.duplicate_labels_not_allowed")); toast.error(t("message.duplicate_labels_not_allowed"));