UNPKG

rulepilot

Version:

Rule parsing engine for JSON rules

79 lines (78 loc) 3.51 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; 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 _Builder_rule, _Builder_validator; Object.defineProperty(exports, "__esModule", { value: true }); exports.Builder = void 0; const errors_1 = require("../errors"); class Builder { constructor(validator) { /** Stores the rule being constructed */ _Builder_rule.set(this, { conditions: [] }); /** Holds a reference to the Validator class */ _Builder_validator.set(this, void 0); __classPrivateFieldSet(this, _Builder_validator, validator, "f"); } /** * Adds a node (in the root) to the rule being constructed * @param node The node to add to the rule */ add(node) { __classPrivateFieldGet(this, _Builder_rule, "f").conditions.push(node); return this; } /** * 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, nodes, result) { return Object.assign({ [type]: nodes }, (result ? { result } : {})); } /** * Creates a new constraint node * @param field The field to apply the constraint to * @param operator The operator to apply to the field * @param value The value to compare the field to */ constraint(field, operator, value) { return { field, operator, value, }; } /** * Sets the default value of the rule being constructed * @param value The default value of the rule */ default(value) { __classPrivateFieldGet(this, _Builder_rule, "f").default = value; return this; } /** * 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) { if (!validate) return __classPrivateFieldGet(this, _Builder_rule, "f"); const validationResult = __classPrivateFieldGet(this, _Builder_validator, "f").validate(__classPrivateFieldGet(this, _Builder_rule, "f")); if (validationResult.isValid) return __classPrivateFieldGet(this, _Builder_rule, "f"); throw new errors_1.RuleError(validationResult); } } exports.Builder = Builder; _Builder_rule = new WeakMap(), _Builder_validator = new WeakMap();