Factory helpers for creating Value Objects with less boilerplate.

Simplifies the creation of string and number-based Value Objects by handling validation and construction in a standardized way.

class ProductName extends ValueObject {
private constructor(readonly value: string) {
super();
}

static create(value: string): Result<ProductName, DomainError> {
return ValueObjectFactory.createString(
value,
ProductName,
[
(v) => Validators.notEmpty(v, 'Product name'),
(v) => Validators.length(v, { min: 3, max: 100 })
]
);
}

protected getEqualityComponents(): unknown[] {
return [this.value];
}
}

Constructors

Methods

  • Creates a string-based Value Object with validation.

    Runs all validators in sequence and creates the Value Object if all pass. Validators receive the raw value and should return Results.

    Type Parameters

    Parameters

    • value: string

      The raw string value

    • ValueObjectClass: new (value: string) => T

      The Value Object class constructor

    • validators: (v: string) => Result<string, DomainError>[] = []

      Array of validation functions

    Returns Result<T, DomainError>

    Result with the created Value Object or validation error

    class Email extends ValueObject {
    private constructor(readonly value: string) { super(); }

    static create(value: string): Result<Email, DomainError> {
    return ValueObjectFactory.createString(value, Email, [
    (v) => Validators.notEmpty(v, 'Email'),
    (v) => Validators.email(v)
    ]);
    }

    protected getEqualityComponents() { return [this.value]; }
    }
  • Creates a number-based Value Object with validation.

    Runs all validators in sequence and creates the Value Object if all pass. Validators receive the raw number and should return Results.

    Type Parameters

    Parameters

    • value: number

      The raw number value

    • ValueObjectClass: new (value: number) => T

      The Value Object class constructor

    • validators: (v: number) => Result<number, DomainError>[] = []

      Array of validation functions

    Returns Result<T, DomainError>

    Result with the created Value Object or validation error

    class Price extends ValueObject {
    private constructor(readonly value: number) { super(); }

    static create(value: number): Result<Price, DomainError> {
    return ValueObjectFactory.createNumber(value, Price, [
    (v) => Validators.range(v, { min: 0, max: 1000000, fieldName: 'Price' })
    ]);
    }

    protected getEqualityComponents() { return [this.value]; }
    }
  • Creates a Value Object from a primitive with custom validation logic.

    More flexible than createString/createNumber when you need custom validation that doesn't fit the standard validator pattern.

    Type Parameters

    Parameters

    Returns Result<T, DomainError>

    Result with the created Value Object or validation error

    class Color extends ValueObject {
    private constructor(readonly hex: string) { super(); }

    static create(hex: string): Result<Color, DomainError> {
    return ValueObjectFactory.create(hex, Color, (value) => {
    if (!/^#[0-9A-F]{6}$/i.test(value)) {
    return Failure.create(
    new DomainError('INVALID_COLOR', 'Must be valid hex color')
    );
    }
    return Success.create(value);
    });
    }

    protected getEqualityComponents() { return [this.hex]; }
    }