Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pyncz/zod-key-mapper
🗝️ Transform keys of zod object schemas
https://github.com/pyncz/zod-key-mapper
keys schema typescript utils validation zod
Last synced: about 7 hours ago
JSON representation
🗝️ Transform keys of zod object schemas
- Host: GitHub
- URL: https://github.com/pyncz/zod-key-mapper
- Owner: pyncz
- License: mit
- Created: 2023-04-04T08:00:16.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-27T10:24:16.000Z (12 months ago)
- Last Synced: 2024-11-08T01:40:37.558Z (9 days ago)
- Topics: keys, schema, typescript, utils, validation, zod
- Language: TypeScript
- Homepage:
- Size: 76.2 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @pyncz/zod-key-mapper
🗝️ Transform keys of [zod](https://github.com/colinhacks/zod) object schemas
---
## Installation
Just install it with your favourite package manager:
```sh
pnpm install @pyncz/zod-key-mapper
```## Usage
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:
```ts
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:
```ts
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:
```ts
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 }
```#### Arguments
- `schema` - your Zod type
- `keysMap` - a readonly object with **keys you want to rename** as keys, and **corresponding new keys** as values.## What's up with other keys operations?
> "What if I want to add or remove keys, not just transform them?"
Well, you should use [pick / omit](https://zod.dev/?id=pickomit) methods zod provides out of the box, before mapping:
```ts
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' }
```