@traversable/schema-codec
v0.0.33
Published
<br /> <h1 align="center">แฏ๐๐ฟ๐ฎ๐๐ฒ๐ฟ๐๐ฎ๐ฏ๐น๐ฒ/๐๐ฐ๐ต๐ฒ๐บ๐ฎ-๐ฐ๐ผ๐ฑ๐ฒ๐ฐ</h1> <br />
Readme
Overview
- Instructions: to install the
.codecmethod on all schemas, all you need to do is import@traversable/schema-codec.- To create a covariant codec (similar to zod's
.transform), use.codec.pipe - To create a contravariant codec (similar to zod's
.preprocess), use.codec.extend(WIP)
- To create a covariant codec (similar to zod's
Example
Play with this example in the TypeScript playground.
import { t } from '@traversable/schema'
import '@traversable/schema-codec'
// ^^ this installs the `.pipe` and `.extend` methods on all schemas
let User = t
.object({ name: t.optional(t.string), createdAt: t.string }).codec // <-- notice we're pulling off the `.codec` property
.pipe((user) => ({ ...user, createdAt: new Date(user.createdAt) }))
.unpipe((user) => ({ ...user, createdAt: user.createdAt.toISOString() }))
let fromAPI = User.parse({ name: 'Bill Murray', createdAt: new Date().toISOString() })
// ^? let fromAPI: Error | { name?: string, createdAt: Date}
if (fromAPI instanceof Error) throw fromAPI
fromAPI
// ^? { name?: string, createdAt: Date }
let toAPI = User.encode(fromAPI)
// ^? let toAPI: { name?: string, createdAt: string }