Ecosystem: zod-redact #6125
holoflash
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
In a project I maintain that handles a lot of PII and has very incomplete test data, presenting new features to stakeholders always means creating mock data and writing throw-away code to swap out real values just to be able to demo in production.
After getting frustrated with this, I built a small Zod plugin that lets you annotate schema fields with replacement values. The plugin adds a
parseAndRedact()version of eachparse()function variant (safe and async). The annotated fields are swapped out after validation and everything else works exactly like normal.I'm using this plugin by having a global "presentation mode" flag which is then used to conditionally choose which parsing path to take. In my schemas, I'm only annotating the sensitive personal information for redaction so that everything looks just like production in presentations but nothing sensitive is exposed.
Replacements can be static values, arrays to pick from, or functions:
Array picks and function calls are deterministic and the same input always produces the same output. This means that the same real data across different schemas will result in the same "fake data" providing continuity across different pages.
There's also a
combine()helper for composing realistic fake data from multiple arrays (building a full name and email from shared first/last name pools for example), keeping the picks consistent across fields.All reactions