Skip to content

Commit e99718e

Browse files
committed
chore: improve invalid custom operators error message
1 parent dfb4e81 commit e99718e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/operators.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { CustomOperator, OperatorGroup } from './types'
2+
import { isArray } from './is'
23

34
// operator precedence from highest to lowest
45
export const operators: OperatorGroup[] = [
@@ -12,6 +13,11 @@ export const operators: OperatorGroup[] = [
1213
]
1314

1415
export function extendOperators(operators: OperatorGroup[], newOperators: CustomOperator[]) {
16+
// backward compatibility error with v4 where `operators` was an object
17+
if (!isArray(newOperators)) {
18+
throw new Error('Invalid custom operators')
19+
}
20+
1521
return newOperators.reduce(extendOperator, operators)
1622
}
1723

src/parse.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ describe('customization', () => {
5757
expect(parse('.score ~= 8', options)).toEqual(['aboutEq', ['get', 'score'], 8])
5858
expect(parse('.score == 8', options)).toEqual(['eq', ['get', 'score'], 8])
5959
})
60+
61+
test('should throw an error in case of an invalid custom operator', () => {
62+
const options: JSONQueryParseOptions = {
63+
// @ts-ignore
64+
operators: [{}]
65+
}
66+
67+
expect(() => parse('.score > 8', options)).toThrow('Invalid custom operator')
68+
})
69+
70+
test('should throw an error in case of an invalid custom operator (2)', () => {
71+
const options: JSONQueryParseOptions = {
72+
// @ts-ignore
73+
operators: {}
74+
}
75+
76+
expect(() => parse('.score > 8', options)).toThrow('Invalid custom operators')
77+
})
6078
})
6179

6280
describe('test-suite', () => {

0 commit comments

Comments
 (0)