Skip to content

Commit 7a11e2f

Browse files
[Security Solution][Detection Engine] Remove RuleTypeSchema in favor of Type for TypeScript (#76586)
## Summary Removes RuleTypeSchema in favor of Type for TypeScript. Does break out one function called `parseScheduleDates` into its own file to remove a circular ref issue.
1 parent a34b0a2 commit 7a11e2f

File tree

20 files changed

+77
-80
lines changed

20 files changed

+77
-80
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import moment from 'moment';
8+
import dateMath from '@elastic/datemath';
9+
10+
export const parseScheduleDates = (time: string): moment.Moment | null => {
11+
const isValidDateString = !isNaN(Date.parse(time));
12+
const isValidInput = isValidDateString || time.trim().startsWith('now');
13+
const formattedDate = isValidDateString
14+
? moment(time)
15+
: isValidInput
16+
? dateMath.parse(time)
17+
: null;
18+
19+
return formattedDate ?? null;
20+
};

x-pack/plugins/security_solution/common/detection_engine/schemas/common/schemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { UUID } from '../types/uuid';
1414
import { IsoDateString } from '../types/iso_date_string';
1515
import { PositiveIntegerGreaterThanZero } from '../types/positive_integer_greater_than_zero';
1616
import { PositiveInteger } from '../types/positive_integer';
17-
import { parseScheduleDates } from '../../utils';
17+
import { parseScheduleDates } from '../../parse_schedule_dates';
1818

1919
export const author = t.array(t.string);
2020
export type Author = t.TypeOf<typeof author>;

x-pack/plugins/security_solution/common/detection_engine/types.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,8 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6-
import * as t from 'io-ts';
7-
86
import { AlertAction } from '../../../alerts/common';
97

108
export type RuleAlertAction = Omit<AlertAction, 'actionTypeId'> & {
119
action_type_id: string;
1210
};
13-
14-
export const RuleTypeSchema = t.keyof({
15-
query: null,
16-
saved_query: null,
17-
machine_learning: null,
18-
threshold: null,
19-
});
20-
export type RuleType = t.TypeOf<typeof RuleTypeSchema>;

x-pack/plugins/security_solution/common/detection_engine/utils.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import moment from 'moment';
8-
import dateMath from '@elastic/datemath';
9-
107
import { EntriesArray } from '../shared_imports';
11-
import { RuleType } from './types';
8+
import { Type } from './schemas/common/schemas';
129

1310
export const hasLargeValueList = (entries: EntriesArray): boolean => {
1411
const found = entries.filter(({ type }) => type === 'list');
@@ -20,16 +17,4 @@ export const hasNestedEntry = (entries: EntriesArray): boolean => {
2017
return found.length > 0;
2118
};
2219

23-
export const isThresholdRule = (ruleType: RuleType) => ruleType === 'threshold';
24-
25-
export const parseScheduleDates = (time: string): moment.Moment | null => {
26-
const isValidDateString = !isNaN(Date.parse(time));
27-
const isValidInput = isValidDateString || time.trim().startsWith('now');
28-
const formattedDate = isValidDateString
29-
? moment(time)
30-
: isValidInput
31-
? dateMath.parse(time)
32-
: null;
33-
34-
return formattedDate ?? null;
35-
};
20+
export const isThresholdRule = (ruleType: Type) => ruleType === 'threshold';

x-pack/plugins/security_solution/common/machine_learning/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { RuleType } from '../detection_engine/types';
7+
import { Type } from '../detection_engine/schemas/common/schemas';
88

99
// Based on ML Job/Datafeed States from x-pack/legacy/plugins/ml/common/constants/states.js
1010
const enabledStates = ['started', 'opened'];
@@ -23,4 +23,4 @@ export const isJobFailed = (jobState: string, datafeedState: string): boolean =>
2323
return failureStates.includes(jobState) || failureStates.includes(datafeedState);
2424
};
2525

26-
export const isMlRule = (ruleType: RuleType) => ruleType === 'machine_learning';
26+
export const isMlRule = (ruleType: Type) => ruleType === 'machine_learning';

x-pack/plugins/security_solution/public/detections/components/alerts_table/timeline_actions/alert_context_menu.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ import styled from 'styled-components';
1717

1818
import { TimelineId } from '../../../../../common/types/timeline';
1919
import { DEFAULT_INDEX_PATTERN } from '../../../../../common/constants';
20-
import { Status } from '../../../../../common/detection_engine/schemas/common/schemas';
20+
import { Status, Type } from '../../../../../common/detection_engine/schemas/common/schemas';
2121
import { isThresholdRule } from '../../../../../common/detection_engine/utils';
22-
import { RuleType } from '../../../../../common/detection_engine/types';
2322
import { isMlRule } from '../../../../../common/machine_learning/helpers';
2423
import { timelineActions } from '../../../../timelines/store/timeline';
2524
import { EventsTd, EventsTdContent } from '../../../../timelines/components/timeline/styles';
@@ -409,7 +408,7 @@ const AlertContextMenuComponent: React.FC<AlertContextMenuProps> = ({
409408
data: nonEcsRowData,
410409
fieldName: 'signal.rule.type',
411410
});
412-
const [ruleType] = ruleTypes as RuleType[];
411+
const [ruleType] = ruleTypes as Type[];
413412
return !isMlRule(ruleType) && !isThresholdRule(ruleType);
414413
}, [nonEcsRowData]);
415414

