{"id":18576114,"url":"https://github.com/thiagodp/validator","last_synced_at":"2025-04-10T08:31:02.641Z","repository":{"id":57040954,"uuid":"43863904","full_name":"thiagodp/validator","owner":"thiagodp","description":"Easy and powerful validator for PHP.","archived":false,"fork":false,"pushed_at":"2018-02-13T00:04:28.000Z","size":81,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T19:02:28.525Z","etag":null,"topics":["class-validation","php","phputil","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":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thiagodp.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}},"created_at":"2015-10-08T04:57:13.000Z","updated_at":"2018-12-21T18:13:57.000Z","dependencies_parsed_at":"2022-08-23T23:30:49.748Z","dependency_job_id":null,"html_url":"https://github.com/thiagodp/validator","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fvalidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fvalidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fvalidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thiagodp%2Fvalidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thiagodp","download_url":"https://codeload.github.com/thiagodp/validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248185321,"owners_count":21061494,"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":["class-validation","php","phputil","validation","validator"],"created_at":"2024-11-06T23:23:38.359Z","updated_at":"2025-04-10T08:31:02.315Z","avatar_url":"https://github.com/thiagodp.png","language":"PHP","readme":"# phputil\\validator\n\nEasy and powerful validation library for PHP.\n\n[![Build Status](https://travis-ci.org/thiagodp/validator.svg?branch=master)](https://travis-ci.org/thiagodp/validator)\n\nWe use [semantic versioning](http://semver.org/). See [our releases](https://github.com/thiagodp/validator/releases).\n\n## Installation\n\n```command\ncomposer require phputil/validator\n```\nDependends only on [phputil/rtti](https://github.com/thiagodp/rtti).\n\n## An Example\n\nA step-by-step example for demonstrating its use.\n```php\n// Suppose that your application receives an object in a JSON like this:\n$json = \u003c\u003c\u003cEXAMPLE\n{\n\t\"name\": \"Bob Developer\",\n\t\"likes\": 150,\n\t\"phone\": { \"number\": \"99988-7766\", \"notes\": \"WhatsApp, Telegram\" },\n\t\"friends\": [ \"Suzan\", \"Mike\", \"Jane\" ]\n}\nEXAMPLE;\n// So you transform the JSON into an object\n$obj = json_decode( $json );\n// Now you want to validate this object, and you create a Validator\n$validator = new Validator();\n// And define the rules\n$rules = array(\n\t// name must have from 2 to 60 characters\n\t'name' =\u003e array( Rule::LENGTH_RANGE =\u003e array( 2, 60 ), Option::LABEL =\u003e 'Name' ),\n\t// likes must be greater or equal to zero\n\t'likes' =\u003e array( Rule::MIN_VAUE =\u003e 0 ),\n\t// for the phone...\n\t'phone' =\u003e array( Rule::WITH =\u003e array(\n\t\t// number must follow a regex\n\t\t'number' =\u003e array(\n\t\t\tRule::REGEX =\u003e '/^[0-9]{5}\\\\-[0-9]{4}$/',\n\t\t\tOption::LABEL =\u003e 'Phone Number'\n\t\t\t)\n\t) ),\n\t// have a friend limit\n\t'friends' =\u003e array( Rule::MAX_COUNT =\u003e 100 )\n);\n// And define the messages (we also could load it from a JSON file)\n$messages = array(\n\t'en' =\u003e array( // \"en\" means \"english\" locale. This is the default locale.\n\t\tRule::LENGTH_RANGE =\u003e '{label} must have from {min_length} to {max_length} characters.',\n\t\tRule::MIN_VAUE =\u003e '{label} must be greater than or equal to {min_value}.',\n\t\tRule::REGEX =\u003e '{label} has an invalid format.',\n\t\tRule::MAX_COUNT =\u003e '{label} must have up to {max_count} item(s).',\n\t)\n);\n$validator-\u003esetMessages( $messages );\n\n// Now we will check the object using our rules\n$problems = $validator-\u003echeckObject( $obj, $rules );\n// In this moment, problems will be an empty array because all values passed.\n// That is: $problems === array()\n\n// However, lets make our rules harder, just to understand how the validation works\n$rules[ 'name' ][ Rule::LENGTH_RANGE ] = array( 2, 5 ); // Max of 5\n$rule[ 'friends' ][ Rule::MAX_COUNT ] = 1; // just one friend (the best one :-)\n\n// And check again\n$problems = $validator-\u003echeckObject( $obj, $rules );\n// Now $problems is an array like this:\n// array(\n//\t'name' =\u003e array( 'length_range' =\u003e 'Name must have from 2 to 5 characters.' ),\n//\t'friends' =\u003e array( 'max_count' =\u003e 'friends must have up to 1 item(s)' )\n// )\n// Which means that we have two fields with problems. The format is:\n//  field =\u003e hurt rule =\u003e message\n// For example, the field \"name\" hurt the \"length_range\" rule and its message is\n// \"Name must have from 2 to 5 characters.\".\n//\n// If we need to know whether \"name\" has a problem, we just check with isset:\nif ( isset( $problems[ 'name' ] ) ) {\n\techo 'Name has a problem', PHP_EOL;\n}\n// If we are only interested in the messages, and don't care about the fields,\n// we just use the ProblemsTransformer\n$messages = ( new ProblemsTransformer() )-\u003ejustTheMessages( $problems );\nvar_dump( $messages );\n// Will print something like:\n// array( 'Name must have from 2 to 5 characters.', 'friends must have up to 1 item(s)' )\n//\n// That's it for now. Enjoy it!\n```\n\n## Features\n\n- [x] Validates basic types (see [example 1](https://github.com/thiagodp/validator/tree/master/examples/ex1.php))\n- [x] Validates arrays (see [example 2](https://github.com/thiagodp/validator/tree/master/examples/ex3.php))\n- [x] Validates dynamic objects (`stdClass`) (see [example 3](https://github.com/thiagodp/validator/tree/master/examples/ex3.php))\n- [x] Validates objects (of user-created classes) with private or protected attributes (see  [example 3](https://github.com/thiagodp/validator/tree/master/examples/ex3.php))\n- [x] Supports localized validation messages (different locales)\n- [x] Supports different string formats (UTF, ISO-8859-1, ASCII, etc.)\n\n### Available Rules\n\n- [x] `required`\n- [x] `min_length`\n- [x] `max_length`\n- [x] `length_range`\n- [x] `min_value`\n- [x] `max_value`\n- [x] `value_range`\n- [x] `min_count` (for arrays)\n- [x] `max_count` (for arrays)\n- [x] `count_range` (for arrays)\n- [x] `in` (for arrays)\n- [x] `not_in` (for arrays)\n- [x] `start_with` (accepts a string or an array of strings, compared with \"or\")\n- [x] `not_start_with` (accepts a string or an array of strings, compared with \"or\")\n- [x] `end_with` (accepts a string or an array of strings, compared with \"or\")\n- [x] `not_end_with` (accepts a string or an array of strings, compared with \"or\")\n- [x] `contains` (accepts a string or an array of strings, compared with \"or\")\n- [x] `not_contains` (accepts a string or an array of strings, compared with \"or\")\n- [x] `regex`\n- [x] `format`: allows to use a format (see [Available Formats](#available-formats))\n- [x] `with`: allows to define rules for sub-arrays or sub-objects.\n- [x] **custom**: you can add others easily. See below.\n\n#### Adding a custom rule\n\n```php\n// Adding a custom rule called \"myRule\" in which the value should be zero:\n$validator-\u003esetRule( 'myRule', function( $value ) { return 0 == $value; } );\n```\nNow checking the custom rule:\n```php\n$value = rand( 0, 5 ); // Value to be checked, a random between 0 and 5 (inclusive)\n$rules = array( 'myRule' =\u003e true ); // Rules to be checked. In this example, just \"myRule\".\n$problems = $validator-\u003echeck( $value, $rules ); // check() will return the hurt rules\necho isset( $problems[ 'myRule' ] ) ? 'myRule as hurt' : 'passed';\n```\n\n### Available Formats\n\n- [x] `anything`\n- [x] `string` (same as `anything`)\n- [x] `name`\n- [x] `word`\n- [x] `alphanumeric`\n- [x] `alpha`\n- [x] `ascii`\n- [x] `numeric`\n- [x] `integer`\n- [x] `double`\n- [x] `float` (same as `double`)\n- [x] `monetary`\n- [x] `price` (same as `monetary`)\n- [x] `tax`\n- [x] `date` (equals to `date_dmy`)\n- [x] `date_dmy`\n- [x] `date_mdy`\n- [x] `date_ymd`\n- [x] `time`\n- [x] `longtime`\n- [x] `datetime` (equals to `datetime_dmy`)\n- [x] `datetime_dmy`\n- [x] `datetime_mdy`\n- [x] `datetime_ymd`\n- [x] `longdatetime` (equals to `longdatetime_dmy`)\n- [x] `longdatetime_dmy`\n- [x] `longdatetime_mdy`\n- [x] `longdatetime_ymd`\n- [x] `email`\n- [x] `http`\n- [x] `url`\n- [x] `ip`\n- [x] `ipv4`\n- [x] `ipv6`\n- [x] **custom**: you can add others easily. See below.\n\nYou may specify the separator for date-based formats. Default is \"/\", for example \"31/12/1999\".\n\n#### Adding a custom format\n\n```php\n// Adding a format \"myFormat\" in which the value should start with \"https://\"\n$validator-\u003esetFormat( 'myFormat', function( $value ) {\n\treturn mb_strpos( $value, 'https://' ) === 0;\n\t} );\n```\n\nNow checking the format:\n\n```php\n$value = 'http://non-https-site.com';\n$rules = array( Rule::FORMAT =\u003e 'myFormat' ); // rules to be checked\n$problems = $validator-\u003echeck( $value, $rules ); // check() returns the hurt rules\necho isset( $problems[ Rule::FORMAT ] ) ? 'myFormat as hurt' : 'passed';\n```\n\n### Message Replacements\n\n- [x] `{min_length}` shows the minimum length;\n- [x] `{max_length}` shows the maximum length;\n- [x] `{length_range}` shows the minimum and the maximum length (e.g. \"5-10\");\n- [x] `{min_value}` shows the minimum value;\n- [x] `{max_value}` shows the maximum value;\n- [x] `{value_range}` shows minimum and maximum values (e.g. \"5-10\");\n- [x] `{min_count}` shows the minimum count;\n- [x] `{max_count}` shows the maximum count;\n- [x] `{count_range}` shows the minimum count and the maximum count (e.g. \"5-10\");\n- [x] `{in}` shows the set of items separated by comma;\n- [x] `{not_in}` shows the set of items separated by comma;\n- [x] `{start_with}` shows the string or the set of strings separated by comma;\n- [x] `{not_start_with}` shows the string or the set of strings separated by comma;\n- [x] `{end_with}` shows the string or the set of strings separated by comma;\n- [x] `{not_end_with}` shows the string or the set of strings separated by comma;\n- [x] `{contains}` shows the string or the set of strings separated by comma;\n- [x] `{not_contains}` shows the string or the set of strings separated by comma;\n- [x] `{regex}` shows the defined regex;\n- [x] `{label}` shows the defined label, if defined. Otherwise, shows the array key or object attribute name;\n- [x] `{value}` shows the value.\n\nNotes:\n\n- [x] `{min_value}` and `{max_value}` are available when the `{value_range}` is used;\n- [x] `{min_length}` and `{max_length}` are available when the `{length_range}` is used;\n- [x] `{min_count}` and `{max_count}` are available when the `{count_range}` is used.\n\n### More\n\n- [x] Supports UTF-8 and other common formats (ISO-8859-1, Windows-1251, ASCII, etc.)\n- [x] Error messages and formats can be specified by locale.\n- [x] Error messages and formats can be specified at once. This allows you, for example, read them from a JSON file.\n- [x] Formats and rules can be specified without having to extend any class.\n- [x] Classes use a [fluent interface](https://en.wikipedia.org/wiki/Fluent_interface) (that is, you type less).\n- [ ] Builder classes available (soon)\n\n## Tests\n\n[See here](https://github.com/thiagodp/validator/tree/master/tests).\n\n## Examples\n\n[See all](https://github.com/thiagodp/validator/tree/master/examples)\n\n[ex1.php](https://github.com/thiagodp/validator/tree/master/examples/ex1.php) - Validating values\n\n[ex2.php](https://github.com/thiagodp/validator/tree/master/examples/ex2.php) - Validating arrays\n\n[ex3.php](https://github.com/thiagodp/validator/tree/master/examples/ex3.php) - Validating objects\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiagodp%2Fvalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthiagodp%2Fvalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthiagodp%2Fvalidator/lists"}