Skip to content

Commit

Permalink
feat(core): Allow multiple criteria when introspecting
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbrg committed Dec 2, 2024
1 parent 62add5d commit a4b5d19
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
14 changes: 7 additions & 7 deletions src/services/introspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Introspector {

introspect<R>(
rule: Rule,
criteria: Omit<Constraint, "operator">,
criteria: Omit<Constraint, "operator">[],
subjects: string[]
): IntrospectionResult<R>[] {
// We care about all the possible values for the subjects which will satisfy
Expand All @@ -41,7 +41,7 @@ export class Introspector {
rule.conditions[i] = this.#reverseNoneToAll(rule.conditions[i]);
rule.conditions[i] = this.#removeIrrelevantConstraints(
rule.conditions[i],
[...subjects, criteria.field]
[...subjects, ...criteria.map((c) => c.field)]
);
}

Expand Down Expand Up @@ -374,7 +374,7 @@ export class Introspector {
*/
#introspectConditions(
condition: Condition,
criteria: Omit<Constraint, "operator">,
criteria: Omit<Constraint, "operator">[],
parentType: keyof Condition = null,
parentResults: Map<string, Constraint[]> = new Map(),
depth: number = 0
Expand Down Expand Up @@ -601,17 +601,17 @@ export class Introspector {
*/
protected test(
candidates: Constraint[],
criteria: Omit<Constraint, "operator">,
criteria: Omit<Constraint, "operator">[],
item: Constraint,
depth: number
): boolean {
// Filter out results which do not match the field of the constraint
candidates = candidates.filter((r) => r.field === item.field);

// Add the input constraint to the results (if it also matches the field)
if (criteria.field === item.field) {
candidates.push({ ...criteria, operator: "==" });
}
criteria.forEach((c) => {
if (c.field === item.field) candidates.push({ ...c, operator: "==" });
});

// Test that the constraint does not breach the results
// The test constraint needs to be compared against all the results
Expand Down
20 changes: 14 additions & 6 deletions src/services/rule-pilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ export class RulePilot {
* the possible range of input criteria which would be satisfied by the rule.
*
* @param rule The rule to evaluate.
* @param constraint The constraint to introspect against.
* @param criteria The criteria to introspect against.
* @param subjects The subjects to introspect for.
* @throws RuleError if the rule is invalid
* @throws RuleTypeError if the rule is not granular
*/
introspect<R = string>(
rule: Rule,
constraint: Omit<Constraint, "operator">,
criteria: Omit<Constraint, "operator"> | Omit<Constraint, "operator">[],
subjects: string[]
): IntrospectionResult<R>[] {
// Before we proceed with the rule, we should validate it.
Expand All @@ -109,7 +109,11 @@ export class RulePilot {
throw new RuleError(validationResult);
}

return this.#introspector.introspect<R>(rule, constraint, subjects);
return this.#introspector.introspect<R>(
rule,
Array.isArray(criteria) ? criteria : [criteria],
subjects
);
}

/**
Expand Down Expand Up @@ -157,17 +161,21 @@ export class RulePilot {
* the possible range of input criteria which would be satisfied by the rule.
*
* @param rule The rule to introspect.
* @param constraint The constraint to introspect against.
* @param criteria The criteria to introspect against.
* @param subjects The subjects to introspect for.
* @throws RuleError if the rule is invalid
* @throws RuleTypeError if the rule is not granular
*/
static introspect<R = string>(
rule: Rule,
constraint: Omit<Constraint, "operator">,
criteria: Omit<Constraint, "operator"> | Omit<Constraint, "operator">[],
subjects: string[]
): IntrospectionResult<R>[] {
return RulePilot.#rulePilot.introspect<R>(rule, constraint, subjects);
return RulePilot.#rulePilot.introspect<R>(
rule,
Array.isArray(criteria) ? criteria : [criteria],
subjects
);
}

/**
Expand Down

0 comments on commit a4b5d19

Please sign in to comment.