Usage Guide

Complete guide to data redaction and anonymization

Last updated:

Usage Guide

Object Redaction

Basic Redaction

import { redact } from "@visulima/redact";

const user = {
  id: 123,
  name: "Alice",
  password: "secret123",
  apiKey: "sk_live_abc123"
};

const safe = redact(user, ["password", "apiKey"]);
console.log(safe);
// { id: 123, name: "Alice", password: "<PASSWORD>", apiKey: "<APIKEY>" }

Nested Properties

import { redact } from "@visulima/redact";

const data = {
  user: {
    profile: {
      email: "alice@example.com",
      ssn: "123-45-6789"
    },
    credentials: {
      password: "secret"
    }
  }
};

const safe = redact(data, [
  "user.profile.ssn",
  "user.credentials.password"
]);

Array Elements

import { redact } from "@visulima/redact";

const users = [
  { name: "Alice", password: "pass1" },
  { name: "Bob", password: "pass2" }
];

const safe = redact(users, ["*.password"]);

Text Anonymization

Automatic Detection

import { stringAnonymize } from "@visulima/redact";

const text = "Contact John Doe at john@example.com or 555-1234";
const result = stringAnonymize(text);
// "Contact <PERSON> at <EMAIL> or <PHONENUMBER>"

Select Categories

import { stringAnonymize } from "@visulima/redact";

const text = "John works at Acme Corp earning $50,000";

// Only anonymize people and money
const result = stringAnonymize(text, {
  include: ["Person", "Money"]
});
// "John works at Acme Corp earning <MONEY>"

Exclude Categories

import { stringAnonymize } from "@visulima/redact";

const text = "Email john@example.com about the project";

// Anonymize everything except emails
const result = stringAnonymize(text, {
  exclude: ["Email"]
});

Built-in Rules

Sensitive Data Patterns

The library includes 40+ detection patterns:

import { redact } from "@visulima/redact";

const data = {
  email: "user@example.com",      // Detected
  password: "myPassword123",      // Detected
  creditCard: "4532123456789010", // Detected
  ssn: "123-45-6789",            // Detected
  apiKey: "sk_live_abc123",      // Detected
  token: "Bearer eyJ...",        // Detected
  ip: "192.168.1.1",             // Detected
  phone: "+1-555-123-4567"       // Detected
};

// Auto-detect all sensitive fields
const safe = redact(data);

Financial Data

import { redact } from "@visulima/redact";

const payment = {
  cardNumber: "4532-1234-5678-9010",
  cvv: "123",
  routing: "021000021",
  account: "123456789"
};

const safe = redact(payment, ["cardNumber", "cvv", "routing", "account"]);

Custom Options

Custom Replacement

import { redact } from "@visulima/redact";

const safe = redact(data, ["password"], {
  replacement: "[REDACTED]"
});
// password: "[REDACTED]"

Case Sensitivity

import { redact } from "@visulima/redact";

const safe = redact(data, ["Password"], {
  caseSensitive: false
});
// Matches "password", "PASSWORD", "Password"

Real-World Examples

Express Middleware

import { redact } from "@visulima/redact";
import type { Request, Response, NextFunction } from "express";

function redactMiddleware(req: Request, res: Response, next: NextFunction) {
  // Redact request body
  if (req.body) {
    req.body = redact(req.body, ["password", "token", "apiKey"]);
  }

  // Redact response
  const originalJson = res.json.bind(res);
  res.json = (data: any) => {
    const safe = redact(data, ["password", "secret", "token"]);
    return originalJson(safe);
  };

  next();
}

app.use(redactMiddleware);

Logger Integration

import { redact } from "@visulima/redact";
import winston from "winston";

const logger = winston.createLogger({
  format: winston.format.combine(
    winston.format((info) => {
      return {
        ...info,
        ...redact(info, ["password", "token", "apiKey"])
      };
    })(),
    winston.format.json()
  )
});

Database Query Sanitization

import { redact } from "@visulima/redact";

class UserRepository {
  async findById(id: string) {
    const user = await this.db.users.findOne({ id });

    // Remove sensitive fields before returning
    return redact(user, [
      "password",
      "passwordHash",
      "resetToken",
      "twoFactorSecret"
    ]);
  }

  async logQuery(query: any) {
    // Redact sensitive data in logs
    const safe = redact(query, [
      "password",
      "token",
      "secret"
    ]);

    logger.debug("Query executed", safe);
  }
}

Next Steps

API Reference

Complete API documentation

Back to Overview

Return to overview

Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues