self-assert / Ruleset
Class: Ruleset
Runs all rules and throws an error if any has failed. The failed rules are included in the error.
Constructors
Constructor
new Ruleset(
assertions,rules):Ruleset
Parameters
| Parameter | Type |
|---|---|
assertions | SelfContainedAssertion[] |
rules | SelfContainedRule[] |
Returns
Ruleset
Methods
ensureAll()
staticensureAll(...assertions):void
Evaluates all assertions synchronously and throws an error if any has failed.
Parameters
| Parameter | Type |
|---|---|
...assertions | SelfContainedAssertions[] |
Returns
void
Throws
RulesBroken if any rule has failed.
Example
ts
const firstNameNotEmpty = Assertion.requiring(
"firstName",
firstNameNotEmptyDescription,
Requirements.isNotEmpty
);
const lastNameNotEmpty = Assertion.requiring(
"lastName",
lastNameNotEmptyDescription,
Requirements.isNotEmpty
);
/** Throws because last name is empty */
Ruleset.ensureAll(
firstNameNotEmpty.evaluateFor("Jane"),
lastNameNotEmpty.evaluateFor("")
);workOn()
staticworkOn(...rules):Promise<void>
Evaluates all rules asynchronously and throws an error if any has failed.
Parameters
| Parameter | Type |
|---|---|
...rules | SelfContainedRules[] |
Returns
Promise<void>
Throws
RulesBroken if any rule has failed
Example
ts
const emailMustBeUnique = Inquiry.requiring<string>(
"user.email.unique",
"Email must be unique",
async (email) => !(await isEmailTaken(email))
);
const emailMustNotBeDisposable = Inquiry.requiring<string>(
"email",
emailMustNotBeDisposableDescription,
async (email) => !(await isDisposable(email))
);
/** Throws because email is disposable */
await Ruleset.workOn(
emailMustBeUnique.evaluateFor("example@disposable.com"),
emailMustNotBeDisposable.evaluateFor("example@disposable.com")
);mustHold()
mustHold():
Promise<void>
Returns
Promise<void>
ensure()
ensure():
void
Returns
void
