{"id":13828489,"url":"https://github.com/franzose/kontrolio","last_synced_at":"2025-04-15T18:17:56.046Z","repository":{"id":62507087,"uuid":"60569307","full_name":"franzose/kontrolio","owner":"franzose","description":"Simple standalone data validation library inspired by Laravel and Symfony","archived":false,"fork":false,"pushed_at":"2024-04-23T06:07:49.000Z","size":166,"stargazers_count":51,"open_issues_count":3,"forks_count":5,"subscribers_count":3,"default_branch":"2.x","last_synced_at":"2025-04-15T18:17:47.742Z","etag":null,"topics":["laravel","php","symfony","validation","validator"],"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/franzose.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","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":"2016-06-07T00:17:38.000Z","updated_at":"2022-01-28T03:16:59.000Z","dependencies_parsed_at":"2024-08-04T09:08:39.453Z","dependency_job_id":"ac664980-2141-49ec-81c5-7bb7a41d3dc0","html_url":"https://github.com/franzose/kontrolio","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franzose%2Fkontrolio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franzose%2Fkontrolio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franzose%2Fkontrolio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franzose%2Fkontrolio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/franzose","download_url":"https://codeload.github.com/franzose/kontrolio/tar.gz/refs/heads/2.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249125998,"owners_count":21216705,"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":["laravel","php","symfony","validation","validator"],"created_at":"2024-08-04T09:02:49.108Z","updated_at":"2025-04-15T18:17:56.031Z","avatar_url":"https://github.com/franzose.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"[![](https://img.shields.io/packagist/dt/franzose/kontrolio.svg)](https://packagist.org/packages/franzose/kontrolio)\n![CI](https://github.com/franzose/kontrolio/actions/workflows/ci.yaml/badge.svg)\n\n[На русском](https://github.com/franzose/kontrolio/blob/master/readme_rus.md)\n\n# Kontrolio: keep your data under control.\nKontrolio is a simple standalone data validation library inspired by Laravel and Symfony and compatible with PHP 8.1+.\n[ [Read on Medium](https://medium.com/@franzose/keep-your-data-under-control-530c23e59fb3) ]\n\n## Setting up validator\nThe best way to set up validator:\n \n```php\n// In container unaware environments\n$valid = Factory::getInstance()-\u003emake($data, $rules, $messages)-\u003evalidate();\n\n// Using a service container implementation\n$container-\u003esingleton('validation', static fn() =\u003e new Factory());\n$container-\u003eget('validation')-\u003emake($data, $rules, $messages)-\u003evalidate();\n```\n\nOf course, you can use `Kontrolio\\Validator` class directly but then you'll need to provide available validation rules by yourself:\n```php\n$validator = new Validator($data, $rules, $messages)-\u003eextend($custom)-\u003evalidate();\n```\n\nData here is supposed to be the key-value pairs—attributes and their values:\n\n```php\n$data = [\n    'foo' =\u003e 'bar',\n    'bar' =\u003e 'baz',\n    'baz' =\u003e 'taz'\n];\n```\n\nValidation rules can be set in three different formats:\u003cbr\u003e\n1. Laravel-like string\u003cbr\u003e\n2. Instances of the class based rules\u003cbr\u003e\n3. Callables (closures or callbacks)\n\nYou can also mix instances and callables when you set multiple validation rules to a single attribute. Here's a simple example:\n\n```php\n$rules = [\n    'one' =\u003e 'not_empty|length:5,15',\n    'two' =\u003e new Email,\n    'three' =\u003e static fn ($value) =\u003e $value === 'taz',\n    'four' =\u003e [\n        static fn ($value) =\u003e is_numeric($value),\n        new GreaterThan(5),\n    ]\n];\n```\n\nWhen you set validation rules as string, validator will parse it to an ordinary array of rules before applying them to the attribute, so when you write `'some' =\u003e 'not_empty|length:5,15'`, it becomes\n\n```php\n'some' =\u003e [\n    new NotEmpty,\n    new Length(5, 15)\n]\n```\n\nIt's pretty straightforward but remember that all arguments you pass after semicolon (separating them by commas) become the arguments of the validation rule constructor.\n\nWhen you set validation rule as callback, internally it is wrapped by special object called `Kontrolio\\Rules\\CallbackRuleWrapper` to keep consistency defined by `Kontrolio\\Rules\\RuleInterface` interface.\n\n## Rule options\n### Allowing empty value\nA single rule validation can be skipped when the validated attribute value is empty. If you feel that you need this option, you instantiate a rule using named constructor `allowingEmptyValue()` or by calling `allowEmptyValue()` method on the already existing instance:\n\n```php\n'some' =\u003e [\n    MyRule::allowingEmptyValue(),\n    // (new MyRule())-\u003eallowEmptyValue()\n]\n```\n\n### Skipping rule validation\nWhen creating a new custom class based rule you might need an option to skip its validation based on some conditions. You can define the behavior using `canSkipValidation()` method:\n\n```php\n\nclass MyRule extends AbstractRule\n{\n    public function canSkipValidation($input = null)\n    {\n        return $input === 'bar';\n    }\n\n    // ...\n}\n\n```\n## Callable rules\nCallable rule is nothing more than a closure or function that takes an attribute value and returns either boolean result of the validation or an options array equivalent to options provided by a class based validation rule:\n\n```php\n    'foo' =\u003e static fn ($value) =\u003e is_string($value),\n    'bar' =\u003e static function ($value) {\n        return [\n            // required when array\n            'valid' =\u003e $value === 'taz',\n            \n            // optionals\n            'name' =\u003e 'baz', // rule identifier\n            'empty_allowed' =\u003e true, // allowing empty value\n            'skip' =\u003e false // don't allow skipping current rule validation,\n            'violations' =\u003e [] // rule violations\n        ];\n    }\n```\n\n## Custom rules\nOf course you can create your custom rules. Just remember that each rule must be an instance of `Kontrolio\\Rules\\RuleInterface`, implement `isValid()` method and have an identifier. By default, identifiers are resolved by `Kontrolio\\Rules\\AbstractRule` and are based on the rule class name without namespace. However, you can override this behavior if you wish overriding `getName()` method.\n\nCustom rules can be added eighter via factory or validator itself:\n\n```php\n$factory = (new Factory())-\u003eextend([CustomRule::class]);\n\n// with a custom identifier\n$factory = (new Factory())-\u003eextend(['some_custom' =\u003e CustomRule::class]);\n$validator = $factory-\u003emake([], [], []);\n\n// if you don't use factory\n$validator = new Validator([], [], []);\n$validator-\u003eextend([CustomRule::class]);\n\n// with a custom identifier\n$validator-\u003eextend(['custom' =\u003e CustomRule::class]);\n$validator-\u003evalidate();\n```\n\n## Bypassing attribute's value validation completely\nIt's not the same as using `allowEmptyValue()` or `canSkipValidation()` on a rule. With those you can skip _only a rule_. But you can also bypass a whole attribute by using `Kontrolio\\Rules\\Core\\Sometimes` rule. `Sometimes` tells validator to bypass validation when the value of the attribute is null or empty. That's all. You can prepend `Sometimes` to the attribute's rules array or use its identifier in a ruleset string:\n\n```php\n$rules = [\n    'one' =\u003e 'sometimes|length:5,15',\n    // 'one' =\u003e [\n    //     new Sometimes(),\n    //     new Length(5, 15)\n    // ]\n];\n```\n\n## Stopping validation on the first failure\nYou can tell the validator to stop validation if any validation error occurs:\n```php\n$validator-\u003eshouldStopOnFailure()-\u003evalidate();\n```\n\n## Stopping certain attribute's validation on the first failure\nUsing `UntilFirstFailure` validation rule, you can also stop validation of a single attribute while keeping validation as a whole:\n```php\n$data = [\n    'attr' =\u003e '',\n    'attr2' =\u003e 'value2'\n];\n\n$rules = [\n    'attr' =\u003e [\n        new UntilFirstFailure(),\n        new NotBlank(),\n        new NotFooBar()\n    ]\n];\n\n$messages = [\u003c...\u003e];\n\n$validator = new Validator($data, $rules, $messages)-\u003evalidate();\n```\nNow when `attr` fail with `NotBlank` rule, its validation will be stopped and validator will proceed to the `attr2`.\n\n## Error messages and rule violations\nError messages has a single simple format you'll love:\n\n```php\n$messages = [\n    'foo' =\u003e 'Foo cannot be null',\n    'foo.length' =\u003e 'Wrong length of foo',\n    'foo.length.min' =\u003e 'Foo is less than 3'\n    // '[attribute].[rule].[violation]\n];\n```\nEvery message key can have three segments separated by dot. They are:\u003cbr\u003e\n1. Attribute name\u003cbr\u003e\n2. Validation rule identifier\u003cbr\u003e\n3. Validation rule _violation_\n\nWith these options you can customize messages from the most general to the most specific. Each violation is set by the rule validating the attribute value. So when writing your own validation rule you may provide your own violations to provide customizability of the validation result and error messages.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffranzose%2Fkontrolio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffranzose%2Fkontrolio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffranzose%2Fkontrolio/lists"}