UNPKG

@ivandt/json-rules

Version:

Rule parsing engine for JSON rules

111 lines (110 loc) 5.03 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_instances, _Builder_rule, _Builder_validator, _Builder_addCondition; Object.defineProperty(exports, "__esModule", { value: true }); exports.Builder = void 0; const errors_1 = require("../errors"); class Builder { constructor(validator) { _Builder_instances.add(this); /** 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 an "all" condition with the given constraints * @param nodes The constraints of the condition */ all(...nodes) { __classPrivateFieldGet(this, _Builder_instances, "m", _Builder_addCondition).call(this, "all", nodes); return this; } /** * Adds an "any" condition with the given constraints * @param nodes The constraints of the condition */ any(...nodes) { __classPrivateFieldGet(this, _Builder_instances, "m", _Builder_addCondition).call(this, "any", nodes); return this; } /** * Adds a "none" condition with the given constraints * @param nodes The constraints of the condition */ none(...nodes) { __classPrivateFieldGet(this, _Builder_instances, "m", _Builder_addCondition).call(this, "none", nodes); return this; } /** * 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, operator, value) { if (value === undefined) { return { field, operator }; } 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; } /** * 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 } : {})); } /** * 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(), _Builder_instances = new WeakSet(), _Builder_addCondition = function _Builder_addCondition(type, nodes) { const condition = { [type]: nodes }; if (Array.isArray(__classPrivateFieldGet(this, _Builder_rule, "f").conditions)) { __classPrivateFieldGet(this, _Builder_rule, "f").conditions.push(condition); } else { __classPrivateFieldGet(this, _Builder_rule, "f").conditions = [__classPrivateFieldGet(this, _Builder_rule, "f").conditions, condition]; } };