Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@dsegovia90/redacted

dsegovia90262MIT0.0.6TypeScript support: included

Explicit access to sensitive data. Wraps values in a Redacted class to protect them from being leaked into loggers (ie: console.log) and serializers. Works with zod.

redaction, sensitive data, logging, serialization, secrecy

readme

### GO FROM THIS ❌

console.log(userPrivateInfo)
{
  id: "1234567890",
  email: "user@example.com",
  name: "John Doe",
  address: "123 Main St",
  phone: "555-555-5555",
  public_thing: "public value"
}

### TO THIS ✅
console.log(userPrivateInfo)
{
  id: Redacted {},
  email: Redacted {},
  name: Redacted {},
  address: Redacted {},
  phone: Redacted {},
  public_thing: "public value",
  public_thing2: "public value2"
}

Inspiration

Heavily inspired by rust secrecy crate.

Usage

JavaScript

const userData = {
  privateData: Redacted("user@example.com")
}

console.log(userData.privateData) // outputs: Redacted {}

// Access private fields explicitly with:
const privateData = userData.privateData.exposeSecret();

TypeScript

interface UserData {
  privateData: Redacted<string>;
}

const userData = {
  privateData: Redacted("user@example.com")
}

console.log(userData.privateData) // outputs: Redacted {}

// Access private fields explicitly with:
const privateData = userData.privateData.exposeSecret();

With Zod

Note that fields that not meet the zod schema will fail on parse().

import { z } from "zod";
import { redactedZodV3 } from "@dsegovia/redacted";

const userSchema = z.object({
  id: redactedZodV3(z.string().uuid()),
  email: redactedZodV3(z.string().email()),
  name: redactedZodV3(z.string().min(2).max(100)),
  address: redactedZodV3(z.string().min(5).max(200)),
  phone: redactedZodV3(z.string().min(10).max(20)),
  public_thing: z.string().min(1).max(100),
  public_thing2: z.string().min(1).max(100),
});

const json = { // This can come from a file or API response
  id: "1234567890",
  email: "user@example.com",
  name: "John Doe",
  address: "123 Main St",
  phone: "555-555-5555",
  public_thing: "public value",
  public_thing2: "public value2"
}

const parsed = userSchema.parse(json);
parsed.id.exposeSecret(); // explicitly access the secret value

// or

const safeParsed = userSchema.safeParse(json);