Skip to content

Commit cc621ef

Browse files
authored
[Security Solution][Exceptions] - Updates enum schema and tests (#76544) (#76588)
## Summary Mistmatch caught between the io-ts type and the corresponding typescript enum. Currently, io-ts does not have support for enums - as a workaround I had made a matching typescript enum type. Tests were added to try to ensure the two stayed in sync, but didn't do a straight up comparison of the two. Updated the tests to now error if the keys do not match.
1 parent d915d82 commit cc621ef

File tree

4 files changed

+10
-65
lines changed

4 files changed

+10
-65
lines changed

x-pack/plugins/lists/common/schemas/common/schemas.test.ts

Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
EsDataTypeRangeTerm,
1717
EsDataTypeSingle,
1818
EsDataTypeUnion,
19+
ExceptionListTypeEnum,
20+
OperatorEnum,
1921
Type,
2022
esDataTypeGeoPoint,
2123
esDataTypeGeoPointRange,
@@ -25,60 +27,10 @@ import {
2527
esDataTypeUnion,
2628
exceptionListType,
2729
operator,
28-
operator_type as operatorType,
2930
type,
3031
} from './schemas';
3132

3233
describe('Common schemas', () => {
33-
describe('operatorType', () => {
34-
test('it should validate for "match"', () => {
35-
const payload = 'match';
36-
const decoded = operatorType.decode(payload);
37-
const message = pipe(decoded, foldLeftRight);
38-
39-
expect(getPaths(left(message.errors))).toEqual([]);
40-
expect(message.schema).toEqual(payload);
41-
});
42-
43-
test('it should validate for "match_any"', () => {
44-
const payload = 'match_any';
45-
const decoded = operatorType.decode(payload);
46-
const message = pipe(decoded, foldLeftRight);
47-
48-
expect(getPaths(left(message.errors))).toEqual([]);
49-
expect(message.schema).toEqual(payload);
50-
});
51-
52-
test('it should validate for "list"', () => {
53-
const payload = 'list';
54-
const decoded = operatorType.decode(payload);
55-
const message = pipe(decoded, foldLeftRight);
56-
57-
expect(getPaths(left(message.errors))).toEqual([]);
58-
expect(message.schema).toEqual(payload);
59-
});
60-
61-
test('it should validate for "exists"', () => {
62-
const payload = 'exists';
63-
const decoded = operatorType.decode(payload);
64-
const message = pipe(decoded, foldLeftRight);
65-
66-
expect(getPaths(left(message.errors))).toEqual([]);
67-
expect(message.schema).toEqual(payload);
68-
});
69-
70-
test('it should contain 4 keys', () => {
71-
// Might seem like a weird test, but its meant to
72-
// ensure that if operatorType is updated, you
73-
// also update the OperatorTypeEnum, a workaround
74-
// for io-ts not yet supporting enums
75-
// https://github.com/gcanti/io-ts/issues/67
76-
const keys = Object.keys(operatorType.keys);
77-
78-
expect(keys.length).toEqual(4);
79-
});
80-
});
81-
8234
describe('operator', () => {
8335
test('it should validate for "included"', () => {
8436
const payload = 'included';
@@ -98,15 +50,16 @@ describe('Common schemas', () => {
9850
expect(message.schema).toEqual(payload);
9951
});
10052

101-
test('it should contain 2 keys', () => {
53+
test('it should contain same amount of keys as enum', () => {
10254
// Might seem like a weird test, but its meant to
10355
// ensure that if operator is updated, you
10456
// also update the operatorEnum, a workaround
10557
// for io-ts not yet supporting enums
10658
// https://github.com/gcanti/io-ts/issues/67
107-
const keys = Object.keys(operator.keys);
59+
const keys = Object.keys(operator.keys).sort().join(',').toLowerCase();
60+
const enumKeys = Object.keys(OperatorEnum).sort().join(',').toLowerCase();
10861

109-
expect(keys.length).toEqual(2);
62+
expect(keys).toEqual(enumKeys);
11063
});
11164
});
11265

@@ -129,15 +82,16 @@ describe('Common schemas', () => {
12982
expect(message.schema).toEqual(payload);
13083
});
13184

132-
test('it should contain 2 keys', () => {
85+
test('it should contain same amount of keys as enum', () => {
13386
// Might seem like a weird test, but its meant to
13487
// ensure that if exceptionListType is updated, you
13588
// also update the ExceptionListTypeEnum, a workaround
13689
// for io-ts not yet supporting enums
13790
// https://github.com/gcanti/io-ts/issues/67
138-
const keys = Object.keys(exceptionListType.keys);
91+
const keys = Object.keys(exceptionListType.keys).sort().join(',').toLowerCase();
92+
const enumKeys = Object.keys(ExceptionListTypeEnum).sort().join(',').toLowerCase();
13993

140-
expect(keys.length).toEqual(2);
94+
expect(keys).toEqual(enumKeys);
14195
});
14296
});
14397

x-pack/plugins/lists/common/schemas/common/schemas.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,6 @@ export enum OperatorEnum {
282282
EXCLUDED = 'excluded',
283283
}
284284

285-
export const operator_type = t.keyof({
286-
exists: null,
287-
list: null,
288-
match: null,
289-
match_any: null,
290-
});
291-
export type OperatorType = t.TypeOf<typeof operator_type>;
292285
export enum OperatorTypeEnum {
293286
NESTED = 'nested',
294287
MATCH = 'match',

x-pack/plugins/lists/common/shared_exports.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export {
2525
NamespaceType,
2626
Operator,
2727
OperatorEnum,
28-
OperatorType,
2928
OperatorTypeEnum,
3029
ExceptionListTypeEnum,
3130
comment,

x-pack/plugins/security_solution/common/shared_imports.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export {
2525
NamespaceType,
2626
Operator,
2727
OperatorEnum,
28-
OperatorType,
2928
OperatorTypeEnum,
3029
ExceptionListTypeEnum,
3130
exceptionListItemSchema,

0 commit comments

Comments
 (0)