self-assert
Version:
A small TypeScript library for designing models with built-in validity.
52 lines • 2.15 kB
TypeScript
import { RuleLabel, RuleLabelAsJson } from "./RuleLabel";
import { Rule } from "./Rule";
import type { LabeledRule, LabelId } from "./types";
/**
* 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
* {@link Rule.require require}. The assertion is considered to "hold" when all the conditions evaluate to `true`.
*
* @see
* - {@link Requirements} provides a list of built-in requirements.
*
* @template ValueType The type of value this assertion applies to.
*
* @example
* Basic usage
* {@includeCode ../../../../examples/snippets/rules.ts#assertion-basic-usage}
*
* @category Rules
*/
export declare class Assertion<ValueType = any> extends Rule<boolean, ValueType> {
/** @category Creation */
static fromJson(assertionAsJson: RuleLabelAsJson): Assertion<any>;
/** @category Creation */
static labeled<ValueType = any>(id: LabelId, description: string): Assertion<ValueType>;
/** @internal */
static requiring(anId: LabelId, aDescription: string, aCondition: () => boolean): Assertion<void>;
/**
* 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>`.
*
* @example
* Without a value
* {@includeCode ../../../../examples/snippets/rules.ts#assertion-requiring-void}
*
* @example
* With a value
* {@includeCode ../../../../examples/snippets/rules.ts#assertion-requiring-value}
*
* @category Creation
*/
static requiring<ValueType = any>(anId: LabelId, aDescription: string, aCondition: (value: ValueType) => boolean): Assertion<ValueType>;
protected constructor(label: RuleLabel);
doesHold(value: ValueType): boolean;
hasFailed(value: ValueType): boolean;
mustHold(value: ValueType): void;
collectFailureInto(failed: LabeledRule[], value: ValueType): void;
}
//# sourceMappingURL=Assertion.d.ts.map