Skip to content

Commit

Permalink
Better validation of rules
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbrg committed Aug 11, 2023
1 parent 0573ea5 commit 517f473
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
22 changes: 22 additions & 0 deletions src/services/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 517f473

Please sign in to comment.