{"id":18407521,"url":"https://github.com/simplecomplex/php-validate","last_synced_at":"2025-08-03T11:03:41.913Z","repository":{"id":57051259,"uuid":"92948176","full_name":"simplecomplex/php-validate","owner":"simplecomplex","description":"Shallow and recursive validation; direct methods and combined rules","archived":false,"fork":false,"pushed_at":"2020-11-18T09:59:53.000Z","size":628,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T21:38:59.615Z","etag":null,"topics":["php","php7","psr","validate","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/simplecomplex.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.txt","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":"2017-05-31T13:25:55.000Z","updated_at":"2022-02-12T08:30:33.000Z","dependencies_parsed_at":"2022-08-24T05:11:11.068Z","dependency_job_id":null,"html_url":"https://github.com/simplecomplex/php-validate","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/simplecomplex/php-validate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-validate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-validate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-validate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-validate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simplecomplex","download_url":"https://codeload.github.com/simplecomplex/php-validate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplecomplex%2Fphp-validate/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268531912,"owners_count":24265250,"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","status":"online","status_checked_at":"2025-08-03T02:00:12.545Z","response_time":2577,"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":["php","php7","psr","validate","validation"],"created_at":"2024-11-06T03:14:21.411Z","updated_at":"2025-08-03T11:03:41.854Z","avatar_url":"https://github.com/simplecomplex.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Validate ##\n\nValidate almost any kind of PHP data structure.\n\n#### Simple shallow validation ####\n\nTest a variable against one of ```Validate```s 60+ rule methods\n- is it of a particular type or class?\n- does it's value match a pattern?\n- is it emptyish?\n- a container (array|object|traversable)?\n- a numerically indexed array?\n\n#### Test against a set of rules ####\n\nA validation rule set is a list of rules (```Validate``` methods)  \n– in effect you can combine all the above questions into a single questions.\n\nYou can also declare that \"well if it _isn't_ an [object|string|whatever], then null|false|0 will do just fine\".\n\n#### Validate objects/arrays recursively ####\n\nValidation rule sets can be nested – indefinitely.  \n```$validate-\u003echallenge($subject, $ruleSet)``` traverses the subject according to the rule set (+ descendant rule sets),  \nand checks that the subject at any level/position accords with the rules at that level/position.\n\n#### Record reason(s) for validation failure ####\n\n```$validate-\u003echallengeRecording($subject, $ruleSet)``` memorizes all wrongdoings along the way,  \nand returns a list of violations (including where detected, and what was wrong).\n\n#### Example of use ####\n\n[SimpleComplex Http](https://github.com/simplecomplex/php-http) uses validation rule sets to check the body of HTTP (REST) responses.  \nRule sets are defined in JSON (readable and portable), and cached as PHP ```ValidationRuleSet```s (using [SimpleComplex Cache](https://github.com/simplecomplex/php-cache)).  \n\n### Non-rule flags ###\n\nMulti-dimensional object/array rule sets support a little more than proper validation rules.\n\n- **optional**: the bucket is allowed to be missing\n- **allowNull**: the value is allowed to be null\n- **alternativeEnum**: list of alternative (scalar|null) values that should make a subject pass,  \neven though another rule is violated\n- **tableElements**: the subject (an object|array) must contain these keys,  \nand for every key there's a sub rule set\n- **listItems**: the subject (an object|array) must be a list consisting of a number of buckets,  \nwhich all have the same type/pattern/structure\n\n```alternativeEnum``` is great for defining \"the bucket must be object or null\".  \n\n```tableElements``` and ```listItems``` are allowed to co-exist.  \nRelevant if you have a – kind of malformed – object that may contain list items as well as non-list elements.  \nData originating from XML may easily have that structure (XML stinks ;-)\n\n#### tableElements sub flags ####\n\n- (obj|arr) **rulesByElements** (required): table of rule sets by keys;  \n~ for every key there's a rule set\n- (bool) **exclusive**: the object|array must only contain the keys specified by ```rulesByElements```\n- (arr) **whitelist**: the object|array must – apart from ```rulesByElements``` – only contain these keys\n- (arr) **blacklist**: the object|array must not contain these keys\n\n#### listItems sub flags ####\n\n- (obj|arr) **listItems** (required): a rule set that every list item must comply to\n- (int) **minOccur**: there must be at least that many list items\n- (int) **maxOccur**: guess what\n\n### Example ###\n\n```php\n// We wanna see some bicycles...\nclass Bicycle\n{\n    public $wheels = 0;\n    public $saddle;\n    public $sound = '';\n    public $accessories = [];\n    public $various;\n\n    public function __construct($wheels, $saddle, $sound, $accessories, $various)\n    {\n        $this-\u003ewheels = $wheels;\n        $this-\u003esaddle = $saddle;\n        $this-\u003esound = $sound;\n        $this-\u003eaccessories = $accessories;\n        $this-\u003evarious = $various;\n    }\n}\n\n// Not just any kind of bicycles. They must comply to this rule set:\n$rule_set = [\n    'class' =\u003e [\n        'Bicycle'\n    ],\n    'tableElements' =\u003e [\n        //'exclusive' =\u003e true,\n        //'whitelist' =\u003e ['unspecified_1'],\n        //'blacklist' =\u003e ['saddle', 'accessories'],\n        'rulesByElements' =\u003e [\n            'wheels' =\u003e [\n                'integer',\n                'range' =\u003e [\n                    1,\n                    3\n                ]\n            ],\n            'saddle' =\u003e [\n                'integer',\n                'alternativeEnum' =\u003e [\n                    // Wrongly defined as nested; confused by enum which\n                    // formally requires to be nested.\n                    // But ValidationRuleSet fixes this, as it does with\n                    // the opposite for (un-nested) enum.\n                    [\n                        true,\n                    ]\n                ]\n            ],\n            'sound' =\u003e [\n                'string' =\u003e true,\n                'enum' =\u003e [\n                    // Counterintuitive nesting, but formally correct because\n                    // the allowed values array is second arg to the enum()\n                    // method.\n                    // ValidationRuleSet fixes un-nested instance.\n                    [\n                        'silent',\n                        'swooshy',\n                        'clattering',\n                    ]\n                ]\n            ],\n            'accessories' =\u003e [\n                'array',\n                'tableElements' =\u003e [\n                    'rulesByElements' =\u003e [\n                        'luggageCarrier' =\u003e [\n                            'boolean',\n                            'optional'\n                        ],\n                        'lights' =\u003e [\n                            'numeric',\n                            'optional'\n                        ]\n                    ]\n                ]\n            ],\n            'various' =\u003e [\n                'array',\n                'optional',\n                // allowNull and alternativeEnum with null\n                // are two ways of allowing null.\n                'allowNull' =\u003e true,\n                'alternativeEnum' =\u003e [\n                    null,\n                ],\n                'listItems' =\u003e [\n                    'maxOccur' =\u003e 5,\n                    'itemRules' =\u003e [\n                        'string',\n                        'alternativeEnum' =\u003e [\n                            // Correctly defined as un-nested array.\n                            true,\n                            false,\n                        ]\n                    ]\n                ]\n            ]\n        ]\n    ]\n];\n```\n\n#### Requirements ####\n\n- PHP \u003e=7.0\n- [SimpleComplex Utils](https://github.com/simplecomplex/php-utils)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplecomplex%2Fphp-validate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimplecomplex%2Fphp-validate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplecomplex%2Fphp-validate/lists"}