Skip to content

Commit 02236ac

Browse files
committed
Merge branch 'fix/extend-is-empty-operator' into 'master'
fix: validate empty objects as well See merge request auto-cloud/cloudgraph/sdk!56
2 parents bf86aba + 064601c commit 02236ac

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

src/rules-engine/operators/array.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,4 @@ export default {
44
notIn: (a, b) => (Array.isArray(b) ? b.indexOf(a) === -1 : false),
55
contains: (a, b) => (Array.isArray(a) ? a.indexOf(b) > -1 : false),
66
doesNotContain: (a, b) => (Array.isArray(a) ? a.indexOf(b) === -1 : false),
7-
isEmpty: (data, shouldBeEmpty) => {
8-
if (Array.isArray(data)) {
9-
// Verify empty/filled arrays
10-
const array = (data || []).length
11-
return shouldBeEmpty ? array === 0 : array > 0
12-
}
13-
14-
return false
15-
},
167
}

src/rules-engine/operators/common.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Common Operators
2+
export default {
3+
isEmpty: (data, shouldBeEmpty) => {
4+
if (Array.isArray(data)) {
5+
// Verify empty/filled arrays
6+
const array = (data || []).length
7+
return shouldBeEmpty ? array === 0 : array > 0
8+
}
9+
10+
// Verify empty/filled objects
11+
if (typeof data === 'object') {
12+
const hasKeys = Object.keys(data).length
13+
return shouldBeEmpty ? hasKeys === 0 : hasKeys > 0
14+
}
15+
16+
return false
17+
},
18+
}

src/rules-engine/operators/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { Operator } from '../types'
22

33
import arrayOperators from './array'
4+
import commonOperators from './common'
45
import comparisonOperators from './comparison'
56
import dateOperators from './date'
67
import regexOperators from './regex'
78

89
export default {
9-
...comparisonOperators,
1010
...arrayOperators,
11+
...comparisonOperators,
12+
...commonOperators,
1113
...dateOperators,
1214
...regexOperators,
1315
} as { [key: string]: Operator }

tests/operators/array.test.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
import ArrayOperators from '../../src/rules-engine/operators/array'
22

33
describe('Array Operators', () => {
4-
describe('isEmpty Operator', () => {
5-
test('Should pass given an empty array', () => {
6-
expect(ArrayOperators.isEmpty([], true)).toBeTruthy()
7-
})
8-
test('Should fail given an empty array', () => {
9-
expect(ArrayOperators.isEmpty([], false)).toBeFalsy()
10-
})
11-
test('Should pass given a filled array', () => {
12-
expect(ArrayOperators.isEmpty(['filled'], true)).toBeFalsy()
13-
})
14-
test('Should fail given a filled array', () => {
15-
expect(ArrayOperators.isEmpty(['filled'], false)).toBeTruthy()
16-
})
17-
})
18-
194
describe('In Operator', () => {
205
test('Should pass given a filled array with the searched element', () => {
216
expect(ArrayOperators.in('rule', ['rule'])).toBeTruthy()

tests/operators/common.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import CommonOperators from '../../src/rules-engine/operators/common'
2+
3+
describe('Common Operators', () => {
4+
describe('isEmpty Operator', () => {
5+
test('Should pass given an empty array', () => {
6+
expect(CommonOperators.isEmpty([], true)).toBeTruthy()
7+
})
8+
test('Should fail given an empty array', () => {
9+
expect(CommonOperators.isEmpty([], false)).toBeFalsy()
10+
})
11+
test('Should pass given a filled array', () => {
12+
expect(CommonOperators.isEmpty(['filled'], true)).toBeFalsy()
13+
})
14+
test('Should fail given a filled array', () => {
15+
expect(CommonOperators.isEmpty(['filled'], false)).toBeTruthy()
16+
})
17+
test('Should pass given a filled object', () => {
18+
expect(CommonOperators.isEmpty({ key: 'one' }, false)).toBeTruthy()
19+
})
20+
test('Should fail given a empty object', () => {
21+
expect(CommonOperators.isEmpty({}, false)).toBeFalsy()
22+
})
23+
test('Should pass given a empty object', () => {
24+
expect(CommonOperators.isEmpty({}, true)).toBeTruthy()
25+
})
26+
test('Should fail given a filled object', () => {
27+
expect(CommonOperators.isEmpty({ key: 'one' }, true)).toBeFalsy()
28+
})
29+
})
30+
})

0 commit comments

Comments
 (0)