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

@@ -0,0 +1,36 @@
import { z } from "zod";
const CarClassSchema = z
.string()
.trim()
.min(1, "Car class cannot be empty")
.max(50, "Car class must be 50 characters or less");
export class CarClass {
private readonly _value: string;
private constructor(value: string) {
this._value = value;
}
static create(value: string): CarClass {
const validated = CarClassSchema.parse(value);
return new CarClass(validated);
}
static fromString(value: string): CarClass {
return new CarClass(value);
}
get value(): string {
return this._value;
}
equals(other: CarClass): boolean {
return this._value === other._value;
}
toString(): string {
return this._value;
}
}

View File

@@ -0,0 +1,36 @@
import { z } from "zod";
const CarNameSchema = z
.string()
.trim()
.min(1, "Car name cannot be empty")
.max(100, "Car name must be 100 characters or less");
export class CarName {
private readonly _value: string;
private constructor(value: string) {
this._value = value;
}
static create(value: string): CarName {
const validated = CarNameSchema.parse(value);
return new CarName(validated);
}
static fromString(value: string): CarName {
return new CarName(value);
}
get value(): string {
return this._value;
}
equals(other: CarName): boolean {
return this._value === other._value;
}
toString(): string {
return this._value;
}
}

View File

@@ -0,0 +1,42 @@
import { IValueObject } from "../../../shared/domain/ValueObject";
export interface DriverNameProps {
value: string;
}
export class DriverName implements IValueObject<DriverNameProps> {
private static readonly MIN_LENGTH = 1;
private static readonly MAX_LENGTH = 50;
private static readonly VALID_CHARACTERS = /^[a-zA-Z0-9\s\-_]+$/;
private constructor(public readonly props: DriverNameProps) {}
static create(name: string): DriverName {
const trimmed = name.trim();
if (trimmed.length < this.MIN_LENGTH) {
throw new Error(`Driver name must be at least ${this.MIN_LENGTH} character long`);
}
if (trimmed.length > this.MAX_LENGTH) {
throw new Error(`Driver name must not exceed ${this.MAX_LENGTH} characters`);
}
if (!this.VALID_CHARACTERS.test(trimmed)) {
throw new Error("Driver name can only contain letters, numbers, spaces, hyphens, and underscores");
}
return new DriverName({ value: trimmed });
}
equals(other: IValueObject<DriverNameProps>): boolean {
if (!(other instanceof DriverName)) {
return false;
}
return this.props.value === other.props.value;
}
toString(): string {
return this.props.value;
}
}

View File

@@ -0,0 +1,38 @@
import type { IValueObject } from '@core/shared/domain';
export interface RaceNameProps {
value: string;
}
export class RaceName implements IValueObject<RaceNameProps> {
public readonly props: RaceNameProps;
private constructor(value: string) {
if (!value || !value.trim()) {
throw new Error('Race name cannot be empty');
}
if (value.trim().length < 3) {
throw new Error('Race name must be at least 3 characters long');
}
if (value.trim().length > 100) {
throw new Error('Race name must not exceed 100 characters');
}
this.props = { value: value.trim() };
}
public static fromString(value: string): RaceName {
return new RaceName(value);
}
get value(): string {
return this.props.value;
}
public toString(): string {
return this.props.value;
}
public equals(other: IValueObject<RaceNameProps>): boolean {
return this.props.value === other.props.value;
}
}

View File

@@ -0,0 +1,75 @@
/**
* Domain Value Object: StrengthOfField
*
* Represents the strength of field (SOF) rating for a race or league.
* Enforces valid range and provides domain-specific operations.
*/
import type { IValueObject } from '@core/shared/domain';
import { RacingDomainValidationError } from '../errors/RacingDomainError';
export interface StrengthOfFieldProps {
value: number;
}
export class StrengthOfField implements IValueObject<StrengthOfFieldProps> {
readonly value: number;
private constructor(value: number) {
this.value = value;
}
static create(value: number): StrengthOfField {
if (!Number.isInteger(value)) {
throw new RacingDomainValidationError('Strength of field must be an integer');
}
if (value < 0 || value > 100) {
throw new RacingDomainValidationError('Strength of field must be between 0 and 100');
}
return new StrengthOfField(value);
}
/**
* Get the strength category
*/
getCategory(): 'beginner' | 'intermediate' | 'advanced' | 'expert' {
if (this.value < 25) return 'beginner';
if (this.value < 50) return 'intermediate';
if (this.value < 75) return 'advanced';
return 'expert';
}
/**
* Check if this SOF is suitable for the given participant count
*/
isSuitableForParticipants(count: number): boolean {
// Higher SOF should generally have more participants
const minExpected = Math.floor(this.value / 10);
return count >= minExpected;
}
/**
* Calculate difference from another SOF
*/
differenceFrom(other: StrengthOfField): number {
return Math.abs(this.value - other.value);
}
get props(): StrengthOfFieldProps {
return { value: this.value };
}
toNumber(): number {
return this.value;
}
equals(other: IValueObject<StrengthOfFieldProps>): boolean {
return this.value === other.props.value;
}
toString(): string {
return this.value.toString();
}
}

View File

@@ -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;
}
}

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;
}
}