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 Parameter | Default type | Description |
|---|---|---|
PredicateReturnType extends MaybeAsync<boolean> | - | The return type of the predicate functions. Specifically, true or Promise<true>. |
ValueType | any | The 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
| Parameter | Type |
|---|---|
aConditionToBeMet | RuleRequirement<PredicateReturnType, ValueType> |
Returns
this
this for chaining
Example
Add a requirement for the rule to hold
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")); // trueRule evaluation
Related to the definition of business rules and their evaluation.
doesHold()
abstractdoesHold(value):PredicateReturnType
Evaluates the requirements for the given value, and returns whether the rule holds or not.
Parameters
| Parameter | Type |
|---|---|
value | ValueType |
Returns
PredicateReturnType
hasFailed()
abstracthasFailed(value):PredicateReturnType
Opposite of doesHold
Parameters
| Parameter | Type |
|---|---|
value | ValueType |
Returns
PredicateReturnType
mustHold()
abstractmustHold(value):PredicateReturnTypeextendsboolean?void:Promise<void>
Evaluates the requirements for the given value. If any condition is not met, throws a RulesBroken exception.
Parameters
| Parameter | Type |
|---|---|
value | ValueType |
Returns
PredicateReturnType extends boolean ? void : Promise<void>
collectFailureInto()
abstractcollectFailureInto(failed,value):PredicateReturnTypeextendsboolean?void:Promise<void>
Updates the list of failed assertions with its label if the rule has failed for the given value.
Parameters
| Parameter | Type |
|---|---|
failed | LabeledRule[] |
value | ValueType |
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
| Parameter | Type |
|---|---|
aValue | ValueType |
Returns
RuleEvaluation<PredicateReturnType, ValueType>
Example
const nameNotBlank = Assertion.requiring(
"customer.name.notBlank",
"Name must not be blank",
Requirements.isNotBlank
);
const evaluation = nameNotBlank.evaluateFor("John");
console.log(evaluation.doesHold()); // trueLabeling
isLabeledAs()
isLabeledAs(
aBrokenRuleLabel):boolean
Checks if the assertion has the same id and description as the given one.
Parameters
| Parameter | Type |
|---|---|
aBrokenRuleLabel | LabeledRule |
Returns
boolean
Implementation of
hasLabel()
hasLabel(
anId,aDescription):boolean
Compare the id and description of the assertion with the given ones.
Parameters
| Parameter | Type |
|---|---|
anId | string |
aDescription | string |
Returns
boolean
Implementation of
hasDescription()
hasDescription(
aDescription):boolean
Checks if the assertion has the given description.
Parameters
| Parameter | Type |
|---|---|
aDescription | string |
Returns
boolean
Remarks
This method is used mostly for testing.
Implementation of
hasLabelId()
hasLabelId(
anId):boolean
Compares the id of the assertion with the given one.
Parameters
| Parameter | Type |
|---|---|
anId | string |
Returns
boolean
Implementation of
getId()
getId():
string
Returns the label identifier.
Returns
string
Implementation of
getDescription()
getDescription():
string
Returns
string
