PHPackages                             coderofsalvation/typeshave - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. coderofsalvation/typeshave

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

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)

091PHP

Since May 28Pushed 5y ago1 watchersCompare

[ Source](https://github.com/coderofsalvation/typeshave.php)[ Packagist](https://packagist.org/packages/coderofsalvation/typeshave)[ RSS](/packages/coderofsalvation-typeshave/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

typeshave
=========

[](#typeshave)

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

[![](https://camo.githubusercontent.com/68ee33c5f79d869c0e6534d3068410b3b9ba8be3ba7b778c97c97c59b3a26cf5/687474703a2f2f636f6465726f6673616c766174696f6e2e6769746875622e696f2f7479706573686176652f6c6f676f2e706e67)](https://camo.githubusercontent.com/68ee33c5f79d869c0e6534d3068410b3b9ba8be3ba7b778c97c97c59b3a26cf5/687474703a2f2f636f6465726f6673616c766174696f6e2e6769746875622e696f2f7479706573686176652f6c6f676f2e706e67)

Guard your function's incoming data using typeshave wrappers in JS &amp; PHP ([typeshave website](http://coderofsalvation.github.io/typeshave/))

Usage
-----

[](#usage)

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

```

and then

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

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

```

> typeshave uses the established [jsonschema](http://jsonschema.net) validation-format, which even supports validating nested datastructures. Re-usable in many other areas (database-, restpayload-, form-validation and so on)

or

```
// 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](test/test.json) without getting verbose

```
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](https://github.com/coderofsalvation/typeshave.php/blob/master/test/test.json)

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

[](#why-non-typesafe-is-great-but-not-with-phat-nested-objects)

For example:

- 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

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

```

Did you you try PITA-fying your code with if/else checks?

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

[](#conclusion)

No more :

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

[![](https://camo.githubusercontent.com/8e4099b6b58aaf023eaa0e7fc59b680072ef864c39b08ff52e9dec3e846a07a3/687474703a2f2f7777772e67696662696e2e636f6d2f62696e2f3130323030392f313235363535333534315f6578706c6f64696e672d74726173682e676966)](https://camo.githubusercontent.com/8e4099b6b58aaf023eaa0e7fc59b680072ef864c39b08ff52e9dec3e846a07a3/687474703a2f2f7777772e67696662696e2e636f6d2f62696e2f3130323030392f313235363535333534315f6578706c6f64696e672d74726173682e676966)

NOTE: arrays
------------

[](#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:

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

```

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/f3ff28af20ec6cdc02a9b020c1f9ef72012f8a597c3b2e5ffd17ac032e893945?d=identicon)[coderofsalvation](/maintainers/coderofsalvation)

---

Top Contributors

[![coderofsalvation](https://avatars.githubusercontent.com/u/180068?v=4)](https://github.com/coderofsalvation "coderofsalvation (20 commits)")

### Embed Badge

![Health badge](/badges/coderofsalvation-typeshave/health.svg)

```
[![Health](https://phpackages.com/badges/coderofsalvation-typeshave/health.svg)](https://phpackages.com/packages/coderofsalvation-typeshave)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
