Code Monkey home page Code Monkey logo

io-ts-builder's Introduction

CircleCI code style: prettier

io-ts-builder

Builder implementation for use with io-ts (https://github.com/gcanti/io-ts). Lets you create valid objects (optionally starting from a template object) using a fluent API.

For the same without io-ts, see https://www.npmjs.com/package/builder-pattern.

Usage

Basic usage

import * as t from 'io-ts'
import { IoTsBuilder } from 'io-ts-builder'

const Order = t.type({
  description: t.string,
  id: t.number
})

type IOrder = t.TypeOf<typeof Order>

const order: IOrder = IoTsBuilder(Order)
  .id(4)
  .description('foo')
  .build()

Actually, the type can be inferred:

const order = IoTsBuilder(Order)
  .id(4)
  .description('foo')
  .build()

When trying to build an invalid object, e.g.

const order = IoTsBuilder(Order)
  .id(4)
  .build()

the call to build() will throw an error using the ThrowReporter from io-ts.

Usage with template objects

You can also specify a template object, which allows easy creation of variation of objects. This is especially useful for making test data setup more readable:

const defaultOrder: IOrder = {
  id: 1,
  description: 'Default order'
};

const modifiedOrder = IoTsBuilder(defaultOrder)
  .id(2)
  .build();

Contributing

PRs are welcome!

Credits

Based on builder-pattern (https://www.npmjs.com/package/builder-pattern) by Vincent Pang.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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.