Skip to content

self-assert / Rule

Class: abstract Rule<PredicateReturnType, ValueType>

Represents a validation rule in the problem domain.

This is the base class for all rules.

  • Use Assertion for rules that can be evaluated synchronously.
  • Use Inquiry for rules that need to be evaluated asynchronously.

Rules are identified by a unique identifier (labelId) and a human-readable description. These identifiers are meant to be meaningful within the domain, and can be used to route or display validation errors.

Extended by

Type Parameters

Type ParameterDefault typeDescription
PredicateReturnType extends MaybeAsync<boolean>-The return type of the predicate functions. Specifically, true or Promise<true>.
ValueTypeanyThe type of value this rule applies to.

Implements

Rule definition

Related to the definition of requirements for business rules.

require()

require(aConditionToBeMet): this

Adds a necessary requirement for the rule to hold.

Parameters

ParameterType
aConditionToBeMetRuleRequirement<PredicateReturnType, ValueType>

Returns

this

this for chaining

Example

Add a requirement for the rule to hold

ts
const nameValid = Assertion.requiring(
  "customer.name.notBlank",
  "Name must not be blank",
  Requirements.isNotBlank
);
nameValid.require((name) => name !== "Johnny");

console.log(nameValid.hasFailed("")); // true
console.log(nameValid.hasFailed("Johnny")); // true
console.log(nameValid.doesHold("John")); // true

Rule evaluation

Related to the definition of business rules and their evaluation.

doesHold()

abstract doesHold(value): PredicateReturnType

Evaluates the requirements for the given value, and returns whether the rule holds or not.

Parameters

ParameterType
valueValueType

Returns

PredicateReturnType


hasFailed()

abstract hasFailed(value): PredicateReturnType

Opposite of doesHold

Parameters

ParameterType
valueValueType

Returns

PredicateReturnType


mustHold()

abstract mustHold(value): PredicateReturnType extends boolean ? void : Promise<void>

Evaluates the requirements for the given value. If any condition is not met, throws a RulesBroken exception.

Parameters

ParameterType
valueValueType

Returns

PredicateReturnType extends boolean ? void : Promise<void>


collectFailureInto()

abstract collectFailureInto(failed, value): PredicateReturnType extends boolean ? void : Promise<void>

Updates the list of failed assertions with its label if the rule has failed for the given value.

Parameters

ParameterType
failedLabeledRule[]
valueValueType

Returns

PredicateReturnType extends boolean ? void : Promise<void>


evaluateFor()

evaluateFor(aValue): RuleEvaluation<PredicateReturnType, ValueType>

Prepares a RuleEvaluation for the given value.

This is the same as new RuleEvaluation(rule, value).

Parameters

ParameterType
aValueValueType

Returns

RuleEvaluation<PredicateReturnType, ValueType>

Example

ts
const nameNotBlank = Assertion.requiring(
  "customer.name.notBlank",
  "Name must not be blank",
  Requirements.isNotBlank
);

const evaluation = nameNotBlank.evaluateFor("John");
console.log(evaluation.doesHold()); // true

Labeling

isLabeledAs()

isLabeledAs(aBrokenRuleLabel): boolean

Checks if the assertion has the same id and description as the given one.

Parameters

ParameterType
aBrokenRuleLabelLabeledRule

Returns

boolean

Implementation of

LabeledRule.isLabeledAs


hasLabel()

hasLabel(anId, aDescription): boolean

Compare the id and description of the assertion with the given ones.

Parameters

ParameterType
anIdstring
aDescriptionstring

Returns

boolean

Implementation of

LabeledRule.hasLabel


hasDescription()

hasDescription(aDescription): boolean

Checks if the assertion has the given description.

Parameters

ParameterType
aDescriptionstring

Returns

boolean

Remarks

This method is used mostly for testing.

Implementation of

LabeledRule.hasDescription


hasLabelId()

hasLabelId(anId): boolean

Compares the id of the assertion with the given one.

Parameters

ParameterType
anIdstring

Returns

boolean

Implementation of

LabeledRule.hasLabelId


getId()

getId(): string

Returns the label identifier.

Returns

string

Implementation of

LabeledRule.getId


getDescription()

getDescription(): string

Returns

string

Implementation of

LabeledRule.getDescription