0.4.6 • Published 7 years ago

typeshave v0.4.6

Weekly downloads
5
License
BSD
Repository
github
Last release
7 years ago

TYPESHAVE

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

Build Status

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 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:

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.

0.4.6

7 years ago

0.4.5

7 years ago

0.4.4

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.3.24

8 years ago

0.3.23

8 years ago

0.3.22

8 years ago

0.3.21

8 years ago

0.3.2

8 years ago

0.3.19

8 years ago

0.3.18

8 years ago

0.3.17

8 years ago

0.3.16

8 years ago

0.3.15

8 years ago

0.3.14

8 years ago

0.3.13

8 years ago

0.3.12

8 years ago

0.3.11

8 years ago

0.3.1

8 years ago

0.3.9

8 years ago

0.3.6

8 years ago

0.3.5

8 years ago

0.3.0

8 years ago

0.2.50

8 years ago

0.2.49

8 years ago

0.2.48

8 years ago

0.2.47

8 years ago

0.2.44

9 years ago

0.2.43

9 years ago

0.2.42

9 years ago

0.2.41

9 years ago

0.2.4

9 years ago

0.2.2

9 years ago

0.2.0

9 years ago

0.1.93

9 years ago

0.1.92

9 years ago

0.1.91

9 years ago

0.1.9

9 years ago

0.1.8

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.0.65

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago