Skip to content

Commit 632ba76

Browse files
author
Marco Franceschi
committed
feat: First approach to compare two fields in the same object
1 parent 9fb710e commit 632ba76

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/rules-engine/evaluators/json-evaluator.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '../types'
1313
import { RuleEvaluator } from './rule-evaluator'
1414
import AdditionalOperators from '../operators'
15+
import ComparisonOperators from '../operators/comparison'
1516

1617
export default class JsonEvaluator implements RuleEvaluator<JsonRule> {
1718
canEvaluate(rule: JsonRule): boolean {
@@ -124,14 +125,25 @@ export default class JsonEvaluator implements RuleEvaluator<JsonRule> {
124125
// remaining field should be the op name
125126
const op = Object.keys(condition)[0] //
126127
const operator = this.operators[op]
127-
const otherArgs = condition[op] // {[and]: xxx }
128+
let otherArgs = condition[op] // {[and]: xxx }
128129
if (!op || !operator) {
129130
throw new Error(`unrecognized operation${JSON.stringify(condition)}`)
130131
}
131132

132133
const data = { ..._data }
133134
let firstArg
134135

136+
if (
137+
Object.keys(ComparisonOperators).includes(operator.name) &&
138+
this.isCondition(otherArgs) &&
139+
otherArgs.path
140+
) {
141+
const otherData = { ..._data }
142+
const elementPath = this.calculatePath(otherData, otherArgs.path)
143+
otherData.elementPath = elementPath
144+
otherArgs = this.resolvePath(otherData, elementPath)
145+
}
146+
135147
if (path) {
136148
const elementPath = this.calculatePath(data, path)
137149
data.elementPath = elementPath

tests/json-evaluator.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,42 @@ describe('JsonEvaluator', () => {
421421

422422
expect(finding.result).toBe(Result.PASS)
423423
})
424+
425+
test('Should passed using the same value for the equal operator with the same path', async () => {
426+
const data = {
427+
data: { a: { b: [0, { e: 'same value', d: 'same value' }] } },
428+
}
429+
const rule = { path: 'a.b[1].d', equal: { path: 'a.b[1].e' } }
430+
431+
const finding = await evaluator.evaluateSingleResource(
432+
{
433+
conditions: rule,
434+
resource: {
435+
id: cuid(),
436+
},
437+
} as any,
438+
data
439+
)
440+
441+
expect(finding.result).toBe(Result.PASS)
442+
})
443+
444+
test('Should failed using two different values for the equal operator', async () => {
445+
const data = {
446+
data: { a: { b: [0, { e: 'not the same', d: 'value' }] } },
447+
}
448+
const rule = { path: 'a.b[1].d', equal: { path: 'a.b[1].e' } }
449+
450+
const finding = await evaluator.evaluateSingleResource(
451+
{
452+
conditions: rule,
453+
resource: {
454+
id: cuid(),
455+
},
456+
} as any,
457+
data
458+
)
459+
460+
expect(finding.result).toBe(Result.FAIL)
461+
})
424462
})

tests/operators/comparison.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import ComparisonOperators from '../../src/rules-engine/operators/comparison'
2+
3+
describe('Comparison Operators', () => {
4+
describe('Equal Operator', () => {
5+
test.todo('Should pass given two equal values')
6+
})
7+
})

0 commit comments

Comments
 (0)