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