2017 © Pedro Peláez
 

library typeshave

Lightweight typesafe function wrapper for PHP/JS. Validate nested data for forms,api's and more with jsonschema (http://coderofsalvation.github.io/typeshave)

image

coderofsalvation/typeshave

Lightweight typesafe function wrapper for PHP/JS. Validate nested data for forms,api's and more with jsonschema (http://coderofsalvation.github.io/typeshave)

  • Monday, August 24, 2015
  • by coderofsalvation
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

typeshave

Prevent functions from exploding with garbage-in garbage-out., (*1)

, (*2)

Guard your function's incoming data using typeshave wrappers in JS & PHP (typeshave website), (*3)

Usage

$ echo '{ "minimum-stability": "dev" }' > composer.json
$ composer require coderofsalvation/typeshave

and then, (*4)

use coderofsalvation\TypeShave;

function foo2($foo){
  TypeShave::check( $foo, (object)array(
    "foo" => (object)array( "type" => "string", "minLength" => 1, "regex" => "/foo/" )
  ));
}

foo2( 123 );       // will throw exception 

or heck, we can even write jsonschema inline:, (*5)

function foo2($foo){
  TypeShave::check( $foo, '{ "foo": { "type": "string", "minLength":1, "regex": "/foo/"}  }' );
}

typeshave uses the established jsonschema validation-format, which even supports validating nested datastructures. Re-usable in many other areas (database-, restpayload-, form-validation and so on), (*6)

or, (*7)

// multiple arguments at once 

function foo($foo, $bar){
  TypeShave::check( func_get_args(), '{
    "foo": { "type": "string"  },
    "bar": { "type": "integer" }
  }');
  // do stuff
}

foo( true, true ); // will throw exception

or how about passing PHAT nested containers using separate jsonschema file without getting verbose, (*8)

function phat($container){
  TypeShave::check( $container, __DIR__."/test.json" ); 
}

$container = (object)array(
  "foo" => "foo",
  "bar" => "bar2",
  "records" => array(
    (object)array( "foo" => "foo" )
  )
);

phat($container);

see test.json here, (*9)

why non-typesafe is great, but not with PHAT nested objects

For example:, (*10)

  • REST payloads
  • objects which represent configs or options
  • datastructures and resultsets for html-rendering or processing purposes

Are you still passing phat data around fingers-crossed-style? Still wondering why functions like this explode once in a while? :D, (*11)

foo( (object)array( "foo"=>"bar", "bar"=>123, "records": array( 1, 2 )) );

Did you you try PITA-fying your code with if/else checks?, (*12)

function foo($data){
  if( isset($data)          && 
      is_object($data)      && 
      isset($data->foo)     && 
      is_string($data->foo) &&
      .. 
      && 
      .. 
      && Argh this is a big PITA 
  // omg how do I even check properties recursively?
  foreach( $data->records as $record ){
    // PITA 
    // PITA 
    // PITA 
    // PITA 
  }
  ...
  // now finally we can do what the function should do :/
}

Conclusion

No more :, (*13)

  • functions going out of control
  • assertions-bloat inside functions
  • complaining about javascript not being typesafe
  • typesafe nested datastructures
  • verbose unittests doing typesafe stuff

Typeshave deals with problems immediately when they occur to prevent this:, (*14)

, (*15)

NOTE: arrays

the v4 jsonschema validator does support arrays by noticing the 'items'-variable, so please omit type: "array" which can be found in older jsonschema formats:, (*16)

   "myarray": {
//   "type": array,     *REMOVE ME*
     "items": [{
        "type": "integer"  
      }]
   }

The Versions

24/08 2015

dev-master

9999999-dev

Lightweight typesafe function wrapper for PHP/JS. Validate nested data for forms,api's and more with jsonschema (http://coderofsalvation.github.io/typeshave)

  Sources   Download

BSD-3-Clause

The Requires