{"id":21477578,"url":"https://github.com/phpdevcommunity/php-validator","last_synced_at":"2025-07-15T10:33:33.141Z","repository":{"id":263710060,"uuid":"869444333","full_name":"phpdevcommunity/php-validator","owner":"phpdevcommunity","description":"PHP Validator is a fast, extensible, and simple PHP validation library that allows you to easily validate various types of data.","archived":false,"fork":false,"pushed_at":"2024-10-08T10:01:52.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-20T02:37:55.061Z","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/phpdevcommunity.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":"2024-10-08T09:59:25.000Z","updated_at":"2024-10-08T10:01:32.000Z","dependencies_parsed_at":"2024-11-20T02:37:58.714Z","dependency_job_id":null,"html_url":"https://github.com/phpdevcommunity/php-validator","commit_stats":null,"previous_names":["phpdevcommunity/php-validator"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpdevcommunity%2Fphp-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpdevcommunity%2Fphp-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpdevcommunity%2Fphp-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpdevcommunity%2Fphp-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phpdevcommunity","download_url":"https://codeload.github.com/phpdevcommunity/php-validator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226033243,"owners_count":17563126,"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-23T11:14:22.557Z","updated_at":"2025-07-15T10:33:33.131Z","avatar_url":"https://github.com/phpdevcommunity.png","language":"PHP","readme":"\r\n# PHP Validator\r\n\r\nPHP Validator is a fast, extensible, and simple PHP validation library that allows you to easily validate various types of data.\r\n\r\n## Installation\r\n\r\nYou can install this library via [Composer](https://getcomposer.org/). Ensure your project meets the minimum PHP version requirement of 7.4.\r\n\r\n```bash\r\ncomposer require phpdevcommunity/php-validator\r\n```\r\n\r\n## Requirements\r\n\r\n- PHP version 7.4 or higher\r\n- Required package for PSR-7 HTTP Message (e.g., `guzzlehttp/psr7`)\r\n\r\n## Usage\r\n\r\nThe PHP Validator library enables you to validate data in a simple and flexible manner using pre-configured validation rules. Here are some usage examples:\r\n\r\n### Example 1: Email Address Validation\r\n\r\nValidate an email address to ensure it is not null and matches the standard email format.\r\n\r\n```php\r\nuse PhpDevCommunity\\Validator\\Validation;\r\nuse PhpDevCommunity\\Validator\\Assert\\NotNull;\r\nuse PhpDevCommunity\\Validator\\Assert\\Email;\r\n\r\n// Instantiate Validation object for email validation\r\n$validation = new Validation([\r\n    'email' =\u003e [new NotNull(), new Email()]\r\n]);\r\n\r\n// Example data array\r\n$data = ['email' =\u003e 'john.doe@example.com'];\r\n\r\n// Validate the data\r\nif ($validation-\u003evalidateArray($data) === true) {\r\n    echo \"Email is valid!\";\r\n} else {\r\n    $errors = $validation-\u003egetErrors();\r\n    echo \"Validation errors: \" . implode(\", \", $errors['email']);\r\n}\r\n```\r\n\r\n### Example 2: Age Validation\r\n\r\nValidate the age to ensure it is a non-null integer and is 18 or older.\r\n\r\n```php\r\nuse PhpDevCommunity\\Validator\\Validation;\r\nuse PhpDevCommunity\\Validator\\Assert\\NotNull;\r\nuse PhpDevCommunity\\Validator\\Assert\\Integer;\r\n\r\n// Instantiate Validation object for age validation\r\n$validation = new Validation([\r\n    'age' =\u003e [new NotNull(), new Integer(['min' =\u003e 18])]\r\n]);\r\n\r\n// Example data array\r\n$data = ['age' =\u003e 25];\r\n\r\n// Validate the data\r\nif ($validation-\u003evalidateArray($data) === true) {\r\n    echo \"Age is valid!\";\r\n} else {\r\n    $errors = $validation-\u003egetErrors();\r\n    echo \"Validation errors: \" . implode(\", \", $errors['age']);\r\n}\r\n```\r\n\r\n### Additional Examples\r\n\r\nLet's explore more examples covering various validators:\r\n\r\n#### Username Validation\r\n\r\nEnsure that a username is not null, has a minimum length of 3 characters, and contains only alphanumeric characters.\r\n\r\n```php\r\nuse PhpDevCommunity\\Validator\\Validation;\r\nuse PhpDevCommunity\\Validator\\Assert\\NotNull;\r\nuse PhpDevCommunity\\Validator\\Assert\\Alphanumeric;\r\nuse PhpDevCommunity\\Validator\\Assert\\StringLength;\r\n\r\n// Instantiate Validation object for username validation\r\n$validation = new Validation([\r\n    'username' =\u003e [new NotNull(), new StringLength(['min' =\u003e 3]), new Alphanumeric()]\r\n]);\r\n\r\n// Example data array\r\n$data = ['username' =\u003e 'john_doe123'];\r\n\r\n// Validate the data\r\nif ($validation-\u003evalidateArray($data) === true) {\r\n    echo \"Username is valid!\";\r\n} else {\r\n    $errors = $validation-\u003egetErrors();\r\n    echo \"Validation errors: \" . implode(\", \", $errors['username']);\r\n}\r\n```\r\n\r\n#### Example: Validating Nested Objects with `Item`\r\n\r\nThe `Item` rule allows you to validate nested objects or associative arrays with specific rules for each key.\r\n\r\n```php\r\nuse PhpDevCommunity\\Validator\\Validation;\r\nuse PhpDevCommunity\\Validator\\Assert\\NotNull;\r\nuse PhpDevCommunity\\Validator\\Assert\\Item;\r\nuse PhpDevCommunity\\Validator\\Assert\\StringLength;\r\nuse PhpDevCommunity\\Validator\\Assert\\Alphabetic;\r\n\r\n// Define validation rules for a nested object (e.g., a \"person\" object)\r\n$validation = new Validation([\r\n    'person' =\u003e [new NotNull(), new Item([\r\n        'first_name' =\u003e [new NotNull(), new Alphabetic(), (new StringLength())-\u003emin(3)],\r\n        'last_name' =\u003e [new NotNull(), new Alphabetic(), (new StringLength())-\u003emin(3)],\r\n    ])]\r\n]);\r\n\r\n// Example data\r\n$data = [\r\n    'person' =\u003e [\r\n        'first_name' =\u003e 'John',\r\n        'last_name' =\u003e 'Doe'\r\n    ]\r\n];\r\n\r\n// Validate the data\r\nif ($validation-\u003evalidateArray($data) === true) {\r\n    echo \"Person object is valid!\";\r\n} else {\r\n    $errors = $validation-\u003egetErrors();\r\n    echo \"Validation errors: \" . json_encode($errors, JSON_PRETTY_PRINT);\r\n}\r\n```\r\n\r\n#### Example: Validating Arrays of Items with `Collection`\r\n\r\nThe `Collection` rule is used to validate arrays where each item in the array must satisfy a set of rules.\r\n\r\n```php\r\nuse PhpDevCommunity\\Validator\\Validation;\r\nuse PhpDevCommunity\\Validator\\Assert\\NotEmpty;\r\nuse PhpDevCommunity\\Validator\\Assert\\Collection;\r\nuse PhpDevCommunity\\Validator\\Assert\\Item;\r\nuse PhpDevCommunity\\Validator\\Assert\\NotNull;\r\nuse PhpDevCommunity\\Validator\\Assert\\StringLength;\r\n\r\n// Define validation rules for a collection of articles\r\n$validation = new Validation([\r\n    'articles' =\u003e [new NotEmpty(), new Collection([\r\n        new Item([\r\n            'title' =\u003e [new NotNull(), (new StringLength())-\u003emin(3)],\r\n            'body' =\u003e [new NotNull(), (new StringLength())-\u003emin(10)],\r\n        ])\r\n    ])]\r\n]);\r\n\r\n// Example data\r\n$data = [\r\n    'articles' =\u003e [\r\n        ['title' =\u003e 'Article 1', 'body' =\u003e 'This is the body of the first article.'],\r\n        ['title' =\u003e 'Article 2', 'body' =\u003e 'Second article body here.']\r\n    ]\r\n];\r\n\r\n// Validate the data\r\nif ($validation-\u003evalidateArray($data) === true) {\r\n    echo \"Articles are valid!\";\r\n} else {\r\n    $errors = $validation-\u003egetErrors();\r\n    echo \"Validation errors: \" . json_encode($errors, JSON_PRETTY_PRINT);\r\n}\r\n```\r\n\r\n#### URL Validation\r\n\r\nValidate a URL to ensure it is not null and is a valid URL format.\r\n\r\n```php\r\nuse PhpDevCommunity\\Validator\\Validation;\r\nuse PhpDevCommunity\\Validator\\Assert\\NotNull;\r\nuse PhpDevCommunity\\Validator\\Assert\\Url;\r\n\r\n// Instantiate Validation object for URL validation\r\n$validation = new Validation([\r\n    'website' =\u003e [new NotNull(), new Url()]\r\n]);\r\n\r\n// Example data array\r\n$data = ['website' =\u003e 'https://example.com'];\r\n\r\n// Validate the data\r\nif ($validation-\u003evalidateArray($data) === true) {\r\n    echo \"Website URL is valid!\";\r\n} else {\r\n    $errors = $validation-\u003egetErrors();\r\n    echo \"Validation errors: \" . implode(\", \", $errors['website']);\r\n}\r\n```\r\n\r\n#### Numeric Value Validation\r\n\r\nValidate a numeric value to ensure it is not null and represents a valid numeric value.\r\n\r\n```php\r\nuse PhpDevCommunity\\Validator\\Validation;\r\nuse PhpDevCommunity\\Validator\\Assert\\NotNull;\r\nuse PhpDevCommunity\\Validator\\Assert\\Numeric;\r\n\r\n// Instantiate Validation object for numeric value validation\r\n$validation = new Validation([\r\n    'price' =\u003e [new NotNull(), new Numeric()]\r\n]);\r\n\r\n// Example data array\r\n$data = ['price' =\u003e 99.99];\r\n\r\n// Validate the data\r\nif ($validation-\u003evalidateArray($data) === true) {\r\n    echo \"Price is valid!\";\r\n} else {\r\n    $errors = $validation-\u003egetErrors();\r\n    echo \"Validation errors: \" . implode(\", \", $errors['price']);\r\n}\r\n```\r\n\r\n#### Custom Validation Rule\r\n\r\nImplement a custom validation rule using a callback function.\r\n\r\n```php\r\nuse PhpDevCommunity\\Validator\\Validation;\r\nuse PhpDevCommunity\\Validator\\Assert\\NotNull;\r\nuse PhpDevCommunity\\Validator\\Assert\\Custom;\r\n\r\n// Custom validation function to check if the value is a boolean\r\n$isBoolean = function ($value) {\r\n    return is_bool($value);\r\n};\r\n\r\n// Instantiate Validation object with a custom validation rule\r\n$validation = new Validation([\r\n    'active' =\u003e [new NotNull(), new Custom($isBoolean)]\r\n]);\r\n\r\n// Example data array\r\n$data = ['active' =\u003e true];\r\n\r\n// Validate the data\r\nif ($validation-\u003evalidateArray($data) === true) {\r\n    echo \"Value is valid!\";\r\n} else {\r\n    $errors = $validation-\u003egetErrors();\r\n    echo \"Validation errors: \" . implode(\", \", $errors['active']);\r\n}\r\n```\r\n\r\nCertainly! Below is a chapter you can add to your README specifically covering examples for using the `validate(ServerRequestInterface $request)` method from your `Validation` class.\r\n\r\n## Using `validate(ServerRequestInterface $request)` Method\r\n\r\nThe `Validation` class provides a convenient method `validate(ServerRequestInterface $request)` to validate data extracted from a `\\Psr\\Http\\Message\\ServerRequestInterface` object. This method simplifies the process of validating request data typically received in a web application.\r\n\r\n### Example 1: Validating User Registration Form\r\n\r\nSuppose you have a user registration form with fields like `username`, `email`, `password`, and `age`. Here's how you can use the `validate` method to validate this form data:\r\n\r\n```php\r\nuse PhpDevCommunity\\Validator\\Validation;\r\nuse PhpDevCommunity\\Validator\\Assert\\NotNull;\r\nuse PhpDevCommunity\\Validator\\Assert\\Email;\r\nuse PhpDevCommunity\\Validator\\Assert\\Integer;\r\n\r\n// Define validation rules for each field\r\n$validation = new Validation([\r\n    'username' =\u003e [new NotNull()],\r\n    'email' =\u003e [new NotNull(), new Email()],\r\n    'password' =\u003e [new NotNull()],\r\n    'age' =\u003e [new NotNull(), new Integer()]\r\n]);\r\n\r\n// Assume $request is the \\Psr\\Http\\Message\\ServerRequestInterface object containing form data\r\nif ($validation-\u003evalidate($request) === true) {\r\n    // Validation passed, retrieve validated data\r\n    $validatedData = $validation-\u003egetData();\r\n    // Process registration logic here (e.g., save to database)\r\n    // $username = $validatedData['username'];\r\n    // $email = $validatedData['email'];\r\n    // $password = $validatedData['password'];\r\n    // $age = $validatedData['age'];\r\n    echo \"Registration successful!\";\r\n} else {\r\n    // Validation failed, retrieve validation errors\r\n    $errors = $validation-\u003egetErrors();\r\n    // Handle validation errors (e.g., display error messages to the user)\r\n    echo \"Validation errors: \" . implode(\", \", $errors);\r\n}\r\n```\r\n\r\nIn this example:\r\n- We instantiate a `Validation` object with validation rules defined for each field (`username`, `email`, `password`, `age`).\r\n- We call the `validate` method with the `$request` object containing form data.\r\n- If validation passes (`validate` method returns `true`), we retrieve the validated data using `$validation-\u003egetData()` and proceed with the registration logic.\r\n- If validation fails, we retrieve the validation errors using `$validation-\u003egetErrors()` and handle them accordingly.\r\n\r\n### Example 2: Validating API Input Data\r\n\r\nConsider validating input data received via an API endpoint. Here's how you can use the `validate` method in this context:\r\n\r\n```php\r\nuse PhpDevCommunity\\Validator\\Validation;\r\nuse PhpDevCommunity\\Validator\\Assert\\NotNull;\r\nuse PhpDevCommunity\\Validator\\Assert\\Numeric;\r\n\r\n// Define validation rules for API input data\r\n$validation = new Validation([\r\n    'product_id' =\u003e [new NotNull(), new Numeric()],\r\n    'quantity' =\u003e [new NotNull(), new Numeric()]\r\n]);\r\n\r\n// Assume $request is the \\Psr\\Http\\Message\\ServerRequestInterface object containing API input data\r\nif ($validation-\u003evalidate($request) === true) {\r\n    // Validation passed, proceed with processing API request\r\n    $validatedData = $validation-\u003egetData();\r\n    // Extract validated data\r\n    $productId = $validatedData['product_id'];\r\n    $quantity = $validatedData['quantity'];\r\n    echo \"API request validated successfully!\";\r\n} else {\r\n    // Validation failed, retrieve validation errors\r\n    $errors = $validation-\u003egetErrors();\r\n    // Handle validation errors (e.g., return error response to the client)\r\n    echo \"Validation errors: \" . implode(\", \", $errors);\r\n}\r\n```\r\n\r\nIn this example:\r\n- We define validation rules for `product_id` and `quantity`.\r\n- We call the `validate` method with the `$request` object containing API input data.\r\n- If validation passes, we retrieve the validated data using `$validation-\u003egetData()` and proceed with processing the API request.\r\n- If validation fails, we retrieve the validation errors using `$validation-\u003egetErrors()` and handle them appropriately.\r\n\r\n---\r\n\r\n### Additional Features\r\n\r\n- **Simple Interface**: Easily define validation rules using a straightforward interface.\r\n- **Extensible**: Extend the library with custom validation rules by implementing the `ValidatorInterface`.\r\n- **Error Handling**: Retrieve detailed validation errors for each field.\r\n\r\n---\r\n\r\n## License\r\n\r\nThis library is open-source software licensed under the [MIT license](LICENSE).\r\n\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpdevcommunity%2Fphp-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphpdevcommunity%2Fphp-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpdevcommunity%2Fphp-validator/lists"}