npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@orchestrator/gen-io-ts

v3.0.1

Published

Generate io-ts codecs for classes by decorating props

Downloads

12,675

Readme

GenIoTs

Build Status Coverage Npm Npm Downloads Licence semantic-release

Generate io-ts codecs for classes by decorating props

Installation

$ npm install --save @orchestrator/gen-io-ts

NOTE: Library uses globalThis variable to get Reflect object. You can either provide it manually via provideReflect() function or make sure that Reflect API is loaded globally (via polyfills).

If required - you may need to load globalThis polyfill.

Usage

First decorate your properties that you want to validate:

// my-class.ts

import { Property } from '@orchestrator/gen-io-ts';

class MyClass {
  @Property() // This will validate `prop` to be a string
  prop: string;
  internalProp: any;
}

Now generate codec type for your decorated class:

// main.ts

import { genIoType } from '@orchestrator/gen-io-ts';

import { MyClass } from './my-class.ts';

const myClassType = genIoType(MyClass);

// Then you can validate the type
myClassType.decode({ prop: 'ok' }); // This will produce Right({prop: 'ok'})
myClassType.decode({ prop: false }); // This will produce Left([...])

You can now report any validation errors as usual in io-ts:

import { ThrowReporter } from 'io-ts/lib/ThrowReporter';

ThrowReporter.report(myClassType.decode({...})); // This will throw errors

NOTE: By default every property is optional. To change it - see Required property section.

Advanced usage

Required property

Every property is configured to be NOT required, which means you can assign null/undefined values and validation will not fail.

To change that set isRequired option to true:

import { Property } from '@orchestrator/gen-io-ts';

class MyClass {
  @Property({ isRequired: true }) // Will now fail if `null/undefined` was set
  prop: string;
}

Custom type

Allows to override default inferred type with custom:

import { Property } from '@orchestrator/gen-io-ts';

class MyClass {
  @Property({ type: typeOf(Boolean) }) // Will now allow only booleans instead of strings
  prop: string;
}

Custom type factories

Allows to override/adjust(refine) default type:

import * as t from 'io-ts';
import { Property } from '@orchestrator/gen-io-ts';

class MyClass {
  @Property({
    typeFactory: type => t.refine(type, str => str.length > 0, 'NotEmpty'),
  }) // Will now allow only non empty strings
  prop: string;
}

See: io-ts Refinements for more usage info.


This project was generated with Angular CLI version 7.1.4.

Development server

Run ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

Code scaffolding

Run ng generate component component-name to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module.

Build

Run ng build to build the project. The build artifacts will be stored in the dist/ directory. Use the --prod flag for a production build.

Running unit tests

Run ng test to execute the unit tests via Karma.

Running end-to-end tests

Run ng e2e to execute the end-to-end tests via Protractor.

Further help

To get more help on the Angular CLI use ng help or go check out the Angular CLI README.