Back to results
pyncz/zod-key-mapper(opens in a new tab)
Language: TypeScript
🗝️ Transform keys of zod object schemas
- stars
- 6
- watchers
- 1
- forks
- 0
- open issues
- 0
- Last updated
- 11/27/2023
Language: TypeScript
🗝️ Transform keys of zod object schemas
🗝️ Transform keys of zod object schemas
Just install it with your favourite package manager:
pnpm install @pyncz/zod-key-mapper
Let's say, you use a 3rd party API to fetch some data. You want to parse and transform it with zod for further usage, including some operations with keys, e.g. mapping keys from snake_case to camelCase, or even change some confusing field names into something more convenient.
@pyncz/zod-key-mapper provides a convenient way to transform keys:
import { z } from 'zod' import { mapped } from '@pyncz/zod-key-mapper' const schema = mapped(z.object({ snake_case_field: z.string(), foo: z.number(), bar: z.number(), }), { snake_case_field: 'weRespectCamelsHere' }) console.log(schema.parse({ snake_case_field: 'hey', foo: 69, bar: 420 })) // > { weRespectCamelsHere: 'hey', foo: 69, bar: 420 }
You can map multiple keys:
const schema = mapped(z.object({ key1: z.string(), key2: z.number(), key3: z.number(), }), { key1: 'key1mod', key2: 'key2mod' }) console.log(schema.parse({ key1: 'hey', key2: 69, key3: 420 })) // > { key1mod: 'hey', key2mod: 69, key3: 420 }
Or you can map the schema key by key:
const schema = mapped(mapped(z.object({ key1: z.string(), key2: z.number(), key3: z.number(), }), { key1: 'key1mod' }), { key2: 'key2mod' }) console.log(schema.parse({ key1: 'hey', key2: 69, key3: 420 })) // > { key1mod: 'hey', key2mod: 69, key3: 420 }
schema - your Zod typekeysMap - a readonly object with keys you want to rename as keys, and corresponding new keys as values."What if I want to add or remove keys, not just transform them?"
Well, you should use pick / omit methods zod provides out of the box, before mapping:
const schema = mapped(z.object({ key1: z.string(), key2: z.number(), key3: z.number(), }).pick({ key1: true }), { key1: 'key1mod' }) console.log(schema.parse({ key1: 'hey', key2: 69, key3: 420 })) // > { key1mod: 'hey' }