forked from mtusi2026/mtusi2026lab6
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import {
|
|
ArrayMaxSize,
|
|
IsArray,
|
|
IsString,
|
|
Validate,
|
|
ValidateIf,
|
|
ValidatorConstraint,
|
|
ValidatorConstraintInterface,
|
|
} from 'class-validator';
|
|
import { CodePointLength } from '../../common/dto/code-point-length.validator';
|
|
import { validate as isUuid } from '../../common/uuid';
|
|
|
|
@ValidatorConstraint({ name: 'uniqueUuidList' })
|
|
class UniqueUuidList implements ValidatorConstraintInterface {
|
|
validate(value: unknown): boolean {
|
|
if (
|
|
!Array.isArray(value) ||
|
|
!value.every((id) => typeof id === 'string' && isUuid(id))
|
|
) {
|
|
return false;
|
|
}
|
|
const normalized = (value as string[]).map((id) => id.toLowerCase());
|
|
return new Set(normalized).size === normalized.length;
|
|
}
|
|
|
|
defaultMessage(): string {
|
|
return 'attachment_ids must contain unique UUIDs';
|
|
}
|
|
}
|
|
|
|
export class CreateMessageDto {
|
|
@IsString()
|
|
@CodePointLength(0, 4000)
|
|
text!: string;
|
|
|
|
@ValidateIf((dto: CreateMessageDto) => dto.attachment_ids !== undefined)
|
|
@IsArray()
|
|
@ArrayMaxSize(10)
|
|
@Validate(UniqueUuidList)
|
|
attachment_ids?: string[];
|
|
}
|