api: apply feedback

This commit is contained in:
yassinedorbozgithub 2025-06-10 10:29:22 +01:00
parent 8490d4f253
commit c71d750bdc
4 changed files with 26 additions and 15 deletions

View File

@ -13,6 +13,7 @@ import { BaseSchema } from '@/utils/generics/base-schema';
import { LifecycleHookManager } from '@/utils/generics/lifecycle-hook-manager';
import { ContentField } from '../dto/contentType.dto';
import { validateUniqueFields } from '../utilities/field-validation.utils';
@Schema({ timestamps: true })
export class ContentType extends BaseSchema {
@ -48,13 +49,7 @@ export class ContentType extends BaseSchema {
* when `runValidators: true` is set.
*/
validator(fields: ContentField[]): boolean {
if (!Array.isArray(fields)) return false;
const seen = new Set<string>();
return fields.every((f) => {
if (seen.has(f.name)) return false;
seen.add(f.name);
return true;
});
return validateUniqueFields(fields, 'name');
},
message:
'Each element in "fields" must have a unique "name" (duplicate detected)',

View File

@ -0,0 +1,21 @@
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
export const validateUniqueFields = <T>(
fields: T[],
fieldName: keyof T,
): boolean => {
if (!Array.isArray(fields)) return false;
const seen = new Set<string>();
return fields.every((f) => {
const fieldValue = f[fieldName] as string;
if (seen.has(fieldValue)) return false;
seen.add(fieldValue);
return true;
});
};

View File

@ -13,19 +13,14 @@ import {
} from 'class-validator';
import { ContentField } from '../dto/contentType.dto';
import { validateUniqueFields } from '../utilities/field-validation.utils';
@ValidatorConstraint({ async: false })
export class UniqueFieldNamesConstraint
implements ValidatorConstraintInterface
{
validate(fields: ContentField[], _args: ValidationArguments) {
if (!Array.isArray(fields)) return false;
const seen = new Set<string>();
return fields.every((f) => {
if (seen.has(f.name)) return false;
seen.add(f.name);
return true;
});
return validateUniqueFields(fields, 'name');
}
defaultMessage(args: ValidationArguments) {

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2025 Hexastack. All rights reserved.
* Copyright © 2024 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.