{"id":33980919,"url":"https://github.com/monooso/apposite","last_synced_at":"2025-12-13T03:31:31.709Z","repository":{"id":35126733,"uuid":"210049916","full_name":"monooso/apposite","owner":"monooso","description":"Conditionally apply Laravel validation rules, even when you don't have access to the validator instance.","archived":false,"fork":false,"pushed_at":"2025-11-26T21:58:13.000Z","size":558,"stargazers_count":34,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"4.x","last_synced_at":"2025-11-27T15:30:27.750Z","etag":null,"topics":["laravel","laravel-package","php","validation"],"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/monooso.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-21T20:33:23.000Z","updated_at":"2025-11-26T21:57:32.000Z","dependencies_parsed_at":"2023-02-17T11:45:37.092Z","dependency_job_id":null,"html_url":"https://github.com/monooso/apposite","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/monooso/apposite","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monooso%2Fapposite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monooso%2Fapposite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monooso%2Fapposite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monooso%2Fapposite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/monooso","download_url":"https://codeload.github.com/monooso/apposite/tar.gz/refs/heads/4.x","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monooso%2Fapposite/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27699628,"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-12-13T02:00:09.769Z","response_time":147,"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":["laravel","laravel-package","php","validation"],"created_at":"2025-12-13T03:31:30.840Z","updated_at":"2025-12-13T03:31:31.694Z","avatar_url":"https://github.com/monooso.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Apposite\n\n\u003cp\u003e\n  \u003cimg src=\"https://github.com/monooso/unobserve/actions/workflows/lint-and-test.yml/badge.svg\" alt=\"Lint and Test Status\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://scrutinizer-ci.com/g/monooso/apposite\"\u003e\u003cimg src=\"https://img.shields.io/scrutinizer/g/monooso/apposite.svg\" alt=\"Quality Score\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://scrutinizer-ci.com/g/monooso/apposite\"\u003e\u003cimg src=\"https://img.shields.io/scrutinizer/coverage/g/monooso/apposite.svg\" alt=\"Coverage\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://packagist.org/packages/monooso/apposite\"\u003e\u003cimg src=\"https://poser.pugx.org/monooso/apposite/v/stable.svg\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://packagist.org/packages/monooso/apposite\"\u003e\u003cimg src=\"https://poser.pugx.org/monooso/apposite/license.svg\" alt=\"License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## About Apposite\nApposite makes it easy to conditionally apply Laravel validation rules, even when you don't have access to [the validator instance](https://laravel.com/docs/validation#conditionally-adding-rules).\n\n## Requirements and installation\nSelect the appropriate branch for your version of Laravel.\n\n| Branch | Laravel Versions | PHP Version |\n|:-------|:-----------------|:------------|\n| 1.x    | `^6.0`           | `^7.2`      |\n| 2.x    | `^7.0`           | `^7.2.5`    |\n| 3.x    | `^8.0`           | `^7.3`      |\n| 4.x    | `^8.0`           | `^8.0`      |\n\nInstall Apposite using [Composer](https://getcomposer.org/):\n\n```bash\ncomposer require monooso/apposite\n```\n\n## Usage\nApposite provides three [custom Laravel validation rules](https://laravel.com/docs/8.x/validation#using-rule-objects):\n\n- [`ApplyWhen`](#apply-when)\n- [`ApplyUnless`](#apply-unless)\n- [`ApplyMap`](#apply-map)\n\n### `ApplyWhen` \u003ca name=\"apply-when\"\u003e\u003c/a\u003e\nUse `ApplyWhen` to apply one or more validation rules when a condition is met. For example, validate the `email` field if the `contact_method` is \"email\".\n\nThe `ApplyWhen` constructor expects two arguments:\n\n- A conditional, which determines whether the validation rules are applied. This may be a boolean value, or a closure which returns a boolean.\n- The validation rules to apply if the conditional evaluates to `true`. The may be in [any format](https://laravel.com/docs/8.x/validation#quick-writing-the-validation-logic) recognised by the Laravel validator.\n\nFor example:\n\n```php\nnew ApplyWhen($foo === $bar, 'required|string|min:10');\n\nnew ApplyWhen(function () {\n    return random_int(1, 10) % 2 === 0;\n}, ['required', 'string', 'min:10']);\n```\n\nAdd the `ApplyWhen` rule to your validation rules array in the normal way.\n\n```php\npublic function store(Request $request)\n{\n    $rules = [\n        'contact_method' =\u003e ['required', 'in:email,phone'],\n        'email'          =\u003e [\n            new ApplyWhen($request-\u003econtact_method === 'email', ['required', 'email', 'max:255']),\n        ],\n    ];\n\n    $validated = $this-\u003evalidate($rules);\n}\n```\n\n### `ApplyUnless` \u003ca name=\"apply-unless\"\u003e\u003c/a\u003e\n`ApplyUnless` is the opposite of `ApplyWhen`. Use it to apply one or more validation rules when a condition _is not_ met.\n\nFor example:\n\n```php\npublic function store(Request $request)\n{\n    $rules = [\n        'contact_method' =\u003e ['required', 'in:email,phone'],\n        'email'          =\u003e [\n            new ApplyUnless($request-\u003econtact_method === 'phone', ['required', 'email', 'max:255']),\n        ],\n    ];\n\n    $validated = $this-\u003evalidate($rules);\n}\n```\n\nRefer to the [`ApplyWhen`](#apply-when) documentation for full usage instructions.\n\n### `ApplyMap` \u003ca name=\"apply-map\"\u003e\u003c/a\u003e\nUse `ApplyMap` when you need to choose between different sets of validation rules. For example, when validating that the chosen `delivery_service` is offered by the chosen `delivery_provider`.\n\n```php\npublic function store(Request $request)\n{\n    $rules = [\n        'delivery_provider' =\u003e ['required', 'in:fedex,ups,usps'],\n        'delivery_service'  =\u003e [\n            'required',\n            new ApplyMap($request-\u003edelivery_provider, [\n                'fedex' =\u003e 'in:one_day,two_day',\n                'ups'   =\u003e 'in:overnight,standard',\n                'usps'  =\u003e 'in:two_day,someday',\n            ]),\n        ],\n    ]; \n\n    $validated = $this-\u003evalidate($rules);\n}\n```\n\nThe `ApplyMap` constructor expects two arguments:\n\n- The \"key\" value, which determines which rules to apply (if any). For example, \"fedex\".\n- A \"map\" of keys to validation rules. The validation rules may be in any format recognised by the Laravel validator.\n\n## License\nApposite is open source software, released under [the MIT license](https://github.com/monooso/apposite/blob/stable/LICENSE.txt).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmonooso%2Fapposite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmonooso%2Fapposite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmonooso%2Fapposite/lists"}