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.
*/
validator(fields: ContentField[]): boolean {
return validateUniqueFields(fields, 'name');
return validateUniqueFields(fields, 'label');
},
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[];

View File

@ -20,10 +20,10 @@ export class UniqueFieldNamesConstraint
implements ValidatorConstraintInterface
{
validate(fields: ContentField[], _args: ValidationArguments) {
return validateUniqueFields(fields, 'name');
return validateUniqueFields(fields, 'label');
}
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',
label: 'Image',
label: 'Subtitle',
type: FieldType.file,
},
],

View File

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