Files
gridpilot.gg/core/racing/domain/value-objects/DriverName.ts
2026-01-16 16:46:57 +01:00

42 lines
1.2 KiB
TypeScript

import { ValueObject } from "../../../shared/domain/ValueObject";
export interface DriverNameProps {
value: string;
}
export class DriverName implements ValueObject<DriverNameProps> {
private static readonly MIN_LENGTH = 1;
private static readonly MAX_LENGTH = 50;
private static readonly VALID_CHARACTERS = /^[a-zA-Z0-9\s\-_]+$/;
private constructor(public readonly props: DriverNameProps) {}
static create(name: string): DriverName {
const trimmed = name.trim();
if (trimmed.length < this.MIN_LENGTH) {
throw new Error(`Driver name must be at least ${this.MIN_LENGTH} character long`);
}
if (trimmed.length > this.MAX_LENGTH) {
throw new Error(`Driver name must not exceed ${this.MAX_LENGTH} characters`);
}
if (!this.VALID_CHARACTERS.test(trimmed)) {
throw new Error("Driver name can only contain letters, numbers, spaces, hyphens, and underscores");
}
return new DriverName({ value: trimmed });
}
equals(other: ValueObject<DriverNameProps>): boolean {
if (!(other instanceof DriverName)) {
return false;
}
return this.props.value === other.props.value;
}
toString(): string {
return this.props.value;
}
}