Variable ValidatorsConst

Validators: {
    notEmpty(value: string, fieldName?: string): Result<string, DomainError>;
    length(
        value: string,
        options: { min?: number; max?: number; fieldName?: string },
    ): Result<string, DomainError>;
    range(
        value: number,
        options: { min?: number; max?: number; fieldName?: string },
    ): Result<number, DomainError>;
    pattern(
        value: string,
        regex: RegExp,
        errorMessage?: string,
    ): Result<string, DomainError>;
    email(value: string): Result<string, DomainError>;
    url(value: string): Result<string, DomainError>;
    compose<T>(
        ...validators: (value: T) => Result<T, DomainError>[],
    ): (value: T) => Result<T, DomainError>;
} = ...

Common validators for value objects and domain entities.

These validators are reusable, composable, and return Results for type-safe error handling.

Type declaration

  • notEmpty:function
    • Validates that a string is not empty.

      Parameters

      • value: string

        The string to validate

      • fieldName: string = 'Field'

        Name of the field for error messages

      Returns Result<string, DomainError>

      Success with trimmed string or Failure

      const result = Validators.notEmpty(name, 'Name');
      
  • length:function
    • Validates string length.

      Parameters

      • value: string

        The string to validate

      • options: { min?: number; max?: number; fieldName?: string }

        Validation options (min, max, fieldName)

      Returns Result<string, DomainError>

      Success with trimmed string or Failure

      const result = Validators.length(name, { min: 3, max: 50, fieldName: 'Name' });
      
  • range:function
    • Validates number range.

      Parameters

      • value: number

        The number to validate

      • options: { min?: number; max?: number; fieldName?: string }

        Validation options (min, max, fieldName)

      Returns Result<number, DomainError>

      Success with number or Failure

      const result = Validators.range(price, { min: 0, max: 10000, fieldName: 'Price' });
      
  • pattern:function
    • Validates against a regex pattern.

      Parameters

      • value: string

        The string to validate

      • regex: RegExp

        The regular expression pattern

      • errorMessage: string = 'Invalid format'

        Custom error message

      Returns Result<string, DomainError>

      Success with string or Failure

      const result = Validators.pattern(code, /^[A-Z]{3}$/, 'Code must be 3 uppercase letters');
      
  • email:function
  • url:function
  • compose:function
    • Composes multiple validators into a single validator. Validators are executed in order, stopping at first failure.

      Type Parameters

      • T

      Parameters

      Returns (value: T) => Result<T, DomainError>

      A composed validator function

      const emailValidator = Validators.compose(
      (v) => Validators.notEmpty(v, 'Email'),
      (v) => Validators.length(v, { max: 254 }),
      (v) => Validators.email(v)
      );

      const result = emailValidator('user@example.com');
// Compose validators for a ProductName value object
static create(value: string): Result<ProductName, DomainError> {
return Validators.compose(
(v) => Validators.notEmpty(v, 'Product name'),
(v) => Validators.length(v, { min: 3, max: 100 })
)(value).map(validated => new ProductName(validated));
}