Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit 82a90ee

Browse files
authored
fix(data): support parent matching for more than one parent per key (#43)
1 parent f0ed0ce commit 82a90ee

4 files changed

Lines changed: 16 additions & 17 deletions

File tree

src/scrubber.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface ScrubberConfig {
1010
* Populated at runtime from any config keys with dots, key is last component (after last dot) and array contains
1111
* preceeding components a.k.a "parents"
1212
*/
13-
splitFields?: StringMap<string[]>
13+
splitFields?: StringMap<string[][]>
1414

1515
// If false, Scrubber catches any exceptions that may occur when using scrubbers,
1616
// logs and ignore them. If true, exceptions are logged and raised

src/scrubber.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,12 @@ test('Support scrubbing based on parent', () => {
387387
})
388388

389389
test('Support scrubbing array based on parent', () => {
390-
const data = { nested: { encryption: [{ key: 'secret' }] } }
390+
const data = { nested: { encryption: [{ key: 'secret' }], second: [{ key: 'secret2' }] } }
391391
const scrubber = new Scrubber(configParentScrubbersMock())
392392
const result = scrubber.scrub(data)
393393

394394
expect(result).toEqual({
395-
nested: { encryption: [{ key: 'replaced' }] },
395+
nested: { encryption: [{ key: 'replaced' }], second: [{ key: 'replaced' }] },
396396
})
397397
})
398398

src/scrubber.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,13 @@ export class Scrubber {
7979
Object.keys(dataCopy).forEach(key => {
8080
let scrubberCurrentField = this.cfg.fields[key]
8181

82-
if (
83-
!scrubberCurrentField &&
84-
this.cfg.splitFields?.[key] &&
85-
parents &&
86-
this.arrayContainsInOrder(parents, this.cfg.splitFields[key])
87-
) {
88-
const splitFieldParentCfg: string[] = this.cfg.splitFields[key] || []
89-
90-
const recomposedKey = [...splitFieldParentCfg, key].join('.')
91-
scrubberCurrentField = this.cfg.fields[recomposedKey]
82+
if (!scrubberCurrentField && this.cfg.splitFields?.[key] && parents) {
83+
for (const splitFieldParentCfg of this.cfg.splitFields[key]!) {
84+
if (this.arrayContainsInOrder(parents, splitFieldParentCfg)) {
85+
const recomposedKey = [...splitFieldParentCfg, key].join('.')
86+
scrubberCurrentField = this.cfg.fields[recomposedKey]
87+
}
88+
}
9289
}
9390

9491
if (!scrubberCurrentField) {
@@ -183,14 +180,16 @@ export class Scrubber {
183180
})
184181
}
185182

186-
private splitFields(cfg: ScrubberConfig): StringMap<string[]> {
187-
const output: StringMap<string[]> = {}
183+
private splitFields(cfg: ScrubberConfig): StringMap<string[][]> {
184+
const output: StringMap<string[][]> = {}
188185
for (const field of Object.keys(cfg.fields)) {
189186
const splitField = field.split('.')
190187

191188
if (splitField.length > 1) {
192189
const key = splitField.pop()!
193-
output[key] = splitField
190+
// Support multiple keys with different parents
191+
output[key] ||= []
192+
output[key]!.push(splitField)
194193
}
195194
}
196195
return output

src/test/scrubber.mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function configParentScrubbersMock(): ScrubberConfig {
5757
replacement: 'replaced',
5858
},
5959
},
60-
'encryption.key': {
60+
'encryption.key, second.key': {
6161
scrubber: 'staticScrubber',
6262
params: {
6363
replacement: 'replaced',

0 commit comments

Comments
 (0)