Git Product home page Git Product logo

typecheck's Introduction

typecheck

Simple, strict, extensible runtime type checker for JavaScript.

Basic usage

import T from '@umbrellio/typecheck'

// define a type
const userType = T.Struct({
  email: T.String,
  password: T.String,
  age: T.Option(T.Number),
  role: T.Sum('admin', 'support'),
})

// run a type check
T.check(userType, {
  email: '[email protected]',
  password: '12345',
  age: 12,
  role: 'admin',
}) // => true

Installation

Install with yarn:

$ yarn add @umbrellio/typecheck
# or with npm:
$ npm i -S @umbrellio/typecheck

Built-in type reference

Primitive types

  • T.String: kek, new String('pek')
  • T.Number: 69, new Number(100), -2131.31
  • T.Boolean: true, false

Arrays

Signature: T.Array(type)

T.Array(T.String) // [], ['kek', 'pek']
T.Array(T.Sum(T.Boolean, T.Number)) // [], [true, 1]

Option

Signature: T.Option(type)

T.Option(T.String) // null, undefined, 'some string'
T.Option(T.Array(T.Number)) // null, undefined, [], [69]

Struct

Signature: T.Struct(schema) where schema is an object

T.Struct({ name: T.String }) // { name: 'Ivan' }
T.Struct({ email: T.String, age: T.Option(T.Number) }) // { email: '[email protected]', age: 69 }

Sum

Signature: T.Sum(...types)

T.Sum(T.Number, T.String, T.Struct({ name: T.String })) // 69, '69', { name: 'ivan' }

Advanced usage

Custom types

import T from '@umbrellio/typecheck'

// age is a positive number
const Age = new T.Type(v => T.check(T.Number, v) && v > 0)

const userType = T.Struct({
  email: T.String,
  age: T.Option(Age),
})

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/typecheck.

License

Released under MIT License.

Authors

Created by Alexander Komarov.

Supported by Umbrellio

typecheck's People

Contributors

akxcv avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

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.