PHPackages                             phpexperts/datatype-validator - 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. phpexperts/datatype-validator

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

phpexperts/datatype-validator
=============================

An easy to use data type validator (both strict and fuzzy).

v3.1.0(1y ago)131.2M↓90.1%2[4 issues](https://github.com/phpexpertsinc/DataTypeValidator/issues)2MITPHPPHP &gt;=8.0

Since May 12Pushed 1y ago2 watchersCompare

[ Source](https://github.com/phpexpertsinc/DataTypeValidator)[ Packagist](https://packagist.org/packages/phpexperts/datatype-validator)[ RSS](/packages/phpexperts-datatype-validator/feed)WikiDiscussions trunk Synced 2w ago

READMEChangelogDependencies (6)Versions (21)Used By (2)

DataTypeValidator
=================

[](#datatypevalidator)

[![TravisCI](https://camo.githubusercontent.com/9e48c05e40d0ccaaa64b42c4a06c3c878285b2d1931f5f7c77dc365d1e490b9d/68747470733a2f2f7472617669732d63692e6f72672f70687065787065727473696e632f446174615479706556616c696461746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/phpexpertsinc/DataTypeValidator)[![Maintainability](https://camo.githubusercontent.com/2b6ca548393d9d5b37bee31d15a0a381686328d384340d645ae1d291ae476272/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35643536616138623834376463653735313539382f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/phpexpertsinc/DataTypeValidator/maintainability)[![Test Coverage](https://camo.githubusercontent.com/c545db24cce16268b073778dc865fd3b48dd5336edb35cd653aa90b4d778ebdb/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35643536616138623834376463653735313539382f746573745f636f766572616765)](https://codeclimate.com/github/phpexpertsinc/DataTypeValidator/test_coverage)

DataTypeValidator is a PHP Experts, Inc., Project designed for easy data type validation.

It supports both traditional, fuzzy, PHP data types (e.g., "1.0" can be both a float, int, and string) and strict data type validations ('1' is only a string, 1.0 is only a float, etc.).

Installation
------------

[](#installation)

Via Composer

```
composer require phpexperts/datatype-validator
```

Upgrading to v3:
----------------

[](#upgrading-to-v3)

As of v3.0, extra values not in the ruleset are explicitly asserted against when operating in strict mode. To upgrade, either use the `$this->isStrictMode()` to ensure you don't have extra inputs, or use the Fuzzy validator.

Usage
-----

[](#usage)

```
// 1. Pick a Type Checker (IsAFuzzyDataType or IsAStrictDataType).
//    * IsAFuzzyDataType tries its best to emulate PHP's `==`.
//    * IsAStrictDataType observes PHP's `strict_types=1` rules.
    $validator = new DataValidator(new IsAStrictDataType());

// There are two powerful mechanisms out of the box:
// 2. It is easy to validate any data type dynamically, without a ton of if statements.

    $validator->isType('asdf', 'string'); // true or false.
    $validator->assertIsType(1, 'int'); // null or throws InvalidDataTypeException

// 3. You can also validate arrays:

    $data = [
        'name'     => 'Cheyenne',
        'age'      => 22,
        'birthday' => Carbon::parse('1996-12-04 15:15:15'),
        'daysOld'  => 8194.35,
        'today'    => Carbon::now(),
        'sayHi'    => function () { return 'Hi!'; },
    ];

    $rules = [
         'name'     => 'string',
         'age'      => 'int',
         'birthday' => 'Carbon',
         'daysOld'  => 'float',
         'today'    => 'Carbon\Carbon',
         'sayHi'    => 'callable',
     ];

    $validator->validate($data, $rules);

// 4. DataValidator::validate() will return `true` on success or throw
//    an `InvalidDataTypeException` that contains an array of errors:

    $data = [
        'name'     => 'Cheyenne',
        'age'      => '22',
        'birthday' => '1996-12-04 15:15:15',
    ];

    try {
        $validator->validate($data, $rules);
    } catch (InvalidDataTypeException $e) {
        print_r($e->getReasons());

        /* Output:
        array:2 [
          0 => "age is not a valid int"
          1 => "birthday is not a valid Carbon"
        ]
        */
    }

// 5. It can validate objects based on their short name or full name.
    $data = [
        'yesterday' => \Carbon\Carbon::parse('2019-05-11'),
        'tomorrow'  => \Carbon\Carbon::parse('2019-05-13'),
    ];

    $validator->validate($data, [
        'yesterday' => '\Carbon\Carbon',
        'tomorrow'  => 'Carbon',
    ]);
```

Benchmarks
----------

[](#benchmarks)

```
phpbench run --report=aggregate
```

PHP v8.4 with opcache enabled +------------------------+----------------+------+-----------+---------+---------+---------+---------+---------+--------+---------+ | benchmark | subject | revs | mem\_peak | best | mean | mode | worst | stdev | rstdev | diff | +------------------------+----------------+------+-----------+---------+---------+---------+---------+---------+--------+---------+ | DataTypeValidatorBench | benchValidator | 1000 | 832.896kb | 4.414μs | 4.803μs | 4.876μs | 5.112μs | 0.233μs | ±4.85% | +2.57% | | DataTypeValidatorBench | benchNative | 1000 | 832.896kb | 2.670μs | 2.866μs | 2.745μs | 3.356μs | 0.250μs | ±8.71% | -42.25% | +------------------------+----------------+------+-----------+---------+---------+---------+---------+---------+--------+---------+

Use cases
=========

[](#use-cases)

PHPExperts\\DataTypeValidator\\DataTypeValidator
✔ Will report whether a strict or permissive validator is being used
✔ Can bulk validate a data array
✔ Will return the name of the data validator logic
✔ Will return an array of invalid keys with explanations
✔ Will silently ignore data not in the rules in permissive mode
✔ Will explicitly fail when data is not in the rules in strict mode
✔ Will silently ignore nullable rules with no data
✔ Data cannot be null by default
✔ Any data type that starts with a '?' is nullable
✔ Any data type that ends with '\[\]' is an array of X
✔ Will allow an empty array of something
✔ Will allow a nullable array of something
✔ Will throw a logic exception if a non string rule is given

PHPExperts\\DataTypeValidator\\DataTypeValidator: Assertions
✔ Will assert a value is a bool
✔ Will assert a value is an int
✔ Will assert a value is a float
✔ Will assert a value is a string
✔ Will assert a value is an array
✔ Will assert a value is an object
✔ Will assert a value is a callable
✔ Will assert a value is a resource
✔ Will assert an array of something
✔ Will assert an object by its short name
✔ Will assert an object by its full name

PHPExperts\\DataTypeValidator\\DataTypeValidator: Data Type Checks
✔ Will validate bools strictly
✔ Will validate ints strictly
✔ Will validate floats strictly
✔ Will validate strings strictly
✔ Will validate arrays strictly
✔ Will validate objects
✔ Will validate callables
✔ Will validate resources
✔ Will validate objects by their short name
✔ Will validate objects by their full name
✔ Can validate bools loosely
✔ Can validate ints loosely
✔ Can validate floats loosely
✔ Can validate strings loosely
✔ Can validate arrays loosely
✔ Will validate arrays of something
✔ Will validate anything as mixed type

PHPExperts\\DataTypeValidator\\IsAFuzzyDataType
✔ Will say that it is a permissive validator
✔ Will return true for valid values
✔ Will return false for invalid values
✔ Will match short classes
✔ Will match specific classes
✔ Will work with an array of something

PHPExperts\\DataTypeValidator\\IsAStrictDataType
✔ Will say that it is a strict validator
✔ Will return true for valid values
✔ Will return false for invalid values
✔ Will match short classes
✔ Will match specific classes
✔ Will work with an array of something

Extended assertIsAType Tests
✔ has extended tests for asserting it is a strict string
✔ has extended tests for asserting it is a strict int
✔ has extended tests for asserting it is a strict bool
✔ has extended tests for asserting it is a strict array
✔ has extended tests for asserting it is a strict specific object
✔ has extended tests for asserting it is a fuzzy string
✔ has extended tests for asserting it is a fuzzy int
✔ has extended tests for asserting it is a fuzzy bool
✔ has extended tests for asserting it is a fuzzy array
✔ has extended tests for asserting it is a fuzzy object
✔ has extended tests for asserting it is a fuzzy specific object
✔ has extended tests for asserting it is not a strict string
✔ has extended tests for asserting it is not a strict int
✔ has extended tests for asserting it is not a strict bool
✔ has extended tests for asserting it is not a strict array
✔ has extended tests for asserting it is not a strict specific object
✔ has extended tests for asserting it is not a fuzzy string
✔ has extended tests for asserting it is not a fuzzy int
✔ has extended tests for asserting it is not a fuzzy bool
✔ has extended tests for asserting it is not a fuzzy array
✔ has extended tests for asserting it is not a fuzzy object
✔ has extended tests for asserting it is not a fuzzy specific object
✔ has extended tests for asserting it is a mixed type

Testing
-------

[](#testing)

```
phpunit
```

Contributors
============

[](#contributors)

[Theodore R. Smith](https://www.phpexperts.pro/%5D)
GPG Fingerprint: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
CEO: PHP Experts, Inc.

License
-------

[](#license)

MIT license. Please see the [license file](LICENSE) for more information.

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity43

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 98% 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.

###  Release Activity

Cadence

Every ~118 days

Recently: every ~19 days

Total

20

Last Release

417d ago

Major Versions

v1.6.1 → v2.0.02025-03-13

v1.6.2 → v2.x-dev2025-03-18

v1.7.0 → v3.0.02025-04-10

v1.8.1 → v3.1.02025-06-27

PHP version history (2 changes)v1.0.0PHP &gt;=7.1

v2.0.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3f3a2dd16766f6b03c330e65aaca9dfb97f1bbbb41c5e2af5681f58f670b7917?d=identicon)[hopeseekr](/maintainers/hopeseekr)

---

Top Contributors

[![hopeseekr](https://avatars.githubusercontent.com/u/1125541?v=4)](https://github.com/hopeseekr "hopeseekr (50 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

validatorvalidationdatatypedata typedatatype validatordata type validator

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phpexperts-datatype-validator/health.svg)

```
[![Health](https://phpackages.com/badges/phpexperts-datatype-validator/health.svg)](https://phpackages.com/packages/phpexperts-datatype-validator)
```

###  Alternatives

[respect/validation

The most awesome validation engine ever created for PHP

6.0k40.8M432](/packages/respect-validation)[opis/json-schema

Json Schema Validator for PHP

65446.2M365](/packages/opis-json-schema)[vlucas/valitron

Simple, elegant, stand-alone validation library with NO dependencies

1.6k4.7M143](/packages/vlucas-valitron)[intervention/validation

Additional validation rules for the Laravel framework

6837.4M22](/packages/intervention-validation)[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.4M50](/packages/proengsoft-laravel-jsvalidation)[wixel/gump

A fast, extensible &amp; stand-alone PHP input validation class that allows you to validate any data.

1.2k1.4M34](/packages/wixel-gump)

PHPackages © 2026

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