@allbin/entity-logic
Advanced tools
Comparing version 0.3.2 to 0.4.0
116
lib/index.js
@@ -17,2 +17,3 @@ "use strict"; | ||
exports.validateSchema = exports.EntityLogic = void 0; | ||
var luxon_1 = require("luxon"); | ||
var operators_1 = __importDefault(require("./operators")); | ||
@@ -112,3 +113,116 @@ var executeCondition = function (schemaPropsByKey, entities, condition) { | ||
} | ||
// FIXME: validate filter values | ||
if (condition.operator === 'known' || | ||
condition.operator === 'unknown' || | ||
condition.operator === 'true' || | ||
condition.operator === 'false') { | ||
if (typeof condition.value !== 'undefined') { | ||
throw new Error("Invalid condition value for " + condition.type + ":" + condition.operator); | ||
} | ||
} | ||
if (condition.type === 'number') { | ||
switch (condition.operator) { | ||
case 'eq': | ||
case 'neq': | ||
case 'gt': | ||
case 'gte': | ||
case 'lt': | ||
case 'lte': | ||
if (typeof condition.value !== 'number') { | ||
throw new Error("Invalid condition value for " + condition.type + ":" + condition.operator); | ||
} | ||
break; | ||
case 'between': | ||
case 'not_between': | ||
case 'none_of': | ||
case 'any_of': | ||
if (!Array.isArray(condition.value) || | ||
condition.value.some(function (v) { return typeof v !== 'number'; })) { | ||
throw new Error("Invalid condition value for " + condition.type + ":" + condition.operator); | ||
} | ||
break; | ||
} | ||
} | ||
else if (condition.type === 'string') { | ||
switch (condition.operator) { | ||
case 'eq': | ||
case 'neq': | ||
case 'matches': | ||
case 'not_matches': | ||
if (typeof condition.value !== 'string') { | ||
throw new Error("Invalid condition value for " + condition.type + ":" + condition.operator); | ||
} | ||
break; | ||
case 'none_of': | ||
case 'any_of': | ||
if (!Array.isArray(condition.value) || | ||
condition.value.some(function (v) { return typeof v !== 'string'; })) { | ||
throw new Error("Invalid condition value for " + condition.type + ":" + condition.operator); | ||
} | ||
break; | ||
} | ||
} | ||
else if (condition.type === 'enum') { | ||
switch (condition.operator) { | ||
case 'eq': | ||
case 'neq': | ||
case 'matches': | ||
case 'not_matches': | ||
if (typeof condition.value !== 'string') { | ||
throw new Error("Invalid condition value for " + condition.type + ":" + condition.operator); | ||
} | ||
break; | ||
case 'none_of': | ||
case 'any_of': | ||
if (!Array.isArray(condition.value) || | ||
condition.value.some(function (v) { | ||
return typeof v !== 'string' || | ||
!prop.alternatives || | ||
!prop.alternatives.includes(v); | ||
})) { | ||
throw new Error("Invalid condition value for " + condition.type + ":" + condition.operator); | ||
} | ||
break; | ||
} | ||
} | ||
else if (condition.type === 'date') { | ||
switch (condition.operator) { | ||
case 'before': | ||
case 'after': | ||
if (!luxon_1.DateTime.isDateTime(condition.value)) { | ||
throw new Error("Invalid condition value for " + condition.type + ":" + condition.operator); | ||
} | ||
break; | ||
case 'between': | ||
case 'not_between': | ||
if (!Array.isArray(condition.value) || | ||
condition.value.some(function (v) { return !luxon_1.DateTime.isDateTime(v); })) { | ||
throw new Error("Invalid condition value for " + condition.type + ":" + condition.operator); | ||
} | ||
break; | ||
} | ||
} | ||
else if (condition.type === 'array:number') { | ||
switch (condition.operator) { | ||
case 'none_of': | ||
case 'any_of': | ||
case 'all_of': | ||
if (!Array.isArray(condition.value) || | ||
condition.value.some(function (v) { return typeof v !== 'number'; })) { | ||
throw new Error("Invalid condition value for " + condition.type + ":" + condition.operator); | ||
} | ||
break; | ||
} | ||
} | ||
else if (condition.type === 'array:string') { | ||
switch (condition.operator) { | ||
case 'none_of': | ||
case 'any_of': | ||
case 'all_of': | ||
if (!Array.isArray(condition.value) || | ||
condition.value.some(function (v) { return typeof v !== 'string'; })) { | ||
throw new Error("Invalid condition value for " + condition.type + ":" + condition.operator); | ||
} | ||
break; | ||
} | ||
} | ||
return op; | ||
@@ -115,0 +229,0 @@ }; |
{ | ||
"name": "@allbin/entity-logic", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "Logic solver for mobilix filters", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
68840
1832