Skip to content

Commit

Permalink
Rewrite type check for Filters component to a type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitaindik committed Sep 18, 2024
1 parent 3753f04 commit 7f44356
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,10 @@ export function getQueryLanguageLabel(language: string) {
}

/**
* Assigns type `Filter` to items that have a `meta` property. Removes any other items.
* Assigns type `Filter[]` to an array if every item in has a `meta` property.
*/
export function typeCheckFilters(filters: unknown[]): Filter[] {
return filters.filter((f) => {
if (typeof f === 'object' && f !== null && 'meta' in f) {
return true;
}

return false;
}) as Filter[];
export function isFilters(maybeFilters: unknown[]): maybeFilters is Filter[] {
return maybeFilters.every((f) => typeof f === 'object' && f !== null && 'meta' in f);
}

type DataSourceProps =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
} from '../../../../../../../../../common/api/detection_engine';
import * as descriptionStepI18n from '../../../../../../../rule_creation_ui/components/description_step/translations';
import { Query, Filters } from '../../../../rule_definition_section';
import { getDataSourceProps, typeCheckFilters } from '../../../../helpers';
import { getDataSourceProps, isFilters } from '../../../../helpers';

interface EqlQueryReadOnlyProps {
eqlQuery: RuleEqlQuery;
Expand All @@ -29,14 +29,12 @@ export function EqlQueryReadOnly({ eqlQuery, dataSource }: EqlQueryReadOnlyProps
},
];

const filters = typeCheckFilters(eqlQuery.filters);

if (filters.length > 0) {
if (isFilters(eqlQuery.filters) && eqlQuery.filters.length > 0) {
const dataSourceProps = getDataSourceProps(dataSource);

listItems.push({
title: descriptionStepI18n.FILTERS_LABEL,
description: <Filters filters={filters} {...dataSourceProps} />,
description: <Filters filters={eqlQuery.filters} {...dataSourceProps} />,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
import { Query, Filters } from '../../../../rule_definition_section';
import * as ruleDetailsI18n from '../../../../translations';
import * as descriptionStepI18n from '../../../../../../../rule_creation_ui/components/description_step/translations';
import { getDataSourceProps, getQueryLanguageLabel, typeCheckFilters } from '../../../../helpers';
import { getDataSourceProps, getQueryLanguageLabel, isFilters } from '../../../../helpers';

const defaultI18nLabels = {
query: descriptionStepI18n.QUERY_LABEL,
Expand Down Expand Up @@ -49,14 +49,12 @@ export function InlineKqlQueryReadOnly({
},
];

const filters = typeCheckFilters(kqlQuery.filters);

if (filters.length > 0) {
if (isFilters(kqlQuery.filters) && kqlQuery.filters.length > 0) {
const dataSourceProps = getDataSourceProps(dataSource);

listItems.push({
title: i18nLabels.filters,
description: <Filters filters={filters} {...dataSourceProps} />,
description: <Filters filters={kqlQuery.filters} {...dataSourceProps} />,
});
}

Expand Down

0 comments on commit 7f44356

Please sign in to comment.