Skip to content

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 ​

Example ​

Basic usage

ts
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 ParameterDefault typeDescription
ValueTypeanyThe type of value this assertion applies to.

Creation ​

fromJson() ​

static fromJson(assertionAsJson): Assertion<any>

Parameters ​

ParameterType
assertionAsJsonRuleLabelAsJson

Returns ​

Assertion<any>


labeled() ​

static labeled<ValueType>(id, description): Assertion<ValueType>

Type Parameters ​

Type ParameterDefault type
ValueTypeany

Parameters ​

ParameterType
idstring
descriptionstring

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 ParameterDefault type
ValueTypeany

Parameters ​

ParameterType
anIdstring
aDescriptionstring
aCondition(value) => boolean

Returns ​

Assertion<ValueType>

Examples ​

Without a value

ts
/** Typed as Assertion<void> */
const systemIsReady = Assertion.requiring(
  "sys.ready",
  "System must be ready",
  () => isReady()
);

With a value

ts
/** 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 ​

ParameterType
aConditionToBeMetRuleRequirement<boolean, 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

Inherited from ​

Rule.require

Rule evaluation ​

doesHold() ​

doesHold(value): boolean

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

Parameters ​

ParameterType
valueValueType

Returns ​

boolean

Overrides ​

Rule.doesHold


hasFailed() ​

hasFailed(value): boolean

Opposite of doesHold

Parameters ​

ParameterType
valueValueType

Returns ​

boolean

Overrides ​

Rule.hasFailed


mustHold() ​

mustHold(value): void

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

Parameters ​

ParameterType
valueValueType

Returns ​

void

Overrides ​

Rule.mustHold


collectFailureInto() ​

collectFailureInto(failed, value): void

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

Parameters ​

ParameterType
failedLabeledRule[]
valueValueType

Returns ​

void

Overrides ​

Rule.collectFailureInto


evaluateFor() ​

evaluateFor(aValue): RuleEvaluation<boolean, ValueType>

Prepares a RuleEvaluation for the given value.

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

Parameters ​

ParameterType
aValueValueType

Returns ​

RuleEvaluation<boolean, 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

Inherited from ​

Rule.evaluateFor

Labeling ​

isLabeledAs() ​

isLabeledAs(aBrokenRuleLabel): boolean

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

Parameters ​

ParameterType
aBrokenRuleLabelLabeledRule

Returns ​

boolean

Inherited from ​

Rule.isLabeledAs


hasLabel() ​

hasLabel(anId, aDescription): boolean

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

Parameters ​

ParameterType
anIdstring
aDescriptionstring

Returns ​

boolean

Inherited from ​

Rule.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.

Inherited from ​

Rule.hasDescription


hasLabelId() ​

hasLabelId(anId): boolean

Compares the id of the assertion with the given one.

Parameters ​

ParameterType
anIdstring

Returns ​

boolean

Inherited from ​

Rule.hasLabelId


getId() ​

getId(): string

Returns the label identifier.

Returns ​

string

Inherited from ​

Rule.getId


getDescription() ​

getDescription(): string

Returns ​

string

Inherited from ​

Rule.getDescription