harden business rules

This commit is contained in:
2025-12-27 19:18:54 +01:00
parent 0e7a01d81c
commit 8d2b17d9a8
11 changed files with 343 additions and 55 deletions

View File

@@ -1,25 +1,36 @@
import { RacingDomainValidationError } from '../errors/RacingDomainError';
import type { IValueObject } from '@core/shared/domain';
import { z } from "zod";
export class TrackName implements IValueObject<string> {
private constructor(private readonly value: string) {}
const TrackNameSchema = z
.string()
.trim()
.min(1, "Track name cannot be empty")
.max(100, "Track name must be 100 characters or less");
get props(): string {
return this.value;
export class TrackName {
private readonly _value: string;
private constructor(value: string) {
this._value = value;
}
static create(value: string): TrackName {
if (!value || value.trim().length === 0) {
throw new RacingDomainValidationError('Track name is required');
}
return new TrackName(value.trim());
const validated = TrackNameSchema.parse(value);
return new TrackName(validated);
}
static fromString(value: string): TrackName {
return new TrackName(value);
}
get value(): string {
return this._value;
}
equals(other: TrackName): boolean {
return this._value === other._value;
}
toString(): string {
return this.value;
}
equals(other: IValueObject<string>): boolean {
return this.value === other.props;
return this._value;
}
}