harden business rules
This commit is contained in:
@@ -1,25 +1,32 @@
|
||||
import { RacingDomainValidationError } from '../errors/RacingDomainError';
|
||||
import type { IValueObject } from '@core/shared/domain';
|
||||
import { z } from "zod";
|
||||
|
||||
export class TrackId implements IValueObject<string> {
|
||||
private constructor(private readonly value: string) {}
|
||||
const TrackIdSchema = z.string().uuid("TrackId must be a valid UUID");
|
||||
|
||||
get props(): string {
|
||||
return this.value;
|
||||
export class TrackId {
|
||||
private readonly _value: string;
|
||||
|
||||
private constructor(value: string) {
|
||||
this._value = value;
|
||||
}
|
||||
|
||||
static create(value: string): TrackId {
|
||||
if (!value || value.trim().length === 0) {
|
||||
throw new RacingDomainValidationError('Track ID cannot be empty');
|
||||
}
|
||||
return new TrackId(value.trim());
|
||||
const validated = TrackIdSchema.parse(value);
|
||||
return new TrackId(validated);
|
||||
}
|
||||
|
||||
static fromString(value: string): TrackId {
|
||||
return new TrackId(value);
|
||||
}
|
||||
|
||||
get value(): string {
|
||||
return this._value;
|
||||
}
|
||||
|
||||
equals(other: TrackId): boolean {
|
||||
return this._value === other._value;
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
equals(other: IValueObject<string>): boolean {
|
||||
return this.value === other.props;
|
||||
return this._value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user