-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '8.7' into backport/8.7/pr-152075
- Loading branch information
Showing
13 changed files
with
354 additions
and
27 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
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
74 changes: 74 additions & 0 deletions
74
...ion/server/lib/detection_engine/signals/source_fields_merging/utils/is_path_valid.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,74 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isPathValid } from './is_path_valid'; | ||
|
||
describe('isPathValid', () => { | ||
test('not valid when empty string and empty object', () => { | ||
expect(isPathValid('', {})).toEqual(false); | ||
}); | ||
|
||
test('valid when a path and empty object', () => { | ||
expect(isPathValid('a.b.c', {})).toEqual(true); | ||
}); | ||
|
||
test('not valid when a path and an array exists', () => { | ||
expect(isPathValid('a', { a: [] })).toEqual(false); | ||
}); | ||
|
||
test('not valid when a path and primitive value exists', () => { | ||
expect(isPathValid('a', { a: 'test' })).toEqual(false); | ||
expect(isPathValid('a', { a: 1 })).toEqual(false); | ||
expect(isPathValid('a', { a: true })).toEqual(false); | ||
}); | ||
|
||
test('valid when a path and object value exists', () => { | ||
expect(isPathValid('a', { a: {} })).toEqual(true); | ||
}); | ||
|
||
test('not valid when a path and an array exists within the parent path at level 1', () => { | ||
expect(isPathValid('a.b', { a: [] })).toEqual(false); | ||
}); | ||
|
||
test('not valid when a path and primitive value exists within the parent path at level 1', () => { | ||
expect(isPathValid('a.b', { a: 'test' })).toEqual(false); | ||
expect(isPathValid('a.b', { a: 1 })).toEqual(false); | ||
expect(isPathValid('a.b', { a: true })).toEqual(false); | ||
}); | ||
|
||
test('valid when a path and object value exists within the parent path at level 1', () => { | ||
expect(isPathValid('a.b', { a: {} })).toEqual(true); | ||
}); | ||
|
||
test('not valid when a path and an array exists within the parent path at level 2', () => { | ||
expect(isPathValid('a.b.c', { a: { b: [] } })).toEqual(false); | ||
}); | ||
|
||
test('not valid when a path and primitive value exists within the parent path at level 2', () => { | ||
expect(isPathValid('a.b', { a: { b: 'test' } })).toEqual(false); | ||
expect(isPathValid('a.b', { a: { b: 1 } })).toEqual(false); | ||
expect(isPathValid('a.b', { a: { b: true } })).toEqual(false); | ||
}); | ||
|
||
test('valid when a path and object value exists within the parent path at level 2', () => { | ||
expect(isPathValid('a.b', { a: { b: {} } })).toEqual(true); | ||
}); | ||
|
||
test('not valid when a path and an array exists within the parent path at level 3', () => { | ||
expect(isPathValid('a.b.c', { a: { b: { c: [] } } })).toEqual(false); | ||
}); | ||
|
||
test('not valid when a path and primitive value exists within the parent path at level 3', () => { | ||
expect(isPathValid('a.b.c', { a: { b: { c: 'test' } } })).toEqual(false); | ||
expect(isPathValid('a.b.c', { a: { b: { c: 1 } } })).toEqual(false); | ||
expect(isPathValid('a.b.c', { a: { b: { c: true } } })).toEqual(false); | ||
}); | ||
|
||
test('valid when a path and object value exists within the parent path at level 3', () => { | ||
expect(isPathValid('a.b.c', { a: { b: { c: {} } } })).toEqual(true); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
...solution/server/lib/detection_engine/signals/source_fields_merging/utils/is_path_valid.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,30 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { get, isPlainObject } from 'lodash/fp'; | ||
import type { SignalSource } from '../../types'; | ||
|
||
/** | ||
* Returns true if path in SignalSource object is valid | ||
* Path is valid if each field in hierarchy is object or undefined | ||
* Path is not valid if ANY of field in hierarchy is not object or undefined | ||
* @param path in source to check within source | ||
* @param source The source document | ||
* @returns boolean | ||
*/ | ||
export const isPathValid = (path: string, source: SignalSource): boolean => { | ||
if (!path) { | ||
return false; | ||
} | ||
const splitPath = path.split('.'); | ||
|
||
return splitPath.every((_, index, array) => { | ||
const newPath = [...array].splice(0, index + 1).join('.'); | ||
const valueToCheck = get(newPath, source); | ||
return valueToCheck === undefined || isPlainObject(valueToCheck); | ||
}); | ||
}; |
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
Oops, something went wrong.