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

62 lines
1.7 KiB
TypeScript

import type { ValueObject } from '@core/shared/domain/ValueObject';
import { RacingDomainValidationError } from '../errors/RacingDomainError';
// Simple UUID v4 generator
function uuidv4(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
export interface TeamRatingEventIdProps {
value: string;
}
export class TeamRatingEventId implements ValueObject<TeamRatingEventIdProps> {
readonly value: string;
private constructor(value: string) {
this.value = value;
}
static create(value: string): TeamRatingEventId {
if (!value || value.trim().length === 0) {
throw new RacingDomainValidationError('TeamRatingEventId cannot be empty');
}
// Strict validation: no leading/trailing whitespace allowed
if (value !== value.trim()) {
throw new RacingDomainValidationError(
`TeamRatingEventId cannot have leading or trailing whitespace: "${value}"`
);
}
// Basic UUID format validation
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
if (!uuidRegex.test(value)) {
throw new RacingDomainValidationError(
`TeamRatingEventId must be a valid UUID format, got: "${value}"`
);
}
return new TeamRatingEventId(value);
}
static generate(): TeamRatingEventId {
return new TeamRatingEventId(uuidv4());
}
get props(): TeamRatingEventIdProps {
return { value: this.value };
}
equals(other: ValueObject<TeamRatingEventIdProps>): boolean {
return this.value === other.props.value;
}
toString(): string {
return this.value;
}
}