0.4.6 • Published 8 years ago

typeshave v0.4.6

Weekly downloads
5
License
BSD
Repository
github
Last release
8 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

8 years ago

0.4.5

8 years ago

0.4.4

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.3.24

9 years ago

0.3.23

9 years ago

0.3.22

9 years ago

0.3.21

9 years ago

0.3.2

9 years ago

0.3.19

9 years ago

0.3.18

9 years ago

0.3.17

9 years ago

0.3.16

9 years ago

0.3.15

10 years ago

0.3.14

10 years ago

0.3.13

10 years ago

0.3.12

10 years ago

0.3.11

10 years ago

0.3.1

10 years ago

0.3.9

10 years ago

0.3.6

10 years ago

0.3.5

10 years ago

0.3.0

10 years ago

0.2.50

10 years ago

0.2.49

10 years ago

0.2.48

10 years ago

0.2.47

10 years ago

0.2.44

10 years ago

0.2.43

10 years ago

0.2.42

10 years ago

0.2.41

10 years ago

0.2.4

10 years ago

0.2.2

10 years ago

0.2.0

10 years ago

0.1.93

10 years ago

0.1.92

10 years ago

0.1.91

10 years ago

0.1.9

10 years ago

0.1.8

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.0.65

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago