{"id":43406539,"url":"https://github.com/tpg/yerp","last_synced_at":"2026-02-02T16:00:42.144Z","repository":{"id":190838905,"uuid":"683458888","full_name":"tpg/yerp","owner":"tpg","description":"Class property validation tool for PHP.","archived":false,"fork":false,"pushed_at":"2023-08-29T07:28:12.000Z","size":35,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-02T12:38:48.916Z","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/tpg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-08-26T16:33:14.000Z","updated_at":"2023-08-26T16:33:58.000Z","dependencies_parsed_at":"2023-08-26T17:23:49.108Z","dependency_job_id":"eba0ce5e-674e-4f6a-8a2b-5818ff1d38cb","html_url":"https://github.com/tpg/yerp","commit_stats":null,"previous_names":["tpg/yerp"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tpg/yerp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpg%2Fyerp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpg%2Fyerp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpg%2Fyerp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpg%2Fyerp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tpg","download_url":"https://codeload.github.com/tpg/yerp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tpg%2Fyerp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29015071,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-02T14:58:54.169Z","status":"ssl_error","status_checked_at":"2026-02-02T14:58:51.285Z","response_time":58,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":[],"created_at":"2026-02-02T16:00:19.457Z","updated_at":"2026-02-02T16:00:42.138Z","avatar_url":"https://github.com/tpg.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Run Tests](https://github.com/tpg/yerp/actions/workflows/tests.yml/badge.svg)](https://github.com/tpg/yerp/actions/workflows/tests.yml)\n\n# Yerp\n\nYerp is an object property validation library for PHP. It was written to provide simply validation for some other libraries, but it may have some value elsewhere.\n\nYerp uses PHP Attributes to set validation rules on public properties. You can then pass an instance of that object to the validator and get a result. Results are are boolean, so there's no translation or language requirements. You can specify your own messages based on the result.\n\n## Installation\n\n```\ncomposer require thepublicgood/yerp\n```\n\n## Usage\n\nEach validation rule is a different attribute class in the `TPG\\Yerp\\Rules` namespace.\n\nHere's a quick example:\n\n```php\nuse TPG\\Yerp\\Rules;\n\nclass User\n{\n    #[Rules\\Required]\n    public string $firstName;\n    #[Rules\\Nullable]\n    public ?string $lastName = null;\n    #[Rules\\Required, Rules\\Email]\n    public string $emailAddress;\n    #[Rules\\Boolean(true)]\n    public boolean $active;\n}\n```\n\nThere are a number of built-in rules and you can easily add your own.\n\n\u003e Note that only public properties can validated.\n\n## Running the validator\n\nTo validate an object, pass it to the `Validator` class and call the `validate` method. This will return an instance of `Validated` which provides access to the results of each rule on each property.\n\n```php\nclass UserController\n{\n    public function create()\n    {\n        // Create a new object instance\n        $user = new User();\n        $user-\u003efirstName = 'John';\n        $user-\u003elastName = 'Doe';\n        $user-\u003eemailAddress = 'john.doe@example.com';\n        $user-\u003eactive = false;\n\n        // Pass to the validator\n        $validated = (new Validator($user))-\u003evalidate();\n    }\n}\n```\n\n## Getting the results\n\nThe `Validated` class provides a `property` method which you can use to get to the result of a particular property. The `property` method returns an instance of `Result`. You can use the provided `passed` method to test the result of a given property:\n\n```php\n$validated = (new Validator($user))-\u003evalidate();\n\n$validated-\u003eproperty('firstName')-\u003epassed();\n```\n\nThe `passed` method returns a boolean representing the validation result of that property. There is also a `failed` method which returns the exact opposite:\n\n```php\n$validated-\u003eproperty('firstName')-\u003efailed();\n```\n\nIf you want to know the result of a specific rule on the property, you can pass the class name of the rule to the `property` method:\n\n```php\n$validated-\u003eproperty('firstName', Rule\\Required::class)-\u003epassed();\n```\n\n## Stopping on the first error\n\nAll validation rules will be evaluated in the order they appear. You can tell Yerp to stop validating the rest of the rules if a specific one fails. You can do this by passing `true` to the `last` property on any rule:\n\n```php\n#[Rule\\Nullable, Rules\\Email(last: true), Rule\\Equal('test@example.com')]\npublic string $emailAddress;\n```\n\nIn the example above, if the `Email` rule fails, then the `Equal` rule will not be evaluated. If the `Email` rule DOES pass, then the `Equal` rule will be evaluated and will be present in the results. If you end the evaluation by using `last`, then any rules that come later will not be included in the results set. This is important because if you try to check the outcome of a rule that doesn't exist in the results, you'll get an `InvalidRuleException`.\n\n## Setting messages per rule\n\nAlthough Yerp is designed to be translation agnostic, it can be handy to set a static message. You can do so by passing a string value to the `failure` property of any rule:\n\n```php\n#[Rules\\Email(failure: 'Must be a valid email address')]\npublic string $email;\n```\n\nWhen this rule evaluates, you'll be able to use the `message` method on the `Result` class:\n\n```php\n$validated-\u003eproperty('email', Rules\\Email::class)-\u003emessage();\n```\n\nIf the rule passes, or no message is set, then the `message` method will return `null`. By default, the message is only returned when the rule fails. However, you can specify a success message if you like by passing a string to the `success`. property:\n\n```php\n#[Rules\\Email(\n    success: 'The email address is valid!',    // Success\n    failure: 'Must be a valid email address',  // Failed\n)]\npublic string $email;\n```\n\n## Available rules\n\n### Required\n\nThe `Required` rule is handy to validate a property that must be present. A \"required\" property must contain a value, cannot be null and cannot be empty. An empty array, or an empty string would fail:\n\n```php\n#[Rules\\Required]\nproperty ?string $someString;\n```\n\n### Alpha\n\nThe `Alpha` rule ensures that the value of the property contains only letters.\n\n```php\n#[Rules\\Alpha]\nproperty string $someString;\n```\n\n### ArrayKey\n\nThe `ArrayKey` rule simply ensures that the specified key exists in the array:\n\n```php\n#[Rules\\ArrayKey('test')]\nproperty array $someArray;\n```\n\n### Boolean\n\nThe `Boolean` rule allows you to validate that the property is either TRUE or FALSE:\n\n```php\n#[Rules\\Boolean(true)]\nproperty bool $mustBeTrue;\n```\n\n### Equality\n\nYou can ensure that a property is equal to a specific value. Simply pass the required value as the first parameter of the `Euqal` rule:\n\n```php\n#[Rules\\Equal('expected')]\npropert ?string $someString;\n```\n\nThere is also an opposing `NotEqual` rule.\n\n### Email\n\nA common validation rule is to ensure that a string is a valid email address:\n\n```php\n#[Rules\\Email]\nproperty string $emailAddress;\n```\n\n### In\n\nThe `In` rule allows you to test if a value is one of the specified values:\n\n```php\n#[Rules\\In(['a', 'b', 'c'])]\nproperty string $someString;\n```\n\n### Length\n\nThe `Length` rule allows you to specify a minimum and/or maximum length. If the property type is an array, then length validates the number of array elements:\n\n```php\n#[Rules\\Length(min: 5, max: 20)]\nproperty string $someString;\n```\n\nYou don't need to specify both, but you must specify at least one value.\n\n### Numeric\n\nThe `Numeric` rule ensures that the property is parsable as a number:\n\n```php\n#[Rules\\Numeric]\nproperty string $someString;\n```\n\n### Regex\n\nYou can pass a regex string as the first parameter of the `Regex` rule:\n\n```php\n#[Rules\\Regex('/^test$/')]\nproperty string $regexString;\n```\n\n## Writing new rules\n\nYerp only provides a small number of rules. This was mainly because they're the only ones we needed at time. We might add new rules as we need them more often, but it's simple to add your own rules without needing to ask.\n\nCreate a new class to contain your rule logic extending the `TPG\\Yerp\\Rules\\AbstractRule` class and add  `#[Attribute]` to the class definition.\n\nYou'll need to implement the required `validate` method which returns a `TPG\\Yerp\\Result`. You can use the `getResult` method, which takes a boolean value, to return a new `Result` instance:\n\n```php\nnamespace CustomRules;\n\nuse Attribute;\nuse TPG\\Yerp\\Rules\\AbstractRule;\nuse TPG\\Yerp\\Result;\n\n#[Attribute]\nclass HyphenatedRule extends AbstractRule\n{\n    public function validate(mixed $value): Result\n    {\n        return $this-\u003egetResult(str_contains((string)$value, '-'));\n    }\n}\n```\n\nNow you can use your new rule in any class:\n\n```php\n\nuse CustomRules\\Hyphenated;\n\nclass Article\n{\n    public string $title;\n    #[HyphenatedRule]\n    public string $slug;\n}\n```\n\nYour new rule gets the same `last`, `success` and `failure` parameters. If your rule needs to accept a value, simply specify it in a constructor:\n\n```php\nnamespace CustomRules;\n\nuse Attribute;\nuse TPG\\Yerp\\Rules\\AbstractRule;\nuse TPG\\Yerp\\Result;\n\nuse DelimitedRule extends AbstractRule\n{\n    public function __construct(protected string $delimiter = ',')\n    {\n    }\n\n    public function validate(string $value): Result\n    {\n        return $this-\u003egetResult(str_contains($value, $this-\u003edelimited));\n    }\n}\n```\n\n## Credits\n\n- [Warrick Bayman](https://github.com/warrickbayman)\n\n## License\n\nThe MIT License (MIT). See the [LICENSE.md]() file for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftpg%2Fyerp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftpg%2Fyerp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftpg%2Fyerp/lists"}