rating
This commit is contained in:
43
core/identity/domain/value-objects/GameKey.ts
Normal file
43
core/identity/domain/value-objects/GameKey.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import type { IValueObject } from '@core/shared/domain';
|
||||
import { IdentityDomainValidationError } from '../errors/IdentityDomainError';
|
||||
|
||||
export interface GameKeyProps {
|
||||
value: string;
|
||||
}
|
||||
|
||||
export class GameKey implements IValueObject<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: IValueObject<GameKeyProps>): boolean {
|
||||
return this.value === other.props.value;
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user