Skip to content

self-assert / Inquiry

Class: Inquiry<ValueType> ​

Represents a rule that needs to be evaluated asynchronously.

Example ​

ts
// #region email-unique
const emailMustBeUnique = Inquiry.requiring<string>(
  "user.email.unique",
  "Email must be unique",
  async (email) => !(await isEmailTaken(email))
);
// #endregion email-unique

await emailMustBeUnique.mustHold("taken@email.com");

Extends ​

  • Rule<Promise<boolean>, ValueType>

Type Parameters ​

Type ParameterDefault type
ValueTypeany

Creation ​

labeled() ​

static labeled<ValueType>(anId, aDescription): Inquiry<ValueType>

Type Parameters ​

Type ParameterDefault type
ValueTypeany

Parameters ​

ParameterType
anIdstring
aDescriptionstring

Returns ​

Inquiry<ValueType>


requiring() ​

static requiring<ValueType>(anId, aDescription, aCondition): Inquiry<ValueType>

Type Parameters ​

Type ParameterDefault type
ValueTypeany

Parameters ​

ParameterType
anIdstring
aDescriptionstring
aCondition(value) => Promise<boolean>

Returns ​

Inquiry<ValueType>

See ​

Assertion.requiring

Rule definition ​

require() ​

require(aConditionToBeMet): this

Adds a necessary requirement for the rule to hold.

Parameters ​

ParameterType
aConditionToBeMetRuleRequirement<Promise<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): Promise<boolean>

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

Parameters ​

ParameterType
valueValueType

Returns ​

Promise<boolean>

Overrides ​

Rule.doesHold


hasFailed() ​

hasFailed(value): Promise<boolean>

Opposite of doesHold

Parameters ​

ParameterType
valueValueType

Returns ​

Promise<boolean>

Overrides ​

Rule.hasFailed


mustHold() ​

mustHold(value): Promise<void>

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

Parameters ​

ParameterType
valueValueType

Returns ​

Promise<void>

Overrides ​

Rule.mustHold


collectFailureInto() ​

collectFailureInto(failed, value): 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 ​

Promise<void>

Overrides ​

Rule.collectFailureInto


evaluateFor() ​

evaluateFor(aValue): RuleEvaluation<Promise<boolean>, ValueType>

Prepares a RuleEvaluation for the given value.

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

Parameters ​

ParameterType
aValueValueType

Returns ​

RuleEvaluation<Promise<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