import { Transform } from 'class-transformer'; import { ArrayUnique, IsArray, IsBoolean, IsEnum, IsInt, IsOptional, IsString, Matches, Max, MaxLength, Min, MinLength, ValidateIf, } from 'class-validator'; import { AssetAttributeDataType } from '../../database/entities'; export class CreateAttributeDefinitionDto { @Transform(({ value }) => typeof value === 'string' ? value.trim().toLowerCase() : value, ) @IsString() @MinLength(2) @MaxLength(80) @Matches(/^[a-z][a-z0-9_-]+$/) code!: string; @Transform(({ value }) => (typeof value === 'string' ? value.trim() : value)) @IsString() @MinLength(1) @MaxLength(160) name!: string; @IsEnum(AssetAttributeDataType) dataType!: AssetAttributeDataType; @IsBoolean() isRequired!: boolean; @IsOptional() @Transform(({ value }) => typeof value === 'string' && value.trim() ? value.trim() : null, ) @IsString() @MaxLength(40) unit?: string | null; @ValidateIf((dto: CreateAttributeDefinitionDto) => dto.dataType === AssetAttributeDataType.SELECT, ) @IsArray() @ArrayUnique() @IsString({ each: true }) @MinLength(1, { each: true }) @MaxLength(160, { each: true }) options?: string[]; @IsInt() @Min(0) @Max(10_000) sortOrder!: number; }