harden business rules
This commit is contained in:
36
core/racing/domain/value-objects/CarName.ts
Normal file
36
core/racing/domain/value-objects/CarName.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user