{"id":19196988,"url":"https://github.com/jitesoft/php-validator","last_synced_at":"2026-06-11T23:31:02.970Z","repository":{"id":31080046,"uuid":"126807159","full_name":"jitesoft/php-validator","owner":"jitesoft","description":"Mirror for the validator composer package.","archived":false,"fork":false,"pushed_at":"2023-08-28T17:36:05.000Z","size":134,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-23T04:45:20.474Z","etag":null,"topics":["hacktoberfest","php","php7","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/jitesoft.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":"2018-03-26T09:42:34.000Z","updated_at":"2022-06-17T10:05:45.000Z","dependencies_parsed_at":"2023-01-14T18:17:53.829Z","dependency_job_id":null,"html_url":"https://github.com/jitesoft/php-validator","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/jitesoft/php-validator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jitesoft%2Fphp-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jitesoft%2Fphp-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jitesoft%2Fphp-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jitesoft%2Fphp-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jitesoft","download_url":"https://codeload.github.com/jitesoft/php-validator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jitesoft%2Fphp-validator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34222709,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-11T02:00:06.485Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["hacktoberfest","php","php7","validation"],"created_at":"2024-11-09T12:15:05.893Z","updated_at":"2026-06-11T23:31:02.944Z","avatar_url":"https://github.com/jitesoft.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Validator\n\nA package for validation of misc values.\n\nObserve that this is a pre-release, changes could be made to the base API before the first real release that breaks\nany implementation done.  \nIf using the validation package before version 1.0.0, be sure to se the constraints on minor version, for example:\n\n`\"jitesoft/validator\": \"0.1.*\"`\n\n## Usage\n\nInstall package:\n```text\n$\u003e composer require jitesoft/validator\n```\n\nWhen creating a validator, the available rules need to be set in the constructor. That way the validator knows which\nrules it can use and can create them when needed.\n\n```php\n$validator = new Validator(\n  Email::class,\n  Text::class\n);\n```\n\nThere are multiple ways to validate data, the following snippets are all valid:\n\nSingle validation with single rule:\n\n```php\n$validator-\u003evalidate('text', 'abc123');\n```\n\nSingle validation with deep rules:\n\n```php\n$validator-\u003evalidate(['text' =\u003e ['pattern' =\u003e '/?.*/' ], 'abc123');\n```\n\nMultiple rules and values in indexed arrays:\n\n```php\n$validator-\u003evalidate([\n    'text',\n    'number'\n], [\n    'abc',\n    123\n]);\n```\n\nMultiple deep rules with indexed array of values:\n\n```php\n$validator-\u003evalidate([\n    'text' =\u003e [\n        'pattern' =\u003e '/?.*/'\n    ],\n    'number' =\u003e [\n        'max' =\u003e 200\n    ]\n], [\n    'abc',\n    123\n]);\n```\n\nNamed tests with named values:\n\n```php\n$validator-\u003evalidate([\n    'first' =\u003e [ \n        'text' =\u003e [ \n            'pattern' =\u003e '/?.*/' \n        ]\n    ],\n    'second' =\u003e 'number'\n], [\n  'second' =\u003e 123,\n  'first' =\u003e 'abc123'\n]);\n```\n\nNamed test on indexed array.\n\n```php\n$validator-\u003evalidate([\n    'first' =\u003e [ \n        'text' =\u003e [ \n            'pattern' =\u003e '/?.*/' \n        ]\n    ],\n    'second' =\u003e 'number'\n], [ 'abc', 123 ]);\n```\n\nThe returned value is a boolean which is true if the data is valid or false if it is not.  \nIf false, the `$validator-\u003egetErrors()` method will return the errors of the validation tests.\n\nBy default, the validator will not throw any exceptions. But if this is wanted, set the `$throw` argument of the\nvalidator constructor to true, and it will throw `ValidationException` instead of returning false.\n\n## The idea\n\nThe idea of the validation structure is that each validation rule can have a set of sub-rules.\nThe sub-rule in turn can have other sub-rule.  \nWhen implementing rules it's important to make sure that the validators don't create a never ending loop of self\nreferences.\n\n```mermaid\ngraph TD;\n\nTextRule-- optional --\u003eLengthRule\nLengthRule-- optional --\u003eMoreThanRule;\nLengthRule-- optional --\u003eLessThanRule;\nTextRule-- optional --\u003ePatternRule;\n```\n\n```php\n$validator-\u003evalidate([\n  'Test' =\u003e [\n    'text' =\u003e [\n      'length' =\u003e [\n        'max' =\u003e 5,\n        'min' =\u003e 2\n      ],\n      'pattern' =\u003e '/abc(\\d{3})/s'\n    ]\n], 'abc123');\n```\n\nWhen a validator errors out, it should fetch the error from the given rules and depending on the `throw` param either\njust store it in a error or throw a validation exception.\n\n## Currently implemented rules\n\nFollowing is a graph of implemented rules and their sub-rule relations.\nRule names are the actual names, not class names. Check the `src/Rules` directory for class names.\n\n```mermaid\ngraph TD;\n\nboolean\n\ntext--\u003eemail\ntext--\u003elength\ntext--\u003epattern\n\nemail--\u003elength\nemail--\u003epattern\n\nlength--\u003emax\nlength--\u003emin\n\nnumber--\u003emin\nnumber--\u003emax\n\nfloat--\u003emin\nfloat--\u003emax\n\nint--\u003emin\nint--\u003emax\n```\n\n## Changes\n\n### 0.1.0\n\n#### Validation calls\n\nUpdated validation function call. It now takes rules as first argument and values as second.  \nThe rules can either be a single string to test a single value, an array of strings testing an array of values or\nan associative array with Test names which defines rules and tests either a list of values or a matching assoc array of values.\n\n\n#### Errors\n\nThe errors are now associative arrays which have the 'test name' as a key then each rules and their errors as a key-value pair\nunder each test name.  \nThe rules and errors are flattened, so the depth is always the same:\n\n```php\n$errors = [\n  'TestName1' =\u003e [\n    'text' =\u003e 'some error'\n  ],\n  'TestName2' =\u003e [\n    'min' =\u003e 'Some error',\n    'max' =\u003e 'Some error',\n    'pattern' =\u003e 'Some other error.'\n  ]\n]\n```\n\nInput is wanted on the flattened array. Should it be flattened or should it have a new depth for each rule name?\n\n\n## License\n\nMIT.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjitesoft%2Fphp-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjitesoft%2Fphp-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjitesoft%2Fphp-validator/lists"}