Skip to content

Commit

Permalink
[SIEM] [Detection Engine] Remove unnecessary ts-ignores (#58689)
Browse files Browse the repository at this point in the history
* fixes from comments on previous PR

* replaces any with unknown, adds test to show how optional chaining of array items can result in undefined even though typescript says its not
  • Loading branch information
dhurley14 authored Feb 27, 2020
1 parent a8380cf commit a06cc31
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export const createFindRulesStatusRoute = (getClients: GetScopedClients): Hapi.S
searchFields: ['alertId'],
});
const accumulated = await acc;

// Array accessors can result in undefined but
// this is not represented in typescript for some reason,
// https://github.com/Microsoft/TypeScript/issues/11122
const currentStatus = convertToSnakeCase<IRuleStatusAttributes>(
lastFiveErrorsForId.saved_objects[0]?.attributes
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import Boom from 'boom';

import { SavedObjectsFindResponse } from 'kibana/server';
import { IRuleSavedAttributesSavedObjectAttributes, IRuleStatusAttributes } from '../rules/types';
import {
transformError,
transformBulkError,
Expand Down Expand Up @@ -323,5 +325,19 @@ describe('utils', () => {
const values = {};
expect(convertToSnakeCase(values)).toEqual({});
});
it('returns null when passed in undefined', () => {
// Array accessors can result in undefined but
// this is not represented in typescript for some reason,
// https://github.com/Microsoft/TypeScript/issues/11122
const values: SavedObjectsFindResponse<IRuleSavedAttributesSavedObjectAttributes> = {
page: 0,
per_page: 5,
total: 0,
saved_objects: [],
};
expect(
convertToSnakeCase<IRuleStatusAttributes>(values.saved_objects[0]?.attributes) // this is undefined, but it says it's not
).toEqual(null);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,9 @@ export const getIndex = (getSpaceId: () => string, config: LegacyServices['confi
return `${signalsIndex}-${spaceId}`;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const convertToSnakeCase = <T extends Record<string, any>>(obj: T): Partial<T> | null => {
export const convertToSnakeCase = <T extends Record<string, unknown>>(
obj: T
): Partial<T> | null => {
if (!obj) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ describe('read_rules', () => {
});
test('should return null if saved object found by alerts client given id is not alert type', async () => {
const alertsClient = alertsClientMock.create();
const { alertTypeId, ...rest } = getResult();
// @ts-ignore
alertsClient.get.mockImplementation(() => rest);
const result = getResult();
delete result.alertTypeId;
alertsClient.get.mockResolvedValue(result);

const rule = await readRules({
alertsClient,
Expand Down Expand Up @@ -109,8 +109,7 @@ describe('read_rules', () => {
test('should return null if the output from alertsClient with ruleId set is empty', async () => {
const alertsClient = alertsClientMock.create();
alertsClient.get.mockResolvedValue(getResult());
// @ts-ignore
alertsClient.find.mockResolvedValue({ data: [] });
alertsClient.find.mockResolvedValue({ data: [], page: 0, perPage: 1, total: 0 });

const rule = await readRules({
alertsClient,
Expand Down

0 comments on commit a06cc31

Please sign in to comment.