mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
api: apply feedback
This commit is contained in:
parent
8490d4f253
commit
c71d750bdc
@ -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)',
|
||||
|
21
api/src/cms/utilities/field-validation.utils.ts
Normal file
21
api/src/cms/utilities/field-validation.utils.ts
Normal 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;
|
||||
});
|
||||
};
|
@ -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) {
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user