io-ts
TypeScript compatible runtime type system for IO validation
Last updated 7 years ago by gcanti .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install io-ts 
SYNC missed versions from official npm registry.

build status dependency status npm downloads

The idea

A value of type Type<S, A> (called "runtime type") is the runtime representation of the static type A.

Also a runtime type can

  • decode inputs (through validate)
  • encode outputs (through serialize)
  • be used as a custom type guard (through is)
export type mixed = object | number | string | boolean | symbol | undefined | null

class Type<S, A> {
  readonly _A: A
  readonly _S: S
  constructor(
    /** a unique name for this runtime type */
    readonly name: string,
    /** a custom type guard */
    readonly is: (v: mixed) => v is A,
    /** succeeds if a value of type S can be decoded to a value of type A */
    readonly validate: (input: S, context: Context) => Either<Errors, A>,
    /** converts a value of type A to a value of type S */
    readonly serialize: (output: A) => S
  ) {}
}

Note. The Either type is defined in fp-ts, a library containing implementations of common algebraic types in TypeScript.

Example

A runtime type representing string can be defined as

import * as t from 'io-ts'

export class StringType extends Type<mixed, string> {
  constructor() {
    super(
      'string',
      (v): v is string => typeof v === 'string',
      (s, c) => (this.is(s) ? success(s) : failure(s, c)),
      a => a
    )
  }
}

A runtime type can be used to validate an object in memory (for example an API payload)

const Person = t.interface({
  name: t.string,
  age: t.number
})

// ok
t.validate(JSON.parse('{"name":"Giulio","age":43}'), Person) // => Right({name: "Giulio", age: 43})

// ko
t.validate(JSON.parse('{"name":"Giulio"}'), Person) // => Left([...])

Error reporters

A reporter implements the following interface

interface Reporter<A> {
  report: (validation: Validation<any>) => A;
}

This package exports two default reporters

  • PathReporter: Reporter<Array<string>>
  • ThrowReporter: Reporter<void>

Example

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

const validation = t.validate({ name: 'Giulio' }, Person)

console.log(PathReporter.report(validation))
// => ['Invalid value undefined supplied to : { name: string, age: number }/age: number']

ThrowReporter.report(validation)
// => throws 'Invalid value undefined supplied to : { name: string, age: number }/age: number'

Community

  • io-ts-types - A collection of runtime types and combinators for use with io-ts
  • io-ts-reporters - Error reporters for io-ts
  • geojson-iots - Runtime types for GeoJSON as defined in rfc7946 made with io-ts

TypeScript integration

Runtime types can be inspected

instrospection

This library uses TypeScript extensively. Its API is defined in a way which automatically infers types for produced values

inference

Note that the type annotation isn't needed, TypeScript infers the type automatically based on a schema.

Static types can be extracted from runtime types with the TypeOf operator

type IPerson = t.TypeOf<typeof Person>

// same as
type IPerson = {
  name: string,
  age: number
}

Recursive types

Recursive types can't be inferred by TypeScript so you must provide the static type as a hint

// helper type
type ICategory = {
  name: string,
  categories: Array<ICategory>
}

const Category =
  t.recursion <
  ICategory >
  ('Category',
  self =>
    t.interface({
      name: t.string,
      categories: t.array(self)
    }))

Implemented types / combinators

import * as t from 'io-ts'
Type TypeScript Flow Runtime type / combinator
null null null t.null or t.nullType
undefined undefined void t.undefined
string string string t.string
number number number t.number
boolean boolean boolean t.boolean
any any any t.any
never never empty t.never
object object t.object
integer t.Integer
array of any Array<mixed> Array<mixed> t.Array
array of type Array<A> Array<A> t.array(A)
dictionary of any { [key: string]: mixed } { [key: string]: mixed } t.Dictionary
dictionary of type { [K in A]: B } { [key: A]: B } t.dictionary(A, B)
function Function Function t.Function
literal 's' 's' t.literal('s')
partial Partial<{ name: string }> $Shape<{ name: string }> t.partial({ name: t.string })
readonly Readonly<T> ReadOnly<T> t.readonly(T)
readonly array ReadonlyArray<number> ReadOnlyArray<number> t.readonlyArray(t.number)
interface interface A { name: string } interface A { name: string } t.interface({ name: t.string }) or t.type({ name: t.string })
interface inheritance interface B extends A {} interface B extends A {} t.intersection([ A, t.interface({}) ])
tuple [ A, B ] [ A, B ] t.tuple([ A, B ])
union A \| B A \| B t.union([ A, B ]) or t.taggedUnion(tag, [ A, B ])
intersection A & B A & B t.intersection([ A, B ])
keyof keyof M $Keys<M> t.keyof(M)
recursive types see Recursive types see Recursive types t.recursion(name, definition)
refinement t.refinement(A, predicate)
strict/exact types $Exact<{{ name: t.string }}> t.strict({ name: t.string })

Tagged unions

If you are encoding tagged unions, instead of the general purpose union combinator, you may want to use the taggedUnion combinator in order to get better performances

const A = t.type({
  tag: t.literal('A'),
  foo: t.string
})

const B = t.type({
  tag: t.literal('B'),
  bar: t.number
})

// the actual presence of the tag is statically checked
const U = t.taggedUnion('tag', [A, B])

Refinements

You can refine a type (any type) using the refinement combinator

const Positive = t.refinement(t.number, n => n >= 0, 'Positive')

const Adult = t.refinement(Person, person => person.age >= 18, 'Adult')

Strict/Exact interfaces

You can make an interface strict (which means that only the given properties are allowed) using the strict combinator

const Person = t.interface({
  name: t.string,
  age: t.number
})

const StrictPerson = t.strict(Person.props)

t.validate({ name: 'Giulio', age: 43, surname: 'Canti' }, Person) // ok
t.validate({ name: 'Giulio', age: 43, surname: 'Canti' }, StrictPerson) // fails

Mixing required and optional props

Note. You can mix required and optional props using an intersection

const A = t.interface({
  foo: t.string
})

const B = t.partial({
  bar: t.number
})

const C = t.intersection([A, B])

type CT = t.TypeOf<typeof C>

// same as
type CT = {
  foo: string
  bar?: number
}

You can define a custom combinator to avoid the boilerplate

export function interfaceWithOptionals<R extends t.Props, O extends t.Props>(
  required: R,
  optional: O,
  name?: string
): t.IntersectionType<
  [t.InterfaceType<R, t.InterfaceOf<R>>, t.PartialType<O, t.PartialOf<O>>],
  t.InterfaceOf<R> & t.PartialOf<O>
> {
  return t.intersection([t.interface(required), t.partial(optional)], name)
}

const C = interfaceWithOptionals({ foo: t.string }, { bar: t.number })

Custom types

You can define your own types. Let's see an example

import * as t from 'io-ts'

// represents a Date from an ISO string
const DateFromString = new t.Type<t.mixed, Date>(
  'DateFromString',
  (v): v is Date => v instanceof Date,
  (v, c) =>
    t.string.validate(v, c).chain(s => {
      const d = new Date(s)
      return isNaN(d.getTime()) ? t.failure(s, c) : t.success(d)
    }),
  a => a.toISOString()
)

const s = new Date(1973, 10, 30).toISOString()

t.validate(s, DateFromString)
// right(new Date('1973-11-29T23:00:00.000Z'))

t.validate('foo', DateFromString)
// left(errors...)

Note that you can deserialize while validating.

Custom combinators

You can define your own combinators. Let's see some examples

The maybe combinator

An equivalent to T | null

export function maybe<RT extends t.Any>(type: RT, name?: string): t.UnionType<[RT, t.NullType], t.TypeOf<RT> | null> {
  return t.union<[RT, t.NullType]>([type, t.null], name)
}

The pluck combinator

Extracting the runtime type of a field contained in each member of a union

const pluck = <F extends string, U extends t.UnionType<Array<t.InterfaceType<{ [K in F]: t.Any }, any>>, any>>(
  union: U,
  field: F
): t.Type<t.mixed, t.TypeOf<U>[F]> => {
  return t.union(union.types.map(type => type.props[field]))
}

export const Action = t.union([
  t.interface({
    type: t.literal('Action1'),
    payload: t.interface({
      foo: t.string
    })
  }),
  t.interface({
    type: t.literal('Action2'),
    payload: t.interface({
      bar: t.string
    })
  })
])

// ActionType: t.Type<t.mixed, "Action1" | "Action2">
const ActionType = pluck(Action, 'type')

Recipes

Is there a way to turn the checks off in production code?

No, however you can define your own logic for that (if you really trust the input and the involved types don't perform deserializations)

import * as t from 'io-ts'
import { failure } from 'io-ts/lib/PathReporter'

const { NODE_ENV } = process.env

export function unsafeValidate<S, A>(value: any, type: t.Type<S, A>): A {
  if (NODE_ENV !== 'production') {
    return t.validate(value, type).getOrElse(errors => {
      throw new Error(failure(errors).join('\n'))
    })
  }
  // unsafe cast
  return value as A
}

Known issues

Due to an upstream bug, VS Code might display weird types for nested interfaces

const NestedInterface = t.interface({
  foo: t.interface({
    bar: t.string
  })
})

type NestedInterfaceType = t.TypeOf<typeof NestedInterface>
/*
Hover on NestedInterfaceType will display

type NestedInterfaceType = {
  foo: t.InterfaceOf<{
    bar: t.StringType;
  }>;
}

instead of

type NestedInterfaceType = {
  foo: {
    bar: string;
  };
}
*/

Current Tags

  • 2.2.21                                ...           latest (a year ago)
  • 2.0.3                                ...           next (5 years ago)

120 Versions

  • 2.2.21                                ...           a year ago
  • 2.2.20                                ...           2 years ago
  • 2.2.19                                ...           2 years ago
  • 2.2.18                                ...           2 years ago
  • 2.2.17                                ...           2 years ago
  • 2.2.16                                ...           4 years ago
  • 2.2.15                                ...           4 years ago
  • 2.2.14                                ...           4 years ago
  • 2.2.13                                ...           4 years ago
  • 2.2.12                                ...           4 years ago
  • 2.2.11                                ...           4 years ago
  • 2.2.10                                ...           4 years ago
  • 2.2.9                                ...           4 years ago
  • 2.2.8                                ...           4 years ago
  • 2.2.7                                ...           4 years ago
  • 2.2.6                                ...           5 years ago
  • 2.2.5                                ...           5 years ago
  • 2.2.4                                ...           5 years ago
  • 2.2.3                                ...           5 years ago
  • 2.2.2                                ...           5 years ago
  • 2.2.1                                ...           5 years ago
  • 2.2.0                                ...           5 years ago
  • 2.1.3                                ...           5 years ago
  • 2.1.2                                ...           5 years ago
  • 2.1.1                                ...           5 years ago
  • 2.1.0                                ...           5 years ago
  • 2.0.6                                ...           5 years ago
  • 2.0.5                                ...           5 years ago
  • 2.0.4                                ...           5 years ago
  • 2.0.3                                ...           5 years ago
  • 2.0.2                                ...           5 years ago
  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.10.4                                ...           5 years ago
  • 1.10.3                                ...           5 years ago
  • 1.10.2                                ...           6 years ago
  • 1.10.1                                ...           6 years ago
  • 1.10.0                                ...           6 years ago
  • 1.9.0                                ...           6 years ago
  • 1.8.6                                ...           6 years ago
  • 1.8.5                                ...           6 years ago
  • 1.8.4                                ...           6 years ago
  • 1.8.3                                ...           6 years ago
  • 1.8.2                                ...           6 years ago
  • 1.8.1                                ...           6 years ago
  • 1.8.0                                ...           6 years ago
  • 1.7.1                                ...           6 years ago
  • 1.7.0                                ...           6 years ago
  • 1.6.4                                ...           6 years ago
  • 1.6.3                                ...           6 years ago
  • 1.6.2                                ...           6 years ago
  • 1.6.1                                ...           6 years ago
  • 1.6.0                                ...           6 years ago
  • 1.5.3                                ...           6 years ago
  • 1.5.2                                ...           6 years ago
  • 1.5.1                                ...           6 years ago
  • 1.5.0                                ...           6 years ago
  • 1.4.2                                ...           6 years ago
  • 1.4.1                                ...           6 years ago
  • 1.4.0                                ...           6 years ago
  • 1.3.4                                ...           6 years ago
  • 1.3.3                                ...           6 years ago
  • 1.3.2                                ...           6 years ago
  • 1.3.1                                ...           6 years ago
  • 1.3.0                                ...           6 years ago
  • 1.2.1                                ...           6 years ago
  • 1.2.0                                ...           6 years ago
  • 1.1.5                                ...           7 years ago
  • 1.1.4                                ...           7 years ago
  • 1.1.3                                ...           7 years ago
  • 1.1.2                                ...           7 years ago
  • 1.1.1                                ...           7 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.6                                ...           7 years ago
  • 1.0.5                                ...           7 years ago
  • 1.0.4                                ...           7 years ago
  • 1.0.3                                ...           7 years ago
  • 1.0.2                                ...           7 years ago
  • 1.0.1                                ...           7 years ago
  • 1.0.0                                ...           7 years ago
  • 0.9.8                                ...           7 years ago
  • 0.9.8-rc                                ...           7 years ago
  • 0.9.7                                ...           7 years ago
  • 0.9.6                                ...           7 years ago
  • 0.9.5                                ...           7 years ago
  • 0.9.4                                ...           7 years ago
  • 0.9.3                                ...           7 years ago
  • 0.9.2                                ...           7 years ago
  • 0.9.2-dev.20171215                                ...           7 years ago
  • 0.9.1                                ...           7 years ago
  • 0.9.1-dev.20171201                                ...           7 years ago
  • 0.9.0                                ...           7 years ago
  • 0.9.0-dev.20171123                                ...           7 years ago
  • 0.9.0-dev.20171122                                ...           7 years ago
  • 0.9.0-dev.20171117                                ...           7 years ago
  • 0.9.0-dev.20171113                                ...           7 years ago
  • 0.8.2                                ...           7 years ago
  • 0.8.1                                ...           7 years ago
  • 0.8.0                                ...           7 years ago
  • 0.7.2                                ...           7 years ago
  • 0.7.1                                ...           7 years ago
  • 0.7.0                                ...           7 years ago
  • 0.6.2                                ...           7 years ago
  • 0.6.1                                ...           7 years ago
  • 0.6.0                                ...           7 years ago
  • 0.5.1                                ...           7 years ago
  • 0.5.0                                ...           8 years ago
  • 0.4.0                                ...           8 years ago
  • 0.3.2                                ...           8 years ago
  • 0.3.1                                ...           8 years ago
  • 0.3.0                                ...           8 years ago
  • 0.2.3                                ...           8 years ago
  • 0.2.2                                ...           8 years ago
  • 0.2.1                                ...           8 years ago
  • 0.2.0                                ...           8 years ago
  • 0.1.1                                ...           8 years ago
  • 0.1.0                                ...           8 years ago
  • 0.0.3                                ...           8 years ago
  • 0.0.2                                ...           8 years ago
  • 0.0.1                                ...           8 years ago
Maintainers (1)
Downloads
Total 2
Today 1
This Week 1
This Month 2
Last Day 0
Last Week 1
Last Month 0
Dependencies (1)
Dev Dependencies (11)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE