maskObject
Deep-walk an object and mask all string values containing PII.
Usage
import { createMasker } from '@pii-mask/core';
const masker = createMasker({ mode: 'mask' });
const { result, detections } = masker.maskObject({
user: {
name: 'Priya Sharma',
email: 'priya@example.com',
phone: '+919876543210',
address: {
street: '42 MG Road',
city: 'Mumbai',
},
},
});
console.log(JSON.parse(result));
// All PII values are masked; non-PII passes through unchangedParameters
| Parameter | Type | Required | Description |
|---|---|---|---|
input | Record<string, unknown> | Yes | The object to deep-walk and mask |
session | MaskSession | No | Session for cross-call consistency (shared tokens/labels) |
Returning an object instead of JSON
maskObject returns the masked value as a JSON string. Use mask() to get the masked object directly:
const { result } = masker.mask({ user: { email: 'priya@example.com' } });
result.user.email; // 'pr**a@example.com'What gets masked
maskObject recursively walks all nested objects and arrays:
- Strings run through every detector; numbers are masked when their key is sensitive (
pin,ssn,phone,zip…). Dates and objects withtoJSON()are masked in their JSON form;Errors become{ type, message, stack };Maps andSets become objects and arrays; BigInts are serialized as strings.- Credential containers such as
cookies: { … }are redacted whole. - An object referenced from two places is masked at both; a reference cycle becomes
'[Circular]'. - Objects deeper than
maxDepth(default 100) become'[Truncated]'.