diff --git a/package.json b/package.json index 93247bb..b8cce50 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rulepilot", - "version": "1.1.11", + "version": "1.1.12", "description": "Rule parsing engine for JSON rules", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/services/validator.ts b/src/services/validator.ts index 6f99de2..b25e9c6 100644 --- a/src/services/validator.ts +++ b/src/services/validator.ts @@ -20,6 +20,17 @@ export class Validator { // Assume the rule is valid. let result: ValidationResult = { isValid: true }; + // Check the rule is a valid JSON + if (!this.objectDiscovery.isObject(rule)) { + return { + isValid: false, + error: { + message: "The rule must be a valid JSON object.", + element: rule, + }, + }; + } + // Cater for the case where the conditions property is not an array. const conditions = rule.conditions instanceof Array ? rule.conditions : [rule.conditions]; @@ -68,6 +79,17 @@ export class Validator { // Set the type of condition. const type = this.objectDiscovery.conditionType(condition); + // Check if the condition is iterable + if(!Array.isArray(condition[type])) { + return { + isValid: false, + error: { + message: `The condition '${type}' should be iterable.`, + element: condition, + }, + }; + } + // Validate each item in the condition. for (const node of condition[type]) { const isCondition = this.objectDiscovery.isCondition(node);