UNPKG

@ivandt/json-rules

Version:

Rule parsing engine for JSON rules

95 lines (94 loc) 4.3 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _a, _JsonRules_jsonRules, _JsonRules_validator, _JsonRules_evaluator; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonRules = void 0; const builder_1 = require("../builder"); const errors_1 = require("../errors"); const evaluator_1 = require("./evaluator"); const validator_1 = require("./validator"); class JsonRules { constructor() { _JsonRules_validator.set(this, new validator_1.Validator()); _JsonRules_evaluator.set(this, new evaluator_1.Evaluator()); } /** * Returns a rule builder class instance. * Allows for the construction of rules using a fluent interface. */ builder() { return new builder_1.Builder(__classPrivateFieldGet(this, _JsonRules_validator, "f")); } /** * 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(rule, criteria, trustRule = false) { // Before we evaluate the rule, we should validate it. // If `trustRuleset` is set to true, we will skip validation. const validationResult = !trustRule && this.validate(rule); if (!trustRule && !validationResult.isValid) { throw new errors_1.RuleError(validationResult); } return __classPrivateFieldGet(this, _JsonRules_evaluator, "f").evaluate(rule, criteria); } /** * 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) { return __classPrivateFieldGet(this, _JsonRules_validator, "f").validate(rule); } /** * Returns a rule builder class instance. * Allows for the construction of rules using a fluent interface. */ static builder() { return __classPrivateFieldGet(this, _a, "f", _JsonRules_jsonRules).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(rule, criteria, trustRule = false) { return __classPrivateFieldGet(_a, _a, "f", _JsonRules_jsonRules).evaluate(rule, criteria, trustRule); } /** * 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) { return __classPrivateFieldGet(_a, _a, "f", _JsonRules_jsonRules).validate(rule); } } exports.JsonRules = JsonRules; _a = JsonRules, _JsonRules_validator = new WeakMap(), _JsonRules_evaluator = new WeakMap(); _JsonRules_jsonRules = { value: new _a() };