@gune/json-sanitizer
v1.0.3
Published
Sanitizes json keys
Readme
Data Sanitizer
A TypeScript library for sanitizing sensitive data in JSON objects. Protect sensitive information like passwords, credit cards, SSNs, and API keys before logging or transmitting data.
Installation
npm install @gune/json-sanitizerFeatures
- 🔒 Deep sanitization of nested objects and arrays
- 🎭 Multiple sanitization strategies: mask or omit
- ⚙️ Configurable masking options
- 🔄 Circular reference handling
- 📦 TypeScript support with full type definitions
Quick Start
import { DataSanitizer } from "@gune/json-sanitizer";
const sanitizer = new DataSanitizer({
keys: ["password", "ssn", "credit_card"],
strategy: "mask",
maskOptions: {
visibleChars: 4,
position: "end",
},
});
const dirtyData = {
user: "Alice",
password: "superSecretPassword",
ssn: "123-456-7890",
};
const cleanData = sanitizer.sanitize(dirtyData);
console.log(cleanData);
// Output: { user: "Alice", password: "****************word", ssn: "************7890" }Strategies
Mask Strategy
The mask strategy replaces sensitive data with mask characters while optionally keeping a portion visible for reference.
const sanitizer = new DataSanitizer({
keys: ["credit_card", "apiKey"],
strategy: "mask",
maskOptions: {
visibleChars: 4,
maskChar: "*",
position: "end",
},
});Mask Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| visibleChars | number | 4 | Number of characters to keep visible |
| maskChar | string | "*" | The character used for masking |
| position | "start" | "end" | "start" | Which end of the string to keep visible |
Position Parameter
The position parameter controls which part of the sensitive data remains visible:
position: "start" - Keeps characters at the beginning visible
const sanitizer = new DataSanitizer({
keys: ["apiKey"],
strategy: "mask",
maskOptions: {
visibleChars: 7,
position: "start",
},
});
sanitizer.sanitize({ apiKey: "sk_live_abc123xyz" });
// Output: { apiKey: "sk_live**********" }position: "end" - Keeps characters at the end visible
const sanitizer = new DataSanitizer({
keys: ["credit_card"],
strategy: "mask",
maskOptions: {
visibleChars: 4,
position: "end",
},
});
sanitizer.sanitize({ credit_card: "4111-1111-1111-1111" });
// Output: { credit_card: "***************1111" }Omit Strategy
The omit strategy completely removes sensitive keys from the output.
const sanitizer = new DataSanitizer({
keys: ["password", "ssn", "apiKey"],
strategy: "omit", // Default strategy
});
const dirtyData = {
id: 101,
user: "Alice",
password: "superSecretPassword",
email: "[email protected]",
};
const cleanData = sanitizer.sanitize(dirtyData);
console.log(cleanData);
// Output: { id: 101, user: "Alice", email: "[email protected]" }Advanced Usage
Deep Nested Sanitization
The sanitizer automatically handles deeply nested objects and arrays:
const sanitizer = new DataSanitizer({
keys: ["credit_card", "ssn", "password"],
strategy: "mask",
maskOptions: {
visibleChars: 4,
position: "end",
},
});
const dirtyData = {
id: 101,
user: "Alice",
password: "superSecretPassword",
meta: {
apiKey: "sk_12345",
timestamp: new Date(),
nested: {
ssn: "123-456-7890",
credit_card: "4111-1111-1111-1111",
},
},
};
const cleanData = sanitizer.sanitize(dirtyData);
console.log(JSON.stringify(cleanData, null, 2));
/*
Output:
{
"id": 101,
"user": "Alice",
"password": "******************word",
"meta": {
"timestamp": "2026-01-31T...",
"nested": {
"ssn": "************7890",
"credit_card": "***************1111"
}
}
}
*/Dynamic Key Addition
Add sensitive keys to an existing sanitizer instance:
const sanitizer = new DataSanitizer({
keys: ["password"],
strategy: "omit",
});
// Later, add more keys
sanitizer.addKeys(["apiKey", "token", "secret"]);API Reference
DataSanitizer
Constructor
new DataSanitizer(config: SanitizerConfig)SanitizerConfig:
interface SanitizerConfig {
keys: string[]; // Array of sensitive key names to sanitize
strategy?: "omit" | "mask"; // Sanitization strategy (default: "omit")
maskOptions?: MaskOptions; // Options for mask strategy
}Methods
sanitize<T>(data: T): T
Sanitizes the provided data according to the configured strategy.
addKeys(keys: string[]): void
Dynamically adds additional sensitive keys to the sanitizer.
How to Run Tests
Install Jest
npm install --save-dev jest ts-jest @types/jestInitialize Jest Configuration
npx ts-jest config:initRun Tests
npm testLicense
MIT
Author
Buddika Gunawardena (BUDDGUN92)