x-pack/plugins/security_solution/public/detections/components/rules/description_step/helpers.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import styled from 'styled-components';
2424
import { assertUnreachable } from '../../../../../common/utility_types';
2525
import * as i18nSeverity from '../severity_mapping/translations';
2626
import * as i18nRiskScore from '../risk_score_mapping/translations';
27-
import { Threshold } from '../../../../../common/detection_engine/schemas/common/schemas';
28-
import { RuleType } from '../../../../../common/detection_engine/types';
27+
import { Threshold, Type } from '../../../../../common/detection_engine/schemas/common/schemas';
2928
import { esFilters } from '../../../../../../../../src/plugins/data/public';
3029

3130
import { tacticsOptions, techniquesOptions } from '../../../mitre/mitre_tactics_techniques';
@@ -357,7 +356,7 @@ export const buildNoteDescription = (label: string, note: string): ListItems[] =
357356
return [];
358357
};
359358

360-
export const buildRuleTypeDescription = (label: string, ruleType: RuleType): ListItems[] => {
359+
export const buildRuleTypeDescription = (label: string, ruleType: Type): ListItems[] => {
361360
switch (ruleType) {
362361
case 'machine_learning': {
363362
return [

x-pack/plugins/security_solution/public/detections/components/rules/description_step/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { isEmpty, chunk, get, pick, isNumber } from 'lodash/fp';
99
import React, { memo, useState } from 'react';
1010
import styled from 'styled-components';
1111

12-
import { RuleType } from '../../../../../common/detection_engine/types';
1312
import {
1413
IIndexPattern,
1514
Filter,
@@ -42,6 +41,7 @@ import { useSecurityJobs } from '../../../../common/components/ml_popover/hooks/
4241
import { buildMlJobDescription } from './ml_job_description';
4342
import { buildActionsDescription } from './actions_description';
4443
import { buildThrottleDescription } from './throttle_description';
44+
import { Type } from '../../../../../common/detection_engine/schemas/common/schemas';
4545

4646
const DescriptionListContainer = styled(EuiDescriptionList)`
4747
&.euiDescriptionList--column .euiDescriptionList__title {
@@ -211,7 +211,7 @@ export const getDescriptionItem = (
211211
const val: string = get(field, data);
212212
return buildNoteDescription(label, val);
213213
} else if (field === 'ruleType') {
214-
const ruleType: RuleType = get(field, data);
214+
const ruleType: Type = get(field, data);
215215
return buildRuleTypeDescription(label, ruleType);
216216
} else if (field === 'kibanaSiemAppUrl') {
217217
return [];

x-pack/plugins/security_solution/public/detections/components/rules/select_rule_type/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import { EuiCard, EuiFlexGrid, EuiFlexItem, EuiFormRow, EuiIcon } from '@elastic
99

1010
import { isMlRule } from '../../../../../common/machine_learning/helpers';
1111
import { isThresholdRule } from '../../../../../common/detection_engine/utils';
12-
import { RuleType } from '../../../../../common/detection_engine/types';
1312
import { FieldHook } from '../../../../shared_imports';
1413
import { useKibana } from '../../../../common/lib/kibana';
1514
import * as i18n from './translations';
1615
import { MlCardDescription } from './ml_card_description';
16+
import { Type } from '../../../../../common/detection_engine/schemas/common/schemas';
1717

1818
interface SelectRuleTypeProps {
1919
describedByIds?: string[];
@@ -30,9 +30,9 @@ export const SelectRuleType: React.FC<SelectRuleTypeProps> = ({
3030
hasValidLicense = false,
3131
isMlAdmin = false,
3232
}) => {
33-
const ruleType = field.value as RuleType;
33+
const ruleType = field.value as Type;
3434
const setType = useCallback(
35-
(type: RuleType) => {
35+
(type: Type) => {
3636
field.setValue(type);
3737
},
3838
[field]

x-pack/plugins/security_solution/public/detections/containers/detection_engine/rules/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import * as t from 'io-ts';
88

9-
import { RuleTypeSchema } from '../../../../../common/detection_engine/types';
109
import {
1110
author,
1211
building_block_type,
@@ -16,6 +15,7 @@ import {
1615
severity_mapping,
1716
timestamp_override,
1817
threshold,
18+
type,
1919
} from '../../../../../common/detection_engine/schemas/common/schemas';
2020
import {
2121
listArray,
@@ -44,7 +44,7 @@ export const NewRuleSchema = t.intersection([
4444
name: t.string,
4545
risk_score: t.number,
4646
severity: t.string,
47-
type: RuleTypeSchema,
47+
type,
4848
}),
4949
t.partial({
5050
actions: t.array(action),
@@ -117,7 +117,7 @@ export const RuleSchema = t.intersection([
117117
severity: t.string,
118118
severity_mapping,
119119
tags: t.array(t.string),
120-
type: RuleTypeSchema,
120+
type,
121121
to: t.string,
122122
threat: t.array(t.unknown),
123123
updated_at: t.string,

0 commit comments

Comments
 (0)