Git Product home page Git Product logo

zio-ts's Introduction

zio-ts

A tiny abstraction to make Zod compatible with fp-ts.

Installation

npm install @valeriobelli/zio-ts fp-ts zod

Usage

This library exposes parse and parseAsync which wraps the Zod parse and parseAsync functions.

parse

The parse function takes a Zod schema and returns a function that takes an unknown value and returns an Either of the ZodError or the decoded value.

import * as R from 'fp-ts/Reader'
import * as RE from 'fp-ts/ReaderEither'
import { constant, pipe } from 'fp-ts/function'
import { parse } from 'zio-ts'
import { z } from 'zod'

const Env = z.object({
	ENVIRONMENT: z.string(),
	NODE_ENV: z.string(),
})

declare const getEnv: R.Reader<void, Record<string, unknown>>

const getDecodedEnv = pipe(
	getEnv, // R.Reader<void, Record<string, unknown>>
	R.map(parse(Env)), // R.ReaderEither<void, z.ZodError<Env>, Env>
	RE.getOrElse(() => constant({ ENVIRONMENT: 'production', NODE_ENV: 'production' }))), // R.Reader<void, Env>
)

parseAsync

The parseAsync function takes a Zod schema and returns a function that takes an unknown value and returns a TaskEither of the ZodError or the decoded value.

import * as RTE from 'fp-ts/ReaderTaskEither'
import { flow } from 'fp-ts/function'
import { parse } from 'zio-ts'
import { z } from 'zod'

const User = z.object({
	name: z.string(),
	age: z.number(),
})

type User = z.infer<typeof User>

declare const getUser: RTE.ReaderTaskEither<void, Error, unknown>

const getDecodedUser = pipe(
	getUser, // RTE.ReaderTaskEither<Error, unknown>
	RTE.flatMapTaskEither(parseAsync(User)), // RTE.ReaderTaskEither<void, Error | z.ZodError<User>, User>
) // RTE.ReaderTaskEither<void, Error | z.ZodError<User>, User>

zio-ts's People

Contributors

valeriobelli avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.