Skip to content

Commit

Permalink
fix(data): support parent matching for more than one parent per key (#43
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kribor authored Oct 10, 2023
1 parent f0ed0ce commit 82a90ee
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/scrubber.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ScrubberConfig {
* Populated at runtime from any config keys with dots, key is last component (after last dot) and array contains
* preceeding components a.k.a "parents"
*/
splitFields?: StringMap<string[]>
splitFields?: StringMap<string[][]>

// If false, Scrubber catches any exceptions that may occur when using scrubbers,
// logs and ignore them. If true, exceptions are logged and raised
Expand Down
4 changes: 2 additions & 2 deletions src/scrubber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,12 @@ test('Support scrubbing based on parent', () => {
})

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

expect(result).toEqual({
nested: { encryption: [{ key: 'replaced' }] },
nested: { encryption: [{ key: 'replaced' }], second: [{ key: 'replaced' }] },
})
})

Expand Down
25 changes: 12 additions & 13 deletions src/scrubber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,13 @@ export class Scrubber {
Object.keys(dataCopy).forEach(key => {
let scrubberCurrentField = this.cfg.fields[key]

if (
!scrubberCurrentField &&
this.cfg.splitFields?.[key] &&
parents &&
this.arrayContainsInOrder(parents, this.cfg.splitFields[key])
) {
const splitFieldParentCfg: string[] = this.cfg.splitFields[key] || []

const recomposedKey = [...splitFieldParentCfg, key].join('.')
scrubberCurrentField = this.cfg.fields[recomposedKey]
if (!scrubberCurrentField && this.cfg.splitFields?.[key] && parents) {
for (const splitFieldParentCfg of this.cfg.splitFields[key]!) {
if (this.arrayContainsInOrder(parents, splitFieldParentCfg)) {
const recomposedKey = [...splitFieldParentCfg, key].join('.')
scrubberCurrentField = this.cfg.fields[recomposedKey]
}
}
}

if (!scrubberCurrentField) {
Expand Down Expand Up @@ -183,14 +180,16 @@ export class Scrubber {
})
}

private splitFields(cfg: ScrubberConfig): StringMap<string[]> {
const output: StringMap<string[]> = {}
private splitFields(cfg: ScrubberConfig): StringMap<string[][]> {
const output: StringMap<string[][]> = {}
for (const field of Object.keys(cfg.fields)) {
const splitField = field.split('.')

if (splitField.length > 1) {
const key = splitField.pop()!
output[key] = splitField
// Support multiple keys with different parents
output[key] ||= []
output[key]!.push(splitField)
}
}
return output
Expand Down
2 changes: 1 addition & 1 deletion src/test/scrubber.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function configParentScrubbersMock(): ScrubberConfig {
replacement: 'replaced',
},
},
'encryption.key': {
'encryption.key, second.key': {
scrubber: 'staticScrubber',
params: {
replacement: 'replaced',
Expand Down

0 comments on commit 82a90ee

Please sign in to comment.