UNPKG

self-assert

Version:

A small TypeScript library for designing models with built-in validity.

86 lines 3.39 kB
import { RuleEvaluation } from "./RuleEvaluation"; import { RuleLabel } from "./RuleLabel"; import type { LabelId, LabeledRule, MaybeAsync, RuleRequirement } from "./types"; /** * Represents a validation rule in the problem domain. * * This is the base class for all rules. * - Use {@link Assertion} for rules that can be evaluated **synchronously**. * - Use {@link Inquiry} for rules that need to be evaluated **asynchronously**. * * Rules are identified by a unique identifier (`labelId`) and a human-readable description. * These identifiers are meant to be meaningful within the domain, * and can be used to route or display validation errors. * * @template PredicateReturnType The return type of the predicate functions. * Specifically, `true` or `Promise<true>`. * @template ValueType The type of value this rule applies to. * * @category Rules * @categoryDescription Rule evaluation * Related to the definition of business rules and their evaluation. * @categoryDescription Rule definition * Related to the definition of requirements for business rules. */ export declare abstract class Rule<PredicateReturnType extends MaybeAsync<boolean>, ValueType = any> implements LabeledRule { protected label: RuleLabel; protected readonly requirements: RuleRequirement<PredicateReturnType, ValueType>[]; protected constructor(label: RuleLabel); /** * Evaluates the requirements for the given value, * and returns whether the rule holds or not. * * @category Rule evaluation */ abstract doesHold(value: ValueType): PredicateReturnType; /** * Opposite of {@link doesHold} * * @category Rule evaluation */ abstract hasFailed(value: ValueType): PredicateReturnType; /** * Evaluates the requirements for the given value. * If any condition is not met, throws a {@link RulesBroken} exception. * * @category Rule evaluation */ abstract mustHold(value: ValueType): PredicateReturnType extends boolean ? void : Promise<void>; /** * Updates the list of failed assertions with its label * if the rule has failed for the given value. * * @category Rule evaluation */ abstract collectFailureInto(failed: LabeledRule[], value: ValueType): PredicateReturnType extends boolean ? void : Promise<void>; /** * Adds a necessary requirement for the rule to hold. * * @example * Add a requirement for the rule to hold * {@includeCode ../../../../examples/snippets/rules.ts#require} * * * @returns `this` for chaining * @category Rule definition */ require(aConditionToBeMet: RuleRequirement<PredicateReturnType, ValueType>): this; /** * Prepares a {@link RuleEvaluation} for the given value. * * This is the same as `new RuleEvaluation(rule, value)`. * * @example * {@includeCode ../../../../examples/snippets/rules.ts#evaluateFor} * * @category Rule evaluation */ evaluateFor(aValue: ValueType): RuleEvaluation<PredicateReturnType, ValueType>; isLabeledAs(aBrokenRuleLabel: LabeledRule): boolean; hasLabel(anId: LabelId, aDescription: string): boolean; hasDescription(aDescription: string): boolean; hasLabelId(anId: LabelId): boolean; getId(): LabelId; getDescription(): string; } //# sourceMappingURL=Rule.d.ts.map