rename to core
This commit is contained in:
100
core/racing/domain/value-objects/LeagueDescription.ts
Normal file
100
core/racing/domain/value-objects/LeagueDescription.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Domain Value Object: LeagueDescription
|
||||
*
|
||||
* Represents a valid league description with validation rules.
|
||||
*/
|
||||
|
||||
import { RacingDomainValidationError } from '../errors/RacingDomainError';
|
||||
import type { IValueObject } from '@gridpilot/shared/domain';
|
||||
|
||||
export interface LeagueDescriptionValidationResult {
|
||||
valid: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export const LEAGUE_DESCRIPTION_CONSTRAINTS = {
|
||||
minLength: 20,
|
||||
maxLength: 1000,
|
||||
recommendedMinLength: 50,
|
||||
} as const;
|
||||
|
||||
export interface LeagueDescriptionProps {
|
||||
value: string;
|
||||
}
|
||||
|
||||
export class LeagueDescription implements IValueObject<LeagueDescriptionProps> {
|
||||
readonly value: string;
|
||||
|
||||
private constructor(value: string) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a league description without creating the value object
|
||||
*/
|
||||
static validate(value: string): LeagueDescriptionValidationResult {
|
||||
const trimmed = value?.trim() ?? '';
|
||||
|
||||
if (!trimmed) {
|
||||
return { valid: false, error: 'Description is required — help drivers understand your league' };
|
||||
}
|
||||
|
||||
if (trimmed.length < LEAGUE_DESCRIPTION_CONSTRAINTS.minLength) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `Description must be at least ${LEAGUE_DESCRIPTION_CONSTRAINTS.minLength} characters — tell drivers what makes your league special`,
|
||||
};
|
||||
}
|
||||
|
||||
if (trimmed.length > LEAGUE_DESCRIPTION_CONSTRAINTS.maxLength) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `Description must be ${LEAGUE_DESCRIPTION_CONSTRAINTS.maxLength} characters or less`,
|
||||
};
|
||||
}
|
||||
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if description meets recommended length for better engagement
|
||||
*/
|
||||
static isRecommendedLength(value: string): boolean {
|
||||
const trimmed = value?.trim() ?? '';
|
||||
return trimmed.length >= LEAGUE_DESCRIPTION_CONSTRAINTS.recommendedMinLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a LeagueDescription from a string value
|
||||
*/
|
||||
static create(value: string): LeagueDescription {
|
||||
const validation = this.validate(value);
|
||||
if (!validation.valid) {
|
||||
throw new RacingDomainValidationError(validation.error ?? 'Invalid league description');
|
||||
}
|
||||
return new LeagueDescription(value.trim());
|
||||
}
|
||||
|
||||
get props(): LeagueDescriptionProps {
|
||||
return { value: this.value };
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to create a LeagueDescription, returning null if invalid
|
||||
*/
|
||||
static tryCreate(value: string): LeagueDescription | null {
|
||||
const validation = this.validate(value);
|
||||
if (!validation.valid) {
|
||||
return null;
|
||||
}
|
||||
return new LeagueDescription(value.trim());
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
equals(other: IValueObject<LeagueDescriptionProps>): boolean {
|
||||
return this.props.value === other.props.value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user