A social knowledge tool for researchers built on ATProto
at development 1.2 kB view raw
1import { ValueObject } from '../../../../shared/domain/ValueObject'; 2import { Result, ok, err } from '../../../../shared/core/Result'; 3 4export class InvalidCollectionNameError extends Error { 5 constructor(message: string) { 6 super(message); 7 this.name = 'InvalidCollectionNameError'; 8 } 9} 10 11interface CollectionNameProps { 12 value: string; 13} 14 15export class CollectionName extends ValueObject<CollectionNameProps> { 16 public static readonly MAX_LENGTH = 100; 17 18 get value(): string { 19 return this.props.value; 20 } 21 22 private constructor(props: CollectionNameProps) { 23 super(props); 24 } 25 26 public static create( 27 name: string, 28 ): Result<CollectionName, InvalidCollectionNameError> { 29 if (!name || name.trim().length === 0) { 30 return err( 31 new InvalidCollectionNameError('Collection name cannot be empty'), 32 ); 33 } 34 35 const trimmedName = name.trim(); 36 37 if (trimmedName.length > this.MAX_LENGTH) { 38 return err( 39 new InvalidCollectionNameError( 40 `Collection name cannot exceed ${this.MAX_LENGTH} characters`, 41 ), 42 ); 43 } 44 45 return ok(new CollectionName({ value: trimmedName })); 46 } 47 48 public toString(): string { 49 return this.value; 50 } 51}