-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added multiple filtering capabilities
- Loading branch information
1 parent
254de4a
commit 66a7246
Showing
9 changed files
with
241 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
x-pack/plugins/lists/common/schemas/types/empty_string_array.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
import { left } from 'fp-ts/lib/Either'; | ||
|
||
import { foldLeftRight, getPaths } from '../../siem_common_deps'; | ||
|
||
import { EmptyStringArray, EmptyStringArrayEncoded } from './empty_string_array'; | ||
|
||
describe('empty_string_array', () => { | ||
test('it should validate "null" and create an empty array', () => { | ||
const payload: EmptyStringArrayEncoded = null; | ||
const decoded = EmptyStringArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual([]); | ||
}); | ||
|
||
test('it should validate "undefined" and create an empty array', () => { | ||
const payload: EmptyStringArrayEncoded = undefined; | ||
const decoded = EmptyStringArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual([]); | ||
}); | ||
|
||
test('it should validate a single value of "a" into an array of size 1 of ["a"]', () => { | ||
const payload: EmptyStringArrayEncoded = 'a'; | ||
const decoded = EmptyStringArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(['a']); | ||
}); | ||
|
||
test('it should validate 2 values of "a,b" into an array of size 2 of ["a", "b"]', () => { | ||
const payload: EmptyStringArrayEncoded = 'a,b'; | ||
const decoded = EmptyStringArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(['a', 'b']); | ||
}); | ||
|
||
test('it should validate 3 values of "a,b,c" into an array of size 3 of ["a", "b", "c"]', () => { | ||
const payload: EmptyStringArrayEncoded = 'a,b,c'; | ||
const decoded = EmptyStringArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(['a', 'b', 'c']); | ||
}); | ||
|
||
test('it should NOT validate a number', () => { | ||
const payload: number = 5; | ||
const decoded = EmptyStringArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([ | ||
'Invalid value "5" supplied to "EmptyStringArray"', | ||
]); | ||
expect(message.schema).toEqual({}); | ||
}); | ||
|
||
test('it should validate 3 values of " a, b, c " into an array of size 3 of ["a", "b", "c"] even though they have spaces', () => { | ||
const payload: EmptyStringArrayEncoded = ' a, b, c '; | ||
const decoded = EmptyStringArray.decode(payload); | ||
const message = pipe(decoded, foldLeftRight); | ||
|
||
expect(getPaths(left(message.errors))).toEqual([]); | ||
expect(message.schema).toEqual(['a', 'b', 'c']); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
x-pack/plugins/lists/common/schemas/types/empty_string_array.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import * as t from 'io-ts'; | ||
import { Either } from 'fp-ts/lib/Either'; | ||
|
||
/** | ||
* Types the EmptyStringArray as: | ||
* - A value that can be undefined, or null (which will be turned into an empty array) | ||
* - A comma separated string that can turn into an array by splitting on it | ||
* - Example input converted to output: undefined -> [] | ||
* - Example input converted to output: null -> [] | ||
* - Example input converted to output: "a,b,c" -> ["a", "b", "c"] | ||
*/ | ||
export const EmptyStringArray = new t.Type<string[], string | undefined | null, unknown>( | ||
'EmptyStringArray', | ||
t.array(t.string).is, | ||
(input, context): Either<t.Errors, string[]> => { | ||
if (input == null) { | ||
return t.success([]); | ||
} else if (typeof input === 'string' && input.trim() !== '') { | ||
const arrayValues = input | ||
.trim() | ||
.split(',') | ||
.map((value) => value.trim()); | ||
const emptyValueFound = arrayValues.some((value) => value === ''); | ||
if (emptyValueFound) { | ||
return t.failure(input, context); | ||
} else { | ||
return t.success(arrayValues); | ||
} | ||
} else { | ||
return t.failure(input, context); | ||
} | ||
}, | ||
String | ||
); | ||
|
||
export type EmptyStringArrayC = typeof EmptyStringArray; | ||
|
||
export type EmptyStringArrayEncoded = t.OutputOf<typeof EmptyStringArray>; | ||
export type EmptyStringArrayDecoded = t.TypeOf<typeof EmptyStringArray>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
x-pack/plugins/lists/server/services/exception_lists/find_exception_list_items.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { LIST_ID } from '../../../common/constants.mock'; | ||
|
||
import { getExceptionListsItemFilter } from './find_exception_list_items'; | ||
|
||
describe('find_exception_list_items', () => { | ||
describe('getExceptionListsItemFilter', () => { | ||
test('It should create a filter with a single listId with an empty filter', () => { | ||
const filter = getExceptionListsItemFilter({ | ||
filter: [], | ||
listId: [LIST_ID], | ||
savedObjectType: ['exception-list'], | ||
}); | ||
expect(filter).toEqual( | ||
'(exception-list.attributes.list_type: item AND exception-list.attributes.list_id: some-list-id)' | ||
); | ||
}); | ||
|
||
test('It should create a filter with a single listId with a single filter', () => { | ||
const filter = getExceptionListsItemFilter({ | ||
filter: ['exception-list.attributes.name: "Sample Endpoint Exception List"'], | ||
listId: [LIST_ID], | ||
savedObjectType: ['exception-list'], | ||
}); | ||
expect(filter).toEqual( | ||
'((exception-list.attributes.list_type: item AND exception-list.attributes.list_id: some-list-id) AND exception-list.attributes.name: "Sample Endpoint Exception List")' | ||
); | ||
}); | ||
|
||
test('It should create a filter with 2 listIds and an empty filter', () => { | ||
const filter = getExceptionListsItemFilter({ | ||
filter: [], | ||
listId: ['list-1', 'list-2'], | ||
savedObjectType: ['exception-list', 'exception-list-agnostic'], | ||
}); | ||
expect(filter).toEqual( | ||
'(exception-list.attributes.list_type: item AND exception-list.attributes.list_id: list-1) OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-2)' | ||
); | ||
}); | ||
|
||
test('It should create a filter with 2 listIds and a single filter', () => { | ||
const filter = getExceptionListsItemFilter({ | ||
filter: ['exception-list.attributes.name: "Sample Endpoint Exception List"'], | ||
listId: ['list-1', 'list-2'], | ||
savedObjectType: ['exception-list', 'exception-list-agnostic'], | ||
}); | ||
expect(filter).toEqual( | ||
'((exception-list.attributes.list_type: item AND exception-list.attributes.list_id: list-1) AND exception-list.attributes.name: "Sample Endpoint Exception List") OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-2)' | ||
); | ||
}); | ||
|
||
test('It should create a filter with 3 listIds and an empty filter', () => { | ||
const filter = getExceptionListsItemFilter({ | ||
filter: [], | ||
listId: ['list-1', 'list-2', 'list-3'], | ||
savedObjectType: ['exception-list', 'exception-list-agnostic', 'exception-list-agnostic'], | ||
}); | ||
expect(filter).toEqual( | ||
'(exception-list.attributes.list_type: item AND exception-list.attributes.list_id: list-1) OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-2) OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-3)' | ||
); | ||
}); | ||
|
||
test('It should create a filter with 3 listIds and a single filter for the first item', () => { | ||
const filter = getExceptionListsItemFilter({ | ||
filter: ['exception-list.attributes.name: "Sample Endpoint Exception List"'], | ||
listId: ['list-1', 'list-2', 'list-3'], | ||
savedObjectType: ['exception-list', 'exception-list-agnostic', 'exception-list-agnostic'], | ||
}); | ||
expect(filter).toEqual( | ||
'((exception-list.attributes.list_type: item AND exception-list.attributes.list_id: list-1) AND exception-list.attributes.name: "Sample Endpoint Exception List") OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-2) OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-3)' | ||
); | ||
}); | ||
|
||
test('It should create a filter with 3 listIds and 3 filters for each', () => { | ||
const filter = getExceptionListsItemFilter({ | ||
filter: [ | ||
'exception-list.attributes.name: "Sample Endpoint Exception List 1"', | ||
'exception-list.attributes.name: "Sample Endpoint Exception List 2"', | ||
'exception-list.attributes.name: "Sample Endpoint Exception List 3"', | ||
], | ||
listId: ['list-1', 'list-2', 'list-3'], | ||
savedObjectType: ['exception-list', 'exception-list-agnostic', 'exception-list-agnostic'], | ||
}); | ||
expect(filter).toEqual( | ||
'((exception-list.attributes.list_type: item AND exception-list.attributes.list_id: list-1) AND exception-list.attributes.name: "Sample Endpoint Exception List 1") OR ((exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-2) AND exception-list.attributes.name: "Sample Endpoint Exception List 2") OR ((exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-3) AND exception-list.attributes.name: "Sample Endpoint Exception List 3")' | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters