typed-contracts
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

typed-contracts

Lint and test

Validation with good Flow and TypeScript type inference

What it is

Contract is a function that receives an arbitrary value and validates it. Сontract returns the value if it passed the validation, or an instance of ValidationError otherwise. Contracts are a 100% guarantee that the data that passed validation matches your expected type definitions.

Install

npm: npm install --save typed-contracts

yarn: yarn add typed-contracts

How it works

All contracts have this interface:

type Contract<+T> = {
  (valueName: string): {
    (value: mixed): ValidationError | T,
    optional(value: mixed): ValidationError | void | T,
    maybe(value: mixed): ValidationError | ?T,
  },

  (valueName: string, value: mixed): ValidationError | T,

  optional(valueName: string): (value: mixed) => ValidationError | void | T,
  optional(valueName: string, value: mixed): ValidationError | void | T,

  maybe(valueName: string): (value: mixed) => ValidationError | ?T,
  maybe(valueName: string, value: mixed): ValidationError | ?T,
}

Interface usage:

contract('my value', value)               // => ValidationError | ValidValue
contract.maybe('my value', null)          // => null
contract.maybe('my value', undefined)     // => undefined
contract.maybe('my value', value)         // => ValidationError | ValidValue
contract.optional('my value', undefined)  // => undefined
contract.optional('my value', value)      // => ValidationError | ValidValue

Contracts are curried:

contract('my value')(value)
contract('my value').maybe(value)
contract('my value').optional(value)
contract.maybe('my value')(value)
contract.optional('my value')(value)

Contracts can be combined

/* @flow */

const {
  array, object, string,
  union, ValidationError,
} = require('typed-contracts')

type Person = {
  +name: string,
  +gender: 'm' | 'f',
  +friends: $ReadOnlyArray<Person>,
  +email?: string | $ReadOnlyArray<string>,
}

// person returns Person-compatible value or ValidationError
const person = object({
  name: string,
  gender: union('m', 'f'),
  friends: array((valueName, value) => person(valueName, value)),
  email: union(string, array(string)).optional,
})

// We need to control compatibility of the return value type with Person
const userValidate:
  (value: mixed) => Person | ValidationError =
    person('user')

const user = userValidate({ name: 'Vasya' })

Contracts API

array

Aliases: isArray, passArray, arr, isArr, passArr.

Creates a contract which expects an array of values that are validated by the initial contract.

(...contracts: Array<
  (valueName: string, value: mixed) => ValidationError | T,
>) => Contract

literal

Aliases: isLiteral, passLiteral, lit, isLit, passLit.

Creates a contract which expects a specific string, number or boolean value.

(expected: string | number | boolean) => Contract

nul

Aliases: isNull, passNull.

Creates a contract which expects null.

number

Aliases: isNumber, passNumber, num, isNum, passNum.

Creates a contract which expects a number.

boolean

Aliases: isBoolean, passBoolean, bool, isBool, passBool.

Creates a contract which expects a boolean.

object

Aliases: isObject, passObject, obj, isObj, passObj.

Creates a contract which expects an object whose properties are validated by the corresponding contracts in the spec.

(spec: { [key: string] (valueName: string, value: mixed) => (ValidationError | T) }) => Contract

shape

(spec: { [key: string] (valueName: string, value: mixed) => (ValidationError | void | T) }) => Contract

objectOf

Aliases: isObjectOf, passObjectOf, objOf, isObjOf, passObjOf.

Creates a contract which expects an object whose properties are validated by the corresponding contracts in the spec.

(...contracts: Array<
  | string
  | number
  | boolean
  | ((valueName: string, value: mixed) => ValidationError | T),
>) => Contract

string

Aliases: isString, passString, str, isStr, passStr.

Creates a contract which expects a string.

union

Aliases: isUnion, passUnion, uni, isUni, passUni.

Creates a contract which expects a value, validating one of the initial contracts.

(...contracts: Array<
  | string
  | number
  | boolean
  | ((valueName: string, value: mixed) => ValidationError | T),
>) => Contract

undef

Aliases: isUndefined, passUndefined, isUndef, passUndef, isVoid, passVoid.

Creates a contract which expects undefined.

Using with ADT

import * as t from 'typed-contracts';
import { type Either, Right, Left } from 'igogo';

const transform = <T>(
  result: t.ValidationError | T,
): Either<t.ValidationError, T> =>
  result instanceof t.ValidationError ? Left(result) : Right(result);

const string = t.string.mapResult(transform);

const number = t.number.match<Either<*, *>>(Right, Left);

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.0.050latest

Version History

VersionDownloads (Last 7 Days)Published
3.0.050
2.3.077
2.2.22
2.2.11
2.2.02
2.1.21
2.1.13
2.1.01
2.0.23
2.0.11
2.0.01
1.2.11
1.2.01
1.1.01
0.4.21
1.0.11
1.0.01
0.4.11
0.4.01
0.3.11
0.3.01
0.2.41
0.2.31
0.1.11
0.1.01
0.0.81
0.0.71
0.0.61
0.0.51
0.0.41
0.0.31
0.0.21
0.0.11

Package Sidebar

Install

npm i typed-contracts

Weekly Downloads

167

Version

3.0.0

License

MIT

Unpacked Size

66 kB

Total Files

27

Last publish

Collaborators

  • bigslycat-profile