UNPKG

@ivandt/json-rules

Version:

Rule parsing engine for JSON rules

52 lines (51 loc) 2.01 kB
import { Validator } from "../services"; import { Rule, Operator, Condition, Constraint, ConditionType } from "../types"; export declare class Builder { #private; constructor(validator: Validator); /** * Adds an "all" condition with the given constraints * @param nodes The constraints of the condition */ all(...nodes: (Constraint | Condition)[]): Builder; /** * Adds an "any" condition with the given constraints * @param nodes The constraints of the condition */ any(...nodes: (Constraint | Condition)[]): Builder; /** * Adds a "none" condition with the given constraints * @param nodes The constraints of the condition */ none(...nodes: (Constraint | Condition)[]): Builder; /** * Creates a constraint with the given field, operator and optional value * @param field The field to check * @param operator The operator to apply to the field * @param value The value to compare the field to (optional for some operators) */ constraint(field: string, operator: Operator, value?: any): Constraint; /** * Sets the default value of the rule being constructed * @param value The default value of the rule */ default(value: Rule["default"]): Builder; /** * Adds a node (in the root) to the rule being constructed * @param node The node to add to the rule */ add(node: Condition): Builder; /** * Creates a new condition node * @param type The type of condition * @param nodes Any child nodes of the condition * @param result The result of the condition node (for granular rules) */ condition(type: ConditionType, nodes: Condition[ConditionType], result?: Condition["result"]): Condition; /** * Builds the rule being and returns it * @param validate Whether to validate the rule before returning it * @throws Error if validation is enabled and the rule is invalid */ build(validate?: boolean): Rule; }