Skip to content

Keeping sensitive data hidden, and expose it only explicitly.

License

Notifications You must be signed in to change notification settings

dsegovia90/redacted

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Version Version

### 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);

About

Keeping sensitive data hidden, and expose it only explicitly.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published