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

typed-contracts

v3.0.0

Published

Validation with good Flow and TypeScript type inference

Downloads

1,041

Readme

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);