@traversable/schema
    Preparing search index...

    Module @traversable/schema-codec - v0.0.21


    ᯓ𝘁𝗿𝗮𝘃𝗲𝗿𝘀𝗮𝗯𝗹𝗲/𝗱𝗲𝗿𝗶𝘃𝗲-𝗰𝗼𝗱𝗲𝗰


    Re-use your schema to derive a bi-directional codec (with compiled codecs coming soon!)


    NPM Version   TypeScript   License   npm  
    Static Badge   Static Badge   Static Badge  
    TypeScript Playground   •   npm


    • Instructions: to install the .codec method 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)

    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 }

    Classes

    Codec

    Interfaces

    Extend
    pipe
    Pipe

    Type Aliases

    VERSION

    Variables

    VERSION

    Functions

    pipe