import { Type } from 'class-transformer'; import { ArrayMaxSize, ArrayMinSize, registerDecorator, IsArray, IsDate, IsEnum, IsInt, IsOptional, IsString, MaxLength, Min, MinLength, ValidateNested, ValidationArguments, ValidationOptions, } from 'class-validator'; import { MATCH_GAME_MODES, POSITIONS } from '../../global/database/schema'; import type { MatchGameMode, Position } from '../../global/database/schema'; function IsMultipleOf( multiple: number, validationOptions?: ValidationOptions, ): PropertyDecorator { return (object, propertyName) => { registerDecorator({ name: 'isMultipleOf', target: object.constructor, propertyName: String(propertyName), constraints: [multiple], options: validationOptions, validator: { validate(value: unknown, args: ValidationArguments) { const [divisor] = args.constraints as [number]; return ( typeof value === 'number' && Number.isInteger(value) && value % divisor === 0 ); }, defaultMessage(args: ValidationArguments) { const [divisor] = args.constraints as [number]; return `${args.property} must be a multiple of ${divisor}.`; }, }, }); }; } export class PositionPreferenceDto { @IsEnum(POSITIONS) position!: Position; @IsInt() @Min(1) priority!: number; } class MatchScheduleItemDto { @IsString() @MaxLength(32) time!: string; @IsString() @MaxLength(128) label!: string; } export class CreateMatchDto { @IsString() @MinLength(1) @MaxLength(128) title!: string; @IsOptional() @IsString() @MaxLength(2048) description?: string; @IsOptional() @IsInt() mapId?: number; @IsDate() @Type(() => Date) scheduledAt!: Date; @IsOptional() @IsInt() @Min(10) @IsMultipleOf(10) maxParticipants?: number; @IsOptional() @IsInt() @Min(1) gameCount?: number; @IsOptional() @IsEnum(MATCH_GAME_MODES) gameMode?: MatchGameMode; @IsOptional() @IsArray() @IsString({ each: true }) @ArrayMaxSize(32) prizes?: string[]; @IsOptional() @IsArray() @IsString({ each: true }) @ArrayMaxSize(32) notices?: string[]; @IsOptional() @IsArray() @ValidateNested({ each: true }) @Type(() => MatchScheduleItemDto) @ArrayMaxSize(32) schedule?: MatchScheduleItemDto[]; } export class UpdateMatchDto { @IsOptional() @IsString() @MaxLength(128) title?: string; @IsOptional() @IsString() @MaxLength(2048) description?: string; @IsOptional() @IsInt() mapId?: number; @IsOptional() @IsDate() @Type(() => Date) scheduledAt?: Date; @IsOptional() @IsInt() @Min(10) @IsMultipleOf(10) maxParticipants?: number; @IsOptional() @IsInt() @Min(1) gameCount?: number; @IsOptional() @IsEnum(MATCH_GAME_MODES) gameMode?: MatchGameMode; @IsOptional() @IsArray() @IsString({ each: true }) prizes?: string[]; @IsOptional() @IsArray() @IsString({ each: true }) notices?: string[]; @IsOptional() @IsArray() @ValidateNested({ each: true }) @Type(() => MatchScheduleItemDto) schedule?: MatchScheduleItemDto[]; } export class JoinMatchDto { @IsArray() @ArrayMinSize(1) @ArrayMaxSize(5) @ValidateNested({ each: true }) @Type(() => PositionPreferenceDto) preferredPositions!: PositionPreferenceDto[]; @IsOptional() @IsString() userId?: string; }