Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support overriding replaceWith for built in redactors #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const redactor = new AsyncRedactor({
});
```

### Disable specific built-in redaction rules
### Disable or override specific built-in redaction rules

```js
const redactor = new SyncRedactor({
Expand All @@ -151,7 +151,7 @@ const redactor = new SyncRedactor({
enabled: false
},
emailAddress: {
enabled: false
replaceWith: "[email protected]"
}
}
});
Expand Down
7 changes: 5 additions & 2 deletions src/composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ export function composeChildRedactors<T extends AsyncCustomRedactorConfig>(opts:
childRedactors.push(
new SimpleRegexpRedactor({
regexpPattern: (simpleRegexpBuiltIns as any)[regexpName],
replaceWith: opts.globalReplaceWith || snakeCase(regexpName).toUpperCase(),
replaceWith:
(opts.builtInRedactors as any)?.[regexpName]?.replaceWith ||
opts.globalReplaceWith ||
snakeCase(regexpName).toUpperCase(),
})
);
}
}

if (!opts.builtInRedactors || !opts.builtInRedactors.names || opts.builtInRedactors.names.enabled !== false) {
childRedactors.push(new NameRedactor(opts.globalReplaceWith));
childRedactors.push(new NameRedactor(opts.builtInRedactors?.names?.replaceWith || opts.globalReplaceWith));
}

if (opts.customRedactors && opts.customRedactors.after) {
Expand Down
11 changes: 11 additions & 0 deletions test/redactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ describe('index.js', function () {
expect(redactor.redact('I love cats, dogs, and cows')).toBe('I love ANIMAL, ANIMAL, and ANIMAL');
});

it('should accept custom replacements for built in redactors', function () {
let redactor = new SyncRedactor({
builtInRedactors: {
emailAddress: { replaceWith: '<REDACTED_EMAIL>' },
names: { replaceWith: '<REDACTED_NAME>' },
},
});
expect(redactor.redact('My email: [email protected].')).toBe('My email: <REDACTED_EMAIL>.');
expect(redactor.redact('My name is Joshua')).toBe('My name is <REDACTED_NAME>');
});

TestCase('should replace digits', [['codeA: 123, codeB: 6789', 'codeA: 123, codeB: DIGITS']]);

TestCase('should replace URLs', [
Expand Down