js-rules-engine
Version:
JavaScript rules engine for validating data object structures.
58 lines • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Condition = void 0;
var ts_dot_prop_1 = require("ts-dot-prop");
/**
* Condition.
*/
var Condition = /** @class */ (function () {
function Condition(config, engine) {
this.init(config, engine);
}
/**
* Evaluate a rule's conditions against the provided data.
*
* @param data Data object to use.
*/
Condition.prototype.evaluate = function (data) {
var factValue = ts_dot_prop_1.get(data, this.fact);
return this.operator.evaluate(factValue, this.value);
};
/**
* To json.
*/
Condition.prototype.toJSON = function () {
return {
fact: this.fact,
operator: this.operator.name,
value: this.value,
};
};
/**
* Init condition from configuration object.
*
* @param json Json object.
* @param engine Engine instance to use.
*/
Condition.prototype.init = function (json, engine) {
if (!engine) {
throw new Error('Condition: constructor requires engine instance');
}
this.engine = engine;
if (!json) {
throw new Error('Condition: constructor requires object');
}
if (!json.fact) {
throw new Error('Condition: "fact" property is required');
}
if (!json.operator) {
throw new Error('Condition: "operator" property is required');
}
this.fact = json.fact;
this.operator = this.engine.getOperator(json.operator);
this.value = json.value;
};
return Condition;
}());
exports.Condition = Condition;
//# sourceMappingURL=condition.js.map