UNPKG

@webf/rule

Version:

Business rule validation library

25 lines 928 B
import { RuleError } from './error.js'; export interface IRule<T> { key: string; apply(value: T): boolean | Promise<boolean>; } export type RuleClassFn<T> = new () => IRule<T>; export type RuleType<T> = IRule<T> | RuleClassFn<T>; export type Collector = { /** Collects the errors against the failed rules and combine them into one while throwing. */ check: <T>(value: T, ...rules: Array<RuleType<T>>) => Promise<void>; /** Throws if there are some errors */ rejectIfError: () => void; }; export declare abstract class Rule { key: string; abstract apply(value: any): boolean | Promise<boolean>; constructor(); } /** * Creates a rule validator function which throws if any of the validators fail. */ export declare function test<T>(value: T, ...rules: Array<RuleType<T>>): Promise<void>; export declare function withCatch(): Collector; export { RuleError }; //# sourceMappingURL=main.d.ts.map