{"id":20672791,"url":"https://github.com/selective-php/validation","last_synced_at":"2025-04-19T19:10:17.679Z","repository":{"id":45580742,"uuid":"130050542","full_name":"selective-php/validation","owner":"selective-php","description":"A validation library for PHP that uses the notification pattern","archived":false,"fork":false,"pushed_at":"2023-09-09T22:05:41.000Z","size":114,"stargazers_count":34,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-29T12:04:30.948Z","etag":null,"topics":["middleware","php","slim4","validation","validator"],"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/selective-php.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2018-04-18T11:14:57.000Z","updated_at":"2024-11-17T20:16:01.000Z","dependencies_parsed_at":"2024-06-18T21:28:20.857Z","dependency_job_id":"de766418-8470-43bf-9ee8-e7b78763936d","html_url":"https://github.com/selective-php/validation","commit_stats":{"total_commits":140,"total_committers":5,"mean_commits":28.0,"dds":0.4357142857142857,"last_synced_commit":"6bcc078ed94628ed224b2f5a5c246736b985a05e"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Fvalidation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Fvalidation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Fvalidation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Fvalidation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/selective-php","download_url":"https://codeload.github.com/selective-php/validation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248943465,"owners_count":21186958,"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":["middleware","php","slim4","validation","validator"],"created_at":"2024-11-16T20:38:49.948Z","updated_at":"2025-04-19T19:10:17.645Z","avatar_url":"https://github.com/selective-php.png","language":"PHP","readme":"# Validation\n\nA validation library for PHP that uses\nthe [notification pattern](https://martinfowler.com/articles/replaceThrowWithNotification.html).\n\n[![Latest Version on Packagist](https://img.shields.io/github/release/selective-php/validation.svg)](https://packagist.org/packages/selective/validation)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)\n[![Build Status](https://github.com/selective-php/validation/workflows/build/badge.svg)](https://github.com/selective-php/validation/actions)\n[![Coverage Status](https://scrutinizer-ci.com/g/selective-php/validation/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/selective-php/validation/code-structure)\n[![Quality Score](https://scrutinizer-ci.com/g/selective-php/validation/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/selective-php/validation/?branch=master)\n[![Total Downloads](https://img.shields.io/packagist/dt/selective/validation.svg)](https://packagist.org/packages/selective/validation/stats)\n\n## Table of contents\n\n* [Requirements](#requirements)\n* [Installation](#installation)\n* [Usage](#usage)\n    * [Validating form data](#validating-form-data)\n    * [Validating JSON](#validating-json)\n    * [Regex](#regex)\n* [Validators](#validators)\n    * [CakePHP Validator](#cakephp-validator)\n* [Transformer](#transformer)\n* [License](#license)\n\n## Requirements\n\n* PHP 8.1+\n\n## Installation\n\n```shell\ncomposer require selective/validation\n```\n\n## Usage\n\n\u003e A notification is a collection of errors\n\nIn order to use a notification, you have to create the `ValidationResult` object. A `ValidationResult` can be really\nsimple:\n\n```php\n\u003c?php\n\nuse Selective\\Validation\\Exception\\ValidationException;\nuse Selective\\Validation\\ValidationResult;\n\n$validationResult = new ValidationResult();\n\nif (empty($data['username'])) {\n    $validationResult-\u003eaddError('username', 'Input required');\n}\n```\n\nYou can now test the `ValidationResult` and throw an exception if it contains errors.\n\n```php\n\u003c?php\nif ($validationResult-\u003efails()) {\n    throw new ValidationException('Please check your input', $validationResult);\n}\n```\n\n## Validating form data\n\nLogin example:\n\n```php\n\u003c?php\n\nuse Selective\\Validation\\Exception\\ValidationException;\nuse Selective\\Validation\\ValidationResult;\n\n// ...\n\n// Get all POST values\n$data = (array)$request-\u003egetParsedBody();\n\n$validation = new ValidationResult();\n\n// Validate username\nif (empty($data['username'])) {\n    $validation-\u003eaddError('username', 'Input required');\n}\n\n// Validate password\nif (empty($data['password'])) {\n    $validation-\u003eaddError('password', 'Input required');\n}\n\n// Check validation result\nif ($validation-\u003efails()) {\n    // Trigger error response (see validation middleware)\n    throw new ValidationException('Please check your input', $validation);\n}\n```\n\n### Validating JSON\n\nValidating a JSON request works like validating form data, because in PHP it's just an array from the request object.\n\n```php\n\u003c?php\nuse Selective\\Validation\\ValidationResult;\n\n// Fetch json data from request as array\n$jsonData = (array)$request-\u003egetParsedBody();\n\n$validation = new ValidationResult();\n\n// ...\n\nif ($validation-\u003efails()) {\n    throw new ValidationException('Please check your input', $validation);\n}\n```\n\nIn vanilla PHP you can fetch the JSON request as follows:\n\n```php\n\u003c?php\n\n$jsonData = (array)json_decode(file_get_contents('php://input'), true);\n\n// ...\n```\n\n### Regex\n\nThe `Selective\\Validation\\Regex\\ValidationRegex` class allows you to validate if\na given string conforms a defined regular expression.\n\n**Example usage:**\n\n```php\nuse Selective\\Validation\\Factory\\CakeValidationFactory;\nuse Selective\\Validation\\Regex\\ValidationRegex;\n// ...\n\n$data = [ /* ... */ ];\n\n$validationFactory = new CakeValidationFactory();\n$validator = $validationFactory-\u003ecreateValidator();\n\n$validator\n    -\u003eregex('id', ValidationRegex::ID, 'Invalid')\n    -\u003eregex('country', ValidationRegex::COUNTRY_ISO_2, 'Invalid country')\n    -\u003eregex('date_of_birth', ValidationRegex::DATE_DMY, 'Invalid date format');\n```\n\n### Middleware\n\nThe `ValidationExceptionMiddleware` catches the `ValidationException` and converts it into a nice JSON response.\n\n#### Slim 4 integration\n\nInsert a container definition for `ValidationExceptionMiddleware::class`:\n\n```php\n\u003c?php\n\nuse Psr\\Container\\ContainerInterface;\nuse Psr\\Http\\Message\\ResponseFactoryInterface;\nuse Selective\\Validation\\Encoder\\JsonEncoder;\nuse Selective\\Validation\\Middleware\\ValidationExceptionMiddleware;\nuse Selective\\Validation\\Transformer\\ErrorDetailsResultTransformer;\nuse Slim\\App;\nuse Slim\\Factory\\AppFactory;\n// ...\n\nreturn [\n    ValidationExceptionMiddleware::class =\u003e function (ContainerInterface $container) {\n        $factory = $container-\u003eget(ResponseFactoryInterface::class);\n\n        return new ValidationExceptionMiddleware(\n            $factory, \n            new ErrorDetailsResultTransformer(), \n            new JsonEncoder()\n        );\n    },\n\n    ResponseFactoryInterface::class =\u003e function (ContainerInterface $container) {\n        $app = $container-\u003eget(App::class);\n\n        return $app-\u003egetResponseFactory();\n    },\n\n    App::class =\u003e function (ContainerInterface $container) {\n        AppFactory::setContainer($container);\n\n        return AppFactory::create();\n    },\n\n    // ...\n\n];\n```\n\nAdd the `ValidationExceptionMiddleware` into your middleware stack:\n\n```php\n\u003c?php\n\nuse Selective\\Validation\\Middleware\\ValidationExceptionMiddleware;\nuse Slim\\Factory\\AppFactory;\n\nrequire_once __DIR__ . '/../vendor/autoload.php';\n\n$app = AppFactory::create();\n\n// ...\n\n$app-\u003eadd(ValidationExceptionMiddleware::class);\n\n// ...\n\n$app-\u003erun();\n```\n\n#### Usage in Slim\n\n```php\n\u003c?php\n\nuse Selective\\Validation\\ValidationException;\nuse Selective\\Validation\\ValidationResult;\n\n$validation = new ValidationResult();\n\n// Validate username\nif (empty($data-\u003eusername)) {\n    $validation-\u003eaddError('username', 'Input required');\n}\n\n// Check validation result\nif ($validation-\u003efails()) {\n    // Trigger the validation middleware\n    throw new ValidationException('Please check your input', $validation);\n}\n```\n\n## Validators\n\nYou can combine this library with a validator that is doing the actual validation of your input data.\n\nThe [converter pattern](https://java-design-patterns.com/patterns/converter/) makes it easy to map instances of one\nclass into instances of another class.\n\n### CakePHP Validator\n\nThe [cakephp/validation](https://github.com/cakephp/validation) library provides features \nto build validators that can validate arbitrary arrays of data with ease.\n\n**Installation**\n\n```\ncomposer require cakephp/validation\n```\n\n**Usage**\n\nThe `Cake\\Validation\\Validator::validate()` method returns a non-empty array when \nthere are validation failures. The list of errors then can be converted into \na `ValidationResult` using the `Selective\\Validation\\Factory\\CakeValidationFactory`\nor `Selective\\Validation\\Converter\\CakeValidationConverter`.\n\nFor example, if you want to validate a login form you could do the following:\n\n```php\nuse Selective\\Validation\\Factory\\CakeValidationFactory;\nuse Selective\\Validation\\Exception\\ValidationException;\n// ...\n\n// Within the Action class: fetch the request data, e.g. from a JSON request\n$data = (array)$request-\u003egetParsedBody();\n\n// Within the Application Service class: Do the validation\n$validationFactory = new CakeValidationFactory();\n$validator = $validationFactory-\u003ecreateValidator();\n\n$validator\n    -\u003enotEmptyString('username', 'Input required')\n    -\u003enotEmptyString('password', 'Input required');\n\n$validationResult = $validationFactory-\u003ecreateValidationResult(\n    $validator-\u003evalidate($data)\n);\n\nif ($validationResult-\u003efails()) {\n    throw new ValidationException('Please check your input', $validationResult);\n}\n```\n\nPlease note: The `CakeValidationFactory` should be injected via constructor.\n\n**Read more:** \u003chttps://odan.github.io/2020/10/18/slim4-cakephp-validation.html\u003e\n\n## Transformer\n\nIf you want to implement a custom response data structure, \nyou can implement a custom transformer against the\n`\\Selective\\Validation\\Transformer\\ResultTransformerInterface` interface.\n\n**Example**\n\n```php\n\u003c?php\n\nnamespace App\\Transformer;\n\nuse Selective\\Validation\\Exception\\ValidationException;\nuse Selective\\Validation\\Transformer\\ResultTransformerInterface;\nuse Selective\\Validation\\ValidationResult;\n\nfinal class MyValidationTransformer implements ResultTransformerInterface\n{\n    public function transform(\n        ValidationResult $validationResult, \n        ValidationException $exception = null\n    ): array {\n        // Implement your own data structure for the response\n        // ...\n\n        return [];\n    }\n}\n```\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fselective-php%2Fvalidation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fselective-php%2Fvalidation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fselective-php%2Fvalidation/lists"}