self-assert / Assertion
Class: Assertion<ValueType>
Represents a validation rule in the problem domain.
An Assertion
expresses a condition that must hold for a given value. These rules are defined using one or more predicate functions, added via require. The assertion is considered to "hold" when all the conditions evaluate to true
.
See
- Requirements provides a list of built-in requirements.
Example
Basic usage
const integerGreaterThan42 = Assertion.requiring(
"integer.greaterThan42",
"A human-readable description",
Requirements.and(Requirements.isInteger, Requirements.greaterThan(42))
);
console.log(integerGreaterThan42.doesHold(42)); // false
console.log(integerGreaterThan42.doesHold(42.1)); // false
console.log(integerGreaterThan42.doesHold(43)); // true
Extends
Rule
<boolean
,ValueType
>
Type Parameters
Type Parameter | Default type | Description |
---|---|---|
ValueType | any | The type of value this assertion applies to. |
Creation
fromJson()
static
fromJson(assertionAsJson
):Assertion
<any
>
Parameters
Parameter | Type |
---|---|
assertionAsJson | RuleLabelAsJson |
Returns
Assertion
<any
>
labeled()
static
labeled<ValueType
>(id
,description
):Assertion
<ValueType
>
Type Parameters
Type Parameter | Default type |
---|---|
ValueType | any |
Parameters
Parameter | Type |
---|---|
id | string |
description | string |
Returns
Assertion
<ValueType
>
requiring()
static
requiring<ValueType
>(anId
,aDescription
,aCondition
):Assertion
<ValueType
>
Creates a new assertion with the given id, description and requirement.
If the requirement does not depend on a value (i.e., a function with no parameters), the rule will be typed as Assertion<void>
.
Type Parameters
Type Parameter | Default type |
---|---|
ValueType | any |
Parameters
Parameter | Type |
---|---|
anId | string |
aDescription | string |
aCondition | (value ) => boolean |
Returns
Assertion
<ValueType
>
Examples
Without a value
/** Typed as Assertion<void> */
const systemIsReady = Assertion.requiring(
"sys.ready",
"System must be ready",
() => isReady()
);
With a value
/** Typed as Assertion<number> */
const greaterThan18 = Assertion.requiring(
"age.min",
"Must be over 18",
(age: number) => age > 18
);
Rule definition
require()
require(
aConditionToBeMet
):this
Adds a necessary requirement for the rule to hold.
Parameters
Parameter | Type |
---|---|
aConditionToBeMet | RuleRequirement <boolean , 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")); // true
Inherited from
Rule evaluation
doesHold()
doesHold(
value
):boolean
Evaluates the requirements for the given value, and returns whether the rule holds or not.
Parameters
Parameter | Type |
---|---|
value | ValueType |
Returns
boolean
Overrides
hasFailed()
hasFailed(
value
):boolean
Opposite of doesHold
Parameters
Parameter | Type |
---|---|
value | ValueType |
Returns
boolean
Overrides
mustHold()
mustHold(
value
):void
Evaluates the requirements for the given value. If any condition is not met, throws a RulesBroken exception.
Parameters
Parameter | Type |
---|---|
value | ValueType |
Returns
void
Overrides
collectFailureInto()
collectFailureInto(
failed
,value
):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
void
Overrides
evaluateFor()
evaluateFor(
aValue
):RuleEvaluation
<boolean
,ValueType
>
Prepares a RuleEvaluation for the given value.
This is the same as new RuleEvaluation(rule, value)
.
Parameters
Parameter | Type |
---|---|
aValue | ValueType |
Returns
RuleEvaluation
<boolean
, 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()); // true
Inherited from
Labeling
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
Inherited from
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
Inherited from
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.
Inherited from
hasLabelId()
hasLabelId(
anId
):boolean
Compares the id of the assertion with the given one.
Parameters
Parameter | Type |
---|---|
anId | string |
Returns
boolean
Inherited from
getId()
getId():
string
Returns the label identifier.
Returns
string
Inherited from
getDescription()
getDescription():
string
Returns
string