self-assert / Inquiry
Class: Inquiry<ValueType>
Represents a rule that needs to be evaluated asynchronously.
Example
// #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 Parameter | Default type |
---|---|
ValueType | any |
Creation
labeled()
static
labeled<ValueType
>(anId
,aDescription
):Inquiry
<ValueType
>
Type Parameters
Type Parameter | Default type |
---|---|
ValueType | any |
Parameters
Parameter | Type |
---|---|
anId | string |
aDescription | string |
Returns
Inquiry
<ValueType
>
requiring()
static
requiring<ValueType
>(anId
,aDescription
,aCondition
):Inquiry
<ValueType
>
Type Parameters
Type Parameter | Default type |
---|---|
ValueType | any |
Parameters
Parameter | Type |
---|---|
anId | string |
aDescription | string |
aCondition | (value ) => Promise <boolean > |
Returns
Inquiry
<ValueType
>
See
Rule definition
require()
require(
aConditionToBeMet
):this
Adds a necessary requirement for the rule to hold.
Parameters
Parameter | Type |
---|---|
aConditionToBeMet | RuleRequirement <Promise <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
):Promise
<boolean
>
Evaluates the requirements for the given value, and returns whether the rule holds or not.
Parameters
Parameter | Type |
---|---|
value | ValueType |
Returns
Promise
<boolean
>
Overrides
hasFailed()
hasFailed(
value
):Promise
<boolean
>
Opposite of doesHold
Parameters
Parameter | Type |
---|---|
value | ValueType |
Returns
Promise
<boolean
>
Overrides
mustHold()
mustHold(
value
):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
Promise
<void
>
Overrides
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
Parameter | Type |
---|---|
failed | LabeledRule [] |
value | ValueType |
Returns
Promise
<void
>
Overrides
evaluateFor()
evaluateFor(
aValue
):RuleEvaluation
<Promise
<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
<Promise
<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