Skip to content

Commit

Permalink
log an malformed filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Jul 17, 2023
1 parent 7d3d7d2 commit 6186c19
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/data/adHocFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,17 @@ describe('AdHocManager', () => {
]);
expect(val).toEqual(`SELECT foo.stuff FROM foo`);
});

it('log a malformed filter', () => {
const warn = jest.spyOn(console, "error");
const value = { key: 'foo.key', operator: '=', value: undefined }
const ahm = new AdHocFilter();
ahm.setTargetTableFromQuery('SELECT * FROM foo');
ahm.apply('SELECT foo.stuff FROM foo', [
// @ts-expect-error
value,
]);
expect(warn).toHaveBeenCalledTimes(1);
expect(warn).toHaveBeenCalledWith("Invalid adhoc filter will be ignored:", value)
});
});
10 changes: 8 additions & 2 deletions src/data/adHocFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ export class AdHocFilter {
}

const filters = adHocFilters
.filter(validate)
.filter((filter: AdHocVariableFilter) => {
const valid = isValid(filter);
if(!valid) {
console.error('Invalid adhoc filter will be ignored:', filter);
}
return valid;
})
.map((f, i) => {
const key = f.key.includes('.') ? f.key.split('.')[1] : f.key;
const value = isNaN(Number(f.value)) ? `\\'${f.value}\\'` : Number(f.value);
Expand All @@ -47,7 +53,7 @@ export class AdHocFilter {
}
}

function validate(filter: AdHocVariableFilter): boolean {
function isValid(filter: AdHocVariableFilter): boolean {
return filter.key !== undefined && filter.operator !== undefined && filter.value !== undefined;
}

Expand Down

0 comments on commit 6186c19

Please sign in to comment.