Class Specification<T>Abstract

Specification Pattern for encapsulating business rules and validation logic.

Specifications allow you to compose complex business rules using logical operators (AND, OR, NOT) while keeping each rule independently testable and reusable.

class CustomerIsActiveSpec extends Specification<Customer> {
isSatisfiedBy(customer: Customer): boolean {
return customer.status === 'active';
}
}

class CustomerHasMinimumBalanceSpec extends Specification<Customer> {
constructor(private minBalance: number) {
super();
}

isSatisfiedBy(customer: Customer): boolean {
return customer.balance >= this.minBalance;
}
}

// Compose specifications
const eligibleForLoan = new CustomerIsActiveSpec()
.and(new CustomerHasMinimumBalanceSpec(1000));

if (eligibleForLoan.isSatisfiedBy(customer)) {
// Process loan application
}

Type Parameters

  • T

    The type of object this specification validates

Constructors

Methods

Constructors

Methods

  • Checks if the candidate satisfies this specification.

    Parameters

    • candidate: T

      The object to check

    Returns boolean

    true if the candidate satisfies this specification

  • Checks if the candidate satisfies this specification and returns a detailed result.

    This method can be overridden to provide more detailed error messages when a specification is not satisfied.

    Parameters

    • candidate: T

      The object to check

    Returns SpecificationResult

    A result object with success status and optional error message