{"id":37266246,"url":"https://github.com/peakphp/array-validation","last_synced_at":"2026-01-16T00:33:32.210Z","repository":{"id":57037352,"uuid":"238811226","full_name":"peakphp/array-validation","owner":"peakphp","description":"Validation utilities for array structure","archived":false,"fork":false,"pushed_at":"2020-04-02T16:38:18.000Z","size":81,"stargazers_count":15,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-01-14T15:50:06.248Z","etag":null,"topics":["array","php","validation"],"latest_commit_sha":null,"homepage":"","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/peakphp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-07T00:11:39.000Z","updated_at":"2022-09-27T08:31:51.000Z","dependencies_parsed_at":"2022-08-24T14:52:52.802Z","dependency_job_id":null,"html_url":"https://github.com/peakphp/array-validation","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/peakphp/array-validation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peakphp%2Farray-validation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peakphp%2Farray-validation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peakphp%2Farray-validation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peakphp%2Farray-validation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peakphp","download_url":"https://codeload.github.com/peakphp/array-validation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peakphp%2Farray-validation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28474505,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T00:15:39.755Z","status":"ssl_error","status_checked_at":"2026-01-16T00:15:32.174Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["array","php","validation"],"created_at":"2026-01-16T00:33:31.632Z","updated_at":"2026-01-16T00:33:32.202Z","avatar_url":"https://github.com/peakphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Peak/ArrayValidation\n\n\u003ca href=\"https://packagist.org/packages/peak/array-validation\"\u003e\u003cimg src=\"https://poser.pugx.org/peak/array-validation/version\" alt=\"version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/peak/array-validation\"\u003e\u003cimg src=\"https://poser.pugx.org/peak/array-validation/downloads\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/peakphp/array-validation/blob/master/LICENSE.md\"\u003e\u003cimg src=\"https://poser.pugx.org/peak/array-validation/license\" alt=\"License\"\u003e\u003c/a\u003e\n\n## Installation\n\n     composer require peak/array-validation\n\n## What is this?\n\nThis component help you to validate array structure by:\n\n- validating the type of any key values\n- ensuring a data structure with expected keys requirements\n- preventing structure pollution by allowing only a set of keys\n\nThis is especially useful when dealing with json data request, before using the data, you must validate his content so\nyou can afterward check the value of those keys with your business logic without worrying about the type or presence of any key value.\n\n# How to use\n##### 8 Usages\n\n## 1- General validation \"à la carte\" (stateless)\n\n```php\n$validator = new Validator();\n\nif ($validator-\u003eexpectExactlyKeys($data, $keys) === true) {\n    // ...\n}\n```\n\n## 2- Validation with fluent interface (stateful)\n\n```php\n$data = [ // data\n    'tags' =\u003e [],\n    'name' =\u003e 'foobar'\n];\n$validation = new Validation($data);\n\n$validation\n    -\u003eexpectExactlyKeys(['tags', 'name'])\n    -\u003eexpectKeyToBeArray('tags');\n    -\u003eexpectKeyToBeString('name');\n\nif ($validation-\u003ehasErrors()) {\n    // $lastError = $validation-\u003egetLastError();\n    // $errors = $validation-\u003egetErrors();\n}\n```\n\n## 3- Strict validation with fluent interface (stateful)\n\n```php\n\n$validation = new StrictValidation($data);\n\n// will throw an exception if any of tests below fail\n$validation\n    -\u003eexpectOnlyKeys(['id', 'title', 'description', 'isPrivate', 'tags'])\n    -\u003eexpectAtLeastKeys(['id', 'title', 'description'])\n    -\u003eexpectKeyToBeInteger('id')\n    -\u003eexpectKeysToBeString(['title', 'description'])\n    -\u003eexpectKeyToBeBoolean('isPrivate')\n    -\u003eexpectKeyToBeArray('tags');\n\n// if we reach this point, it means the array structure is\n// valid according to the validation rules above.\n\n```\n\n## 4- Create a ValidationDefinition for later usage\n\n```php\n$vDef = new ValidationDefinition();\n$vDef\n    -\u003eexpectOnlyKeys(['title', 'content', 'description'])\n    -\u003eexpectAtLeastKeys(['title', 'content'])\n    -\u003eexpectKeysToBeString(['title', 'content', 'description']);\n\n$validation = new ValidationFromDefinition($vDef, $data);\n\nif ($validation-\u003ehasErrors()) {\n    // $validation-\u003egetErrors();\n}\n\n```\n\n## 5- Create a validation Schema for later usage\n\nSchema is just another way to write validation definitions. This format is ideal when you want to store schemas in file (ex: json, php array file, yml, etc.)\n\n```php\n\n$mySchema = [\n    'title' =\u003e [\n        'type' =\u003e 'string',\n        'required' =\u003e true\n    ],\n    'content' =\u003e [\n        'type' =\u003e 'string',\n        'nullable' =\u003e true,\n    ],\n];\n\n$schema = new Schema(new SchemaCompiler(), $mySchema, 'mySchemaName');\n\n$validation = new ValidationFromSchema($schema, $data);\n\nif ($validation-\u003ehasErrors()) {\n    // $validation-\u003egetErrors();\n}\n```\n\n\n\n## 6- Strict validation using ValidationDefinition\n\n```php\n// all validation definitions are executed at object creation and an exception is thrown if any of tests failed\nnew StrictValidationFromDefinition($validationDefinition, $arrayToValidate);\n```\n\n## 7- Strict validation using Schema\n\n```php\n// all validation definitions are executed at object creation and an exception is thrown if any of tests failed\nnew StrictValidationFromSchema($schema, $arrayToValidate);\n```\n\n## 8- Validation and Strict Validation with ValidationBuilder\n\n```php\n$validation = new ValidationBuilder();\n$validation\n    -\u003eexpectOnlyKeys(['title', 'content', 'description'])\n    -\u003eexpectAtLeastKeys(['title', 'content'])\n    -\u003eexpectKeysToBeString(['title', 'content', 'description']);\n\nif ($validation-\u003evalidate($data) === false)  {\n    // $validation-\u003egetErrors();\n    // $validation-\u003egetLastError();\n}\n```\n\nand with strict validation:\n\n```php\n//  will throw an exception if any of tests fail\n$validation-\u003estrictValidate($data);\n```\n\n# Validation methods\n```php\n\ninterface ValidationInterface\n{\n    public function expectExactlyKeys(array $keys);\n    public function expectOnlyOneFromKeys( array $keys);\n    public function expectAtLeastKeys(array $keys);\n    public function expectOnlyKeys(array $keys);\n    public function expectNKeys(int $n);\n    public function expectKeyToBeArray(string $key, bool $acceptNull = false);\n    public function expectKeysToBeArray(array $keys, bool $acceptNull = false);\n    public function expectKeyToBeInteger(string $key, bool $acceptNull = false);\n    public function expectKeysToBeInteger(array $keys, bool $acceptNull = false);\n    public function expectKeyToBeFloat(string $key, bool $acceptNull = false);\n    public function expectKeysToBeFloat(array $keys, bool $acceptNull = false);\n    public function expectKeyToBeString(string $key, bool $acceptNull = false);\n    public function expectKeysToBeString(array $keys, bool $acceptNull = false);\n    public function expectKeyToBeBoolean(string $key, bool $acceptNull = false);\n    public function expectKeysToBeBoolean(array $keys, bool $acceptNull = false);\n    public function expectKeyToBeObject(string $key, bool $acceptNull = false);\n    public function expectKeysToBeObject(array $keys, bool $acceptNull = false);\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeakphp%2Farray-validation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeakphp%2Farray-validation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeakphp%2Farray-validation/lists"}