# typeshave

> Typecheck functionguards for function arguments and (nested) objects when it matters (REST payloads etc)

Latest version **0.4.6** (published 2017-05-04) · BSD license · 0 weekly downloads

## Install

```sh
npm install typeshave
pnpm add typeshave
yarn add typeshave
bun add typeshave
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.6 |
| Published | 2017-05-04 |
| First published | 2015-08-23 |
| Weekly downloads | 0 |
| License | BSD |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Leon van Kammen / Coder of Salvation |
| Maintainers | coderofsalvation |
| Keywords | typecheck, nested, object, validation, typesafe, assertion, jsonschema, wrapper |

## Links

- npm: https://www.npmjs.com/package/typeshave
- Repository: https://github.com/coderofsalvation/typeshave.js
- Homepage: http://coderofsalvation.github.io/typeshave
- Issues: https://github.com/coderofsalvation/typeshave.js/issues
- npm.io page: https://npm.io/package/typeshave

## Dependencies (1)

- [tv4-node](https://npm.io/package/tv4-node.md) 0.0.0

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 0.4.6 (latest) — 2017-05-04
- 0.4.5 — 2017-05-04
- 0.4.4 — 2017-03-24
- 0.4.2 — 2017-03-22
- 0.4.1 — 2017-03-20
- 0.3.24 — 2016-10-03
- 0.3.23 — 2016-09-03
- 0.3.22 — 2016-01-07
- 0.3.21 — 2016-01-07
- 0.3.2 — 2016-01-05
- 0.3.19 — 2016-01-05
- 0.3.18 — 2016-01-05
- 0.3.17 — 2016-01-05
- 0.3.16 — 2016-01-05
- 0.3.15 — 2015-11-29
- … 35 more at https://npm.io/package/typeshave/versions

## README

TYPESHAVE 
=========

Prevent functions from exploding with garbage-in garbage-out.

![Build Status](https://travis-ci.org/coderofsalvation/typeshave.js.svg?branch=master)

<center><img src="https://raw.githubusercontent.com/coderofsalvation/typeshave/gh-pages/logo.png"/></center>

Typecheck functionguards for function arguments and (nested) objects when it matters (REST payloads etc):

Usage:   

    typeshave         = require("typeshave")
    typesafe          = typeshave.typesafe

    var foo = typesafe({
      foo: { type: "string" }
      bar: { type: "integer", required:true }
    }, (foo, bar) => {
      return console.log("arguments are valid");
    });

    foo(1); // throws typesafe exception

> NOTE: typeshave is built on the shoulders of the [jsonschema](http://jsonschema.net) standard. 

Output:
  
    Error: 
    {
      "data": 1,
      "errors": {
        "errors": [
          {
            "message": "Argument foo should be string"

## Why should I use this? 

Ever ran into this situation? :

    foo( { foo:"bar", bar: 123, records: [ 1, 2 ], cbs: [myfunction] } );

    function foo(data){
      if( data == undefined data.bar == undefined || bar == undefined || Argh this is a big PITA 
      // omg how do I even check properties recursively?
      // argh..forget about it? YOLO?
      // *wait until disaster happens*

Say bye bye to 

* the temptation of typescript?
* functions going out of control
* assertions-bloat inside functions 
* complaining about javascript not being 
* unsafe nested datastructures 
* verbose unittests doing typesafe stuff 

## Recover from errors:

The `typeshave.error(errors)` function is triggered in case of errors, you can define your own like so:

    typeshave.error = (errors) => {
      console.error(errors)
      return new Error(errors)
    }

## What about type-safe nested structures?

Passing around big-ass nested data?
You better police that data upfront:

     schema = {                                             
       type: "object", 
       properties:{                                         
         foo: { type: "string", regex: /abc/, required:true }, 
         bar: { type: "integer", minimum: 0, maximum: 100 }, 
         records:{
           type: "array", 
           required:true, 
           items: {
             type:"object", 
             properties: {
              name: { type: "string", minLength: 2 }, 
              age:  { type: "integer"              }       
             }
           } 
         }, 
         cbs: { type: "array", items: { type: "function", required :true } }
      }
                                                           
     function foo = typesafe( schema, ( data ) => {                    
       console.log "valid data passed!"                    
       # do something with data                            
     }


Then obviously at some point this happens:

<center><img src="http://www.gifbin.com/bin/102009/1256553541_exploding-trash.gif"/></center>

> Well not anymore with typeshave :)

## Usecases

* REST payloads 
* payment transaction payloads
* objects which represent configs or options 
* datastructures and resultsets for html-rendering or processing 

## In the browser 

    <script src="typeshave.min.js"></script>
    <script>
      typeshave = require("typeshave").typesafe;

      var foo = typeshave({
        foo: { type: "string" },
        bar: { type: "boolean" }
      }, function(foo,bar){
        alert("ok data passed!");
      });

      foo( "string", true );
    </script>

## Manual validation

Manual validation is always at your fingertips as well:

      var typeshave = require('typeshave)
      var validate  = typeshave.validate

      var foo = function(foo,bar)
         validate( arguments, {               // throws exception in case of error
          foo: { type: "string" },
          bar: { type: "boolean" }
        });
        // do stuff with data

> The example uses `arguments` as input, but passing an object would work as well.

---
_Source: https://npm.io/package/typeshave · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
