UNPKG

rulepilot

Version:

Rule parsing engine for JSON rules

142 lines (141 loc) 5.79 kB
import { Builder } from "../builder"; import { ValidationResult } from "./validator"; import { Rule, Constraint, IntrospectionResult } from "../types"; export declare class RulePilot { #private; /** * Returns a rule builder class instance. * Allows for the construction of rules using a fluent interface. */ builder(): Builder; /** * Adds a mutation to the rule pilot instance. * Mutations allow for the modification of the criteria before * it is evaluated against a rule. * * @param name The name of the mutation. * @param mutation The mutation function. */ addMutation(name: string, mutation: Function): RulePilot; /** * Removes a mutation to the rule pilot instance. * Any cached mutation values for this mutation will be purged. * * @param name The name of the mutation. */ removeMutation(name: string): RulePilot; /** * Clears the mutator cache. * The entire cache, or cache for a specific mutator, can be cleared * by passing or omitting the mutator name as an argument. * * @param name The mutator name to clear the cache for. */ clearMutationCache(name?: string): RulePilot; /** * Evaluates a rule against a set of criteria and returns the result. * If the criteria is an array (indicating multiple criteria to test), * the rule will be evaluated against each item in the array and * an array of results will be returned. * * @param rule The rule to evaluate. * @param criteria The criteria to evaluate the rule against. * @param trustRule Set true to avoid validating the rule before evaluating it (faster). * @throws RuleError if the rule is invalid. */ evaluate<T>(rule: Rule, criteria: object | object[], trustRule?: boolean): Promise<T | boolean>; /** * Given a rule, checks the constraints and conditions to determine * the possible range of input criteria which would be satisfied by the rule. * * @param rule The rule to evaluate. * @param criteria The criteria to introspect against. * @param subjects The subjects to introspect for. * @throws RuleError if the rule is invalid * @throws RuleTypeError if the rule is not granular */ introspect<R = string>(rule: Rule, criteria: Omit<Constraint, "operator"> | Omit<Constraint, "operator">[], subjects: string[]): IntrospectionResult<R>[]; /** * Returns the number of outcomes that a rule has. * @param rule The rule to check. */ numOutcomes(rule: Rule): number; /** * Takes in a rule as a parameter and returns a ValidationResult * indicating whether the rule is valid or not. * * Invalid rules will contain an error property which contains a message and the element * that caused the validation to fail. * * @param rule The rule to validate. */ validate(rule: Rule): ValidationResult; /** * Returns a rule builder class instance. * Allows for the construction of rules using a fluent interface. */ static builder(): Builder; /** * Evaluates a rule against a set of criteria and returns the result. * If the criteria is an array (indicating multiple criteria to test), * the rule will be evaluated against each item in the array and * an array of results will be returned. * * @param rule The rule to evaluate. * @param criteria The criteria to evaluate the rule against. * @param trustRule Set true to avoid validating the rule before evaluating it (faster). * @throws RuleError if the rule is invalid. */ static evaluate<T>(rule: Rule, criteria: object | object[], trustRule?: boolean): Promise<T | boolean>; /** * Given a rule, checks the constraints and conditions to determine * the possible range of input criteria which would be satisfied by the rule. * * @param rule The rule to introspect. * @param criteria The criteria to introspect against. * @param subjects The subjects to introspect for. * @throws RuleError if the rule is invalid * @throws RuleTypeError if the rule is not granular */ static introspect<R = string>(rule: Rule, criteria: Omit<Constraint, "operator"> | Omit<Constraint, "operator">[], subjects: string[]): IntrospectionResult<R>[]; /** * Returns the number of outcomes that a rule has. * @param rule The rule to check. */ static numOutcomes(rule: Rule): number; /** * Takes in a rule as a parameter and returns a ValidationResult * indicating whether the rule is valid or not. * * Invalid rules will contain an error property which contains a message and the element * that caused the validation to fail. * * @param rule The rule to validate. */ static validate(rule: Rule): ValidationResult; /** * Adds a mutation. * * Mutations allow for the modification of the criteria before * it is evaluated against a rule. * * @param name The name of the mutation. * @param mutation The mutation function. */ static addMutation(name: string, mutation: Function): RulePilot; /** * Removes a mutation to the rule pilot instance. * Any cached mutation values for this mutation will be purged. * * @param name The name of the mutation. */ static removeMutation(name: string): RulePilot; /** * Clears the mutator cache. * The entire cache, or cache for a specific mutator, can be cleared * by passing or omitting the mutator name as an argument. * * @param name The mutator name to clear the cache for. */ static clearMutationCache(name?: string): RulePilot; }