{"id":20737105,"url":"https://github.com/phpexpertsinc/datatypevalidator","last_synced_at":"2025-10-08T06:10:08.769Z","repository":{"id":57040341,"uuid":"186283928","full_name":"PHPExpertsInc/DataTypeValidator","owner":"PHPExpertsInc","description":"An easy to use data type validator (both strict and fuzzy). Laravel-like.","archived":false,"fork":false,"pushed_at":"2025-04-10T13:23:19.000Z","size":71,"stargazers_count":13,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"trunk","last_synced_at":"2025-04-10T13:59:07.255Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PHPExpertsInc.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-12T17:26:32.000Z","updated_at":"2025-04-10T12:46:23.000Z","dependencies_parsed_at":"2025-04-10T13:38:53.039Z","dependency_job_id":"7afc866e-4563-4c82-ad2a-b93e1eb774c4","html_url":"https://github.com/PHPExpertsInc/DataTypeValidator","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHPExpertsInc%2FDataTypeValidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHPExpertsInc%2FDataTypeValidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHPExpertsInc%2FDataTypeValidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHPExpertsInc%2FDataTypeValidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PHPExpertsInc","download_url":"https://codeload.github.com/PHPExpertsInc/DataTypeValidator/tar.gz/refs/heads/trunk","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250545680,"owners_count":21448247,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-17T06:13:33.714Z","updated_at":"2025-10-08T06:10:03.750Z","avatar_url":"https://github.com/PHPExpertsInc.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DataTypeValidator\n\n[![TravisCI](https://travis-ci.org/phpexpertsinc/DataTypeValidator.svg?branch=master)](https://travis-ci.org/phpexpertsinc/DataTypeValidator)\n[![Maintainability](https://api.codeclimate.com/v1/badges/5d56aa8b847dce751598/maintainability)](https://codeclimate.com/github/phpexpertsinc/DataTypeValidator/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/5d56aa8b847dce751598/test_coverage)](https://codeclimate.com/github/phpexpertsinc/DataTypeValidator/test_coverage)\n\nDataTypeValidator is a PHP Experts, Inc., Project designed for easy data type validation.\n\nIt supports both traditional, fuzzy, PHP data types (e.g., \"1.0\" can be both a float, int, and string)\nand strict data type validations ('1' is only a string, 1.0 is only a float, etc.).\n\n## Installation\n\nVia Composer\n\n```bash\ncomposer require phpexperts/datatype-validator\n```\n\n## Upgrading to v3:\n\nAs of v3.0, extra values not in the ruleset are explicitly asserted against when operating\nin strict mode. To upgrade, either use the `$this-\u003eisStrictMode()` to ensure you don't have\nextra inputs, or use the Fuzzy validator.\n\n## Usage\n\n```php\n// 1. Pick a Type Checker (IsAFuzzyDataType or IsAStrictDataType).\n//    * IsAFuzzyDataType tries its best to emulate PHP's `==`.\n//    * IsAStrictDataType observes PHP's `strict_types=1` rules.\n    $validator = new DataValidator(new IsAStrictDataType());\n\n// There are two powerful mechanisms out of the box:\n// 2. It is easy to validate any data type dynamically, without a ton of if statements.\n\n    $validator-\u003eisType('asdf', 'string'); // true or false.\n    $validator-\u003eassertIsType(1, 'int'); // null or throws InvalidDataTypeException\n\n// 3. You can also validate arrays:\n\n    $data = [\n        'name'     =\u003e 'Cheyenne',\n        'age'      =\u003e 22,\n        'birthday' =\u003e Carbon::parse('1996-12-04 15:15:15'),\n        'daysOld'  =\u003e 8194.35,\n        'today'    =\u003e Carbon::now(),\n        'sayHi'    =\u003e function () { return 'Hi!'; },\n    ];\n    \n    $rules = [\n         'name'     =\u003e 'string',\n         'age'      =\u003e 'int',\n         'birthday' =\u003e 'Carbon',\n         'daysOld'  =\u003e 'float',\n         'today'    =\u003e 'Carbon\\Carbon',\n         'sayHi'    =\u003e 'callable',\n     ];\n\n    $validator-\u003evalidate($data, $rules);\n\n// 4. DataValidator::validate() will return `true` on success or throw \n//    an `InvalidDataTypeException` that contains an array of errors:\n\n    $data = [\n        'name'     =\u003e 'Cheyenne',\n        'age'      =\u003e '22',\n        'birthday' =\u003e '1996-12-04 15:15:15',\n    ];\n    \n    try {\n        $validator-\u003evalidate($data, $rules);\n    } catch (InvalidDataTypeException $e) {\n        print_r($e-\u003egetReasons());\n\n        /* Output:\n        array:2 [\n          0 =\u003e \"age is not a valid int\"\n          1 =\u003e \"birthday is not a valid Carbon\"\n        ]\n        */\n    }\n\n// 5. It can validate objects based on their short name or full name.\n    $data = [\n        'yesterday' =\u003e \\Carbon\\Carbon::parse('2019-05-11'),\n        'tomorrow'  =\u003e \\Carbon\\Carbon::parse('2019-05-13'),\n    ];\n    \n    $validator-\u003evalidate($data, [\n        'yesterday' =\u003e '\\Carbon\\Carbon',\n        'tomorrow'  =\u003e 'Carbon',\n    ]);\n\n```\n\n## Benchmarks\n```bash\nphpbench run --report=aggregate\n```\n\nPHP v8.4 with opcache enabled\n+------------------------+----------------+------+-----------+---------+---------+---------+---------+---------+--------+---------+\n| benchmark              | subject        | revs | mem_peak  | best    | mean    | mode    | worst   | stdev   | rstdev | diff    |\n+------------------------+----------------+------+-----------+---------+---------+---------+---------+---------+--------+---------+\n| DataTypeValidatorBench | benchValidator | 1000 | 832.896kb | 4.414μs | 4.803μs | 4.876μs | 5.112μs | 0.233μs | ±4.85% | +2.57%  |\n| DataTypeValidatorBench | benchNative    | 1000 | 832.896kb | 2.670μs | 2.866μs | 2.745μs | 3.356μs | 0.250μs | ±8.71% | -42.25% |\n+------------------------+----------------+------+-----------+---------+---------+---------+---------+---------+--------+---------+\n\n\n# Use cases\n\nPHPExperts\\DataTypeValidator\\DataTypeValidator  \n ✔ Will report whether a strict or permissive validator is being used  \n ✔ Can bulk validate a data array  \n ✔ Will return the name of the data validator logic  \n ✔ Will return an array of invalid keys with explanations  \n ✔ Will silently ignore data not in the rules in permissive mode  \n ✔ Will explicitly fail when data is not in the rules in strict mode  \n ✔ Will silently ignore nullable rules with no data  \n ✔ Data cannot be null by default  \n ✔ Any data type that starts with a '?' is nullable  \n ✔ Any data type that ends with '[]' is an array of X  \n ✔ Will allow an empty array of something  \n ✔ Will allow a nullable array of something  \n ✔ Will throw a logic exception if a non string rule is given  \n\nPHPExperts\\DataTypeValidator\\DataTypeValidator: Assertions  \n ✔ Will assert a value is a bool  \n ✔ Will assert a value is an int  \n ✔ Will assert a value is a float  \n ✔ Will assert a value is a string  \n ✔ Will assert a value is an array  \n ✔ Will assert a value is an object  \n ✔ Will assert a value is a callable  \n ✔ Will assert a value is a resource  \n ✔ Will assert an array of something  \n ✔ Will assert an object by its short name  \n ✔ Will assert an object by its full name  \n\nPHPExperts\\DataTypeValidator\\DataTypeValidator: Data Type Checks  \n ✔ Will validate bools strictly  \n ✔ Will validate ints strictly  \n ✔ Will validate floats strictly  \n ✔ Will validate strings strictly  \n ✔ Will validate arrays strictly  \n ✔ Will validate objects  \n ✔ Will validate callables  \n ✔ Will validate resources  \n ✔ Will validate objects by their short name  \n ✔ Will validate objects by their full name  \n ✔ Can validate bools loosely  \n ✔ Can validate ints loosely  \n ✔ Can validate floats loosely  \n ✔ Can validate strings loosely  \n ✔ Can validate arrays loosely  \n ✔ Will validate arrays of something  \n ✔ Will validate anything as mixed type  \n\nPHPExperts\\DataTypeValidator\\IsAFuzzyDataType  \n ✔ Will say that it is a permissive validator  \n ✔ Will return true for valid values  \n ✔ Will return false for invalid values  \n ✔ Will match short classes  \n ✔ Will match specific classes  \n ✔ Will work with an array of something  \n\nPHPExperts\\DataTypeValidator\\IsAStrictDataType  \n ✔ Will say that it is a strict validator  \n ✔ Will return true for valid values  \n ✔ Will return false for invalid values  \n ✔ Will match short classes  \n ✔ Will match specific classes  \n ✔ Will work with an array of something  \n\nExtended assertIsAType Tests  \n✔ has extended tests for asserting it is a strict string  \n✔ has extended tests for asserting it is a strict int  \n✔ has extended tests for asserting it is a strict bool  \n✔ has extended tests for asserting it is a strict array  \n✔ has extended tests for asserting it is a strict specific object  \n✔ has extended tests for asserting it is a fuzzy string  \n✔ has extended tests for asserting it is a fuzzy int  \n✔ has extended tests for asserting it is a fuzzy bool  \n✔ has extended tests for asserting it is a fuzzy array  \n✔ has extended tests for asserting it is a fuzzy object  \n✔ has extended tests for asserting it is a fuzzy specific object  \n✔ has extended tests for asserting it is not a strict string  \n✔ has extended tests for asserting it is not a strict int  \n✔ has extended tests for asserting it is not a strict bool  \n✔ has extended tests for asserting it is not a strict array  \n✔ has extended tests for asserting it is not a strict specific object  \n✔ has extended tests for asserting it is not a fuzzy string  \n✔ has extended tests for asserting it is not a fuzzy int  \n✔ has extended tests for asserting it is not a fuzzy bool  \n✔ has extended tests for asserting it is not a fuzzy array  \n✔ has extended tests for asserting it is not a fuzzy object  \n✔ has extended tests for asserting it is not a fuzzy specific object  \n✔ has extended tests for asserting it is a mixed type\n\n## Testing\n\n```bash\nphpunit\n```\n\n# Contributors\n\n[Theodore R. Smith](https://www.phpexperts.pro/]) \u003ctheodore@phpexperts.pro\u003e  \nGPG Fingerprint: 4BF8 2613 1C34 87AC D28F  2AD8 EB24 A91D D612 5690  \nCEO: PHP Experts, Inc.\n\n## License\n\nMIT license. Please see the [license file](LICENSE) for more information.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpexpertsinc%2Fdatatypevalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphpexpertsinc%2Fdatatypevalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpexpertsinc%2Fdatatypevalidator/lists"}