UNPKG

js-rules-engine

Version:

JavaScript rules engine for validating data object structures.

136 lines (135 loc) 3.71 kB
import { Condition } from './condition'; import { Engine } from './engine'; import { RuleJson } from './interfaces'; /** * Rule. */ export declare class Rule { constructor(json?: RuleJson, engine?: Engine); /** * Rule type. */ private type; /** * Rule items. */ private items; /** * Engine instance. */ private engine; /** * Add a condition with an equals operator. * * @param fact Property name or dot notation path. * @param value Value to compare. */ equals(fact: string, value: any): this; /** * Add a condition with an notEquals operator. * * @param fact Property name or dot notation path. * @param value Value to compare. */ notEquals(fact: string, value: any): this; /** * Add a condition with an in operator. * * @param fact Property name or dot notation path. * @param value Value to compare. */ in(fact: string, value: string): this; /** * Add a condition with an notIn operator. * * @param fact Property name or dot notation path. * @param value Value to compare. */ notIn(fact: string, value: string): this; /** * Add a condition with an contains operator. * * @param fact Property name or dot notation path. * @param value Value to compare. */ contains(fact: string, value: any): this; /** * Add a condition with an notContains operator. * * @param fact Property name or dot notation path. * @param value Value to compare. */ notContains(fact: string, value: any): this; /** * Add a condition with an lessThan operator. * * @param fact Property name or dot notation path. * @param value Value to compare. */ lessThan(fact: string, value: number): this; /** * Add a condition with an lessThanOrEquals operator. * * @param fact Property name or dot notation path. * @param value Value to compare. */ lessThanOrEquals(fact: string, value: number): this; /** * Add a condition with an greaterThan operator. * * @param fact Property name or dot notation path. * @param value Value to compare. */ greaterThan(fact: string, value: number): this; /** * Add a condition with an greaterThanOrEquals operator. * * @param fact Property name or dot notation path. * @param value Value to compare. */ greaterThanOrEquals(fact: string, value: number): this; /** * Add a condition. * * @param fact Property name or dot notation path. * @param operator Name of operator to use. * @param value Value to compare. */ add(fact: string, operator: string, value: any): this; /** * Add a nested AND rule. * * @param fn Callback function. */ and(fn: (rule: Rule) => void): this; /** * Add a nested OR rule. * * @param fn Callback function. */ or(fn: (rule: Rule) => void): this; /** * Evaluate a rule's conditions against the provided data. * * @param data Data object to use. */ evaluate(data: Record<string, unknown>): boolean; /** * To json. */ toJSON(): { [x: string]: (Condition | Rule)[]; }; /** * Init rule from json object. * * @param json Json object. */ private init; /** * Identifies if a json object is a rule or a condition. * * @param json Object to check. */ private isRule; }