48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import type { ValueObject } from '@core/shared/domain/ValueObject';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { IdentityDomainValidationError } from '../errors/IdentityDomainError';
|
|
|
|
export interface RatingEventIdProps {
|
|
value: string;
|
|
}
|
|
|
|
export class RatingEventId implements ValueObject<RatingEventIdProps> {
|
|
readonly value: string;
|
|
|
|
private constructor(value: string) {
|
|
this.value = value;
|
|
}
|
|
|
|
static create(value: string): RatingEventId {
|
|
if (!value || value.trim().length === 0) {
|
|
throw new IdentityDomainValidationError('RatingEventId cannot be empty');
|
|
}
|
|
|
|
const trimmed = value.trim();
|
|
// Basic UUID v4 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(trimmed)) {
|
|
throw new IdentityDomainValidationError(
|
|
`Invalid UUID format: ${value}`
|
|
);
|
|
}
|
|
|
|
return new RatingEventId(trimmed);
|
|
}
|
|
|
|
static generate(): RatingEventId {
|
|
return new RatingEventId(uuidv4());
|
|
}
|
|
|
|
get props(): RatingEventIdProps {
|
|
return { value: this.value };
|
|
}
|
|
|
|
equals(other: ValueObject<RatingEventIdProps>): boolean {
|
|
return this.value === other.props.value;
|
|
}
|
|
|
|
toString(): string {
|
|
return this.value;
|
|
}
|
|
} |