{"id":17348236,"url":"https://github.com/diogocavilha/array-validation","last_synced_at":"2025-04-14T21:11:28.998Z","repository":{"id":62487697,"uuid":"59666916","full_name":"diogocavilha/array-validation","owner":"diogocavilha","description":"It's a simple array validator which uses native filters and validators from PHP.","archived":false,"fork":false,"pushed_at":"2019-06-28T00:03:01.000Z","size":30,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T21:11:24.338Z","etag":null,"topics":["composer-library","composer-package","composer-packages","filter","php","php-package","validator"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/diogocavilha.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-25T13:48:10.000Z","updated_at":"2020-09-14T18:58:49.000Z","dependencies_parsed_at":"2022-11-02T10:46:42.893Z","dependency_job_id":null,"html_url":"https://github.com/diogocavilha/array-validation","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diogocavilha%2Farray-validation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diogocavilha%2Farray-validation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diogocavilha%2Farray-validation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diogocavilha%2Farray-validation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/diogocavilha","download_url":"https://codeload.github.com/diogocavilha/array-validation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248961236,"owners_count":21189993,"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":["composer-library","composer-package","composer-packages","filter","php","php-package","validator"],"created_at":"2024-10-15T16:51:40.733Z","updated_at":"2025-04-14T21:11:28.981Z","avatar_url":"https://github.com/diogocavilha.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/diogocavilha/array-validation.svg?branch=master)](https://travis-ci.org/diogocavilha/array-validation)\n[![Latest Stable Version](https://img.shields.io/packagist/v/array/validation.svg?style=flat-square)](https://packagist.org/packages/array/validation)\n\n[Documentação em português/Portuguese documentation](https://github.com/diogocavilha/array-validation/blob/master/README.pt-BR.md)\n\n# Array validation\n\nIt's a simple array validator which uses native filters and validators from PHP.\n\n# Installing\n\n```bash\ncomposer require array/validation\n```\n\n# Usage\n\nMethods:\n\n- `setFields(array $fieldsRules)`\n\u003e It adds optional fields to filter/validate.\n\n- `setRequiredFields(array $requiredFieldsRules)`\n\u003e It adds required fields to filter/validate.\n\n- `validate(array $input)`\n\u003e It validates an input array. It throws an exception in case the validation is not satisfied.\n\n- `isValid(array $input)`\n\u003e It validates an input array. It returns `true` in case the input array is valid, otherwise it returns `false`.\n\n- `removeOnly(array $fieldsToRemove)`\n\u003e It removes fields that are not in filter/validation rules.\n\n- `getValidArray()`\n\u003e It returns an array containing the filtered/validated data.\n\n- `getMessages()`\n\u003e It returns an array containing the validation messages. This method should be called after calling the `isValid`.\n\n### Validating required fields:\n\n```php\n\u003c?php\n\nuse Validation\\SimpleArray;\n\n$rules = [\n    'name' =\u003e FILTER_SANITIZE_STRING,\n    'age' =\u003e FILTER_VALIDATE_INT,\n];\n\n$arrayToValidate = [\n    'name' =\u003e 'Diogo Alexsander',\n    'age' =\u003e 26,\n];\n\n$validator = new SimpleArray();\n$validator\n    -\u003esetRequiredFields($rules)\n    -\u003evalidate($arrayToValidate);\n\n$data = $validator-\u003egetValidArray();\n```\n\n### Validating optional fields:\n\n```php\n\u003c?php\n\nuse Validation\\SimpleArray;\n\n$rules = [\n    'name' =\u003e FILTER_SANITIZE_STRING,\n    'age' =\u003e FILTER_VALIDATE_INT,\n];\n\n$arrayToValidate = [\n    'name' =\u003e 'Diogo Alexsander',\n];\n\n$validator = new SimpleArray();\n$validator\n    -\u003esetFields($rules)\n    -\u003evalidate($arrayToValidate);\n\n$data = $validator-\u003egetValidArray();\n```\n\n### Validating both:\n\n```php\n\u003c?php\n\nuse Validation\\SimpleArray;\n\n$fieldsRules = [\n    'name' =\u003e FILTER_SANITIZE_STRING,\n    'age' =\u003e FILTER_VALIDATE_INT,\n];\n\n$requiredFieldsRules = [\n    'id' =\u003e FILTER_VALIDATE_INT,\n];\n\n$arrayToValidate = [\n    'id' =\u003e 1,\n    'name' =\u003e 'Diogo Alexsander',\n    'age' =\u003e 26,\n];\n\n$validator = new SimpleArray();\n$validator\n    -\u003esetFields($fieldsRules)\n    -\u003esetRequiredFields($requiredFieldsRules)\n    -\u003evalidate($arrayToValidate);\n\n$data = $validator-\u003egetValidArray();\n```\n\nThe `removeOnly` method can be used to remove a few fields from input array.\n\nIn case it's not called, all the other fields that are not present in the filter/validation rules will be removed from input array.\n\nIf you wish to remove just a few fields from input array, the method `removeOnly` can be called by passing an array containing the fields you wish to remove.\n\n\u003e Ps: It's not possible to remove a field that is present in filter/validation rules, if so, it will throw a `RuntimeException`.\n\n```php\n\u003c?php\n\nuse Validation\\SimpleArray;\n\n$fieldsRules = [\n    'name' =\u003e FILTER_SANITIZE_STRING,\n    'age' =\u003e FILTER_VALIDATE_INT,\n];\n\n$requiredFieldsRules = [\n    'id' =\u003e FILTER_VALIDATE_INT,\n];\n\n$arrayToValidate = [\n    'id' =\u003e 1,\n    'name' =\u003e 'Diogo Alexsander',\n    'age' =\u003e 26,\n    'email' =\u003e 'unwanted',\n    'phone' =\u003e 'unwanted',\n];\n\n$validator = new SimpleArray();\n$validator\n    -\u003esetFields($fieldsRules)\n    -\u003esetRequiredFields($requiredFieldsRules)\n    -\u003evalidate($arrayToValidate);\n\n$data = $validator-\u003egetValidArray(); // It will return only 'id', 'name' and 'age'\n\n$validator-\u003eremoveOnly(['phone']);\n$data = $validator-\u003egetValidArray(); // It will return 'id', 'name', 'age' and 'email'\n```\n\nIf you don't want validator automatically throw exceptions when the validation is not satisfied, it's possible to call the method `isValid` instead of `validate`.\n\nAfter that, you can call the method `getMessages` to get an array containing the validation messages.\n\n#### Sample:\n\n```php\n\u003c?php\n\n$input = [\n    'name' =\u003e '\u003cstrong\u003eDiogo\u003c/strong\u003e',\n    'description' =\u003e \"\u003cb\u003eThis is a test\u003c/b\u003e, to know more about it \u003ca href='index.phtml'\u003eclick here\u003c/a\u003e\",\n    'email' =\u003e 'email@domain.com',\n    'phone' =\u003e '5555555 - test',\n];\n\n$rules = [\n    'phone' =\u003e FILTER_VALIDATE_INT,\n    'name' =\u003e FILTER_SANITIZE_STRING,\n    'description' =\u003e FILTER_SANITIZE_STRING\n];\n\n$rulesRequired = [\n    'id' =\u003e FILTER_VALIDATE_INT,\n    'code' =\u003e FILTER_VALIDATE_INT,\n];\n\n$validator = new SimpleArray();\n$validator\n    -\u003esetFields($rules)\n    -\u003esetRequiredFields($rulesRequired);\n    \nif (!$validator-\u003eisValid($input)) {\n    $messages = $validator-\u003egetMessages();\n    foreach ($messages as $message) {\n        echo $message, '\u003cbr\u003e';\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiogocavilha%2Farray-validation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiogocavilha%2Farray-validation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiogocavilha%2Farray-validation/lists"}