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

43 lines
1.1 KiB
TypeScript

import type { ValueObject } from '@core/shared/domain/ValueObject';
import { IdentityDomainValidationError } from '../errors/IdentityDomainError';
export interface GameKeyProps {
value: string;
}
export class GameKey implements ValueObject<GameKeyProps> {
readonly value: string;
private constructor(value: string) {
this.value = value;
}
static create(value: string): GameKey {
if (!value || value.trim().length === 0) {
throw new IdentityDomainValidationError('GameKey cannot be empty');
}
const trimmed = value.trim();
// Game keys should be lowercase alphanumeric with optional underscores/hyphens
const gameKeyRegex = /^[a-z0-9][a-z0-9_-]*$/;
if (!gameKeyRegex.test(trimmed)) {
throw new IdentityDomainValidationError(
`Invalid game key: ${value}. Must be lowercase alphanumeric with optional underscores/hyphens`
);
}
return new GameKey(trimmed);
}
get props(): GameKeyProps {
return { value: this.value };
}
equals(other: ValueObject<GameKeyProps>): boolean {
return this.value === other.props.value;
}
toString(): string {
return this.value;
}
}