|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: (c) Respect Project Contributors |
| 3 | +SPDX-License-Identifier: MIT |
| 4 | +--> |
| 5 | + |
| 6 | +# Composite |
| 7 | + |
| 8 | +- `Composite()` |
| 9 | +- `Composite(Validator ...$validators)` |
| 10 | + |
| 11 | +Consolidates zero or more validators into a single validator. |
| 12 | + |
| 13 | +- When no validators are provided, it acts as [AlwaysValid](AlwaysValid.md). |
| 14 | +- When a single validator is provided, it acts as a pass-through. |
| 15 | +- When multiple validators are provided, they are combined using [AllOf](AllOf.md), meaning all validators must pass for the input to be considered valid. |
| 16 | + |
| 17 | +```php |
| 18 | +v::composite()->assert('anything'); |
| 19 | +// Validation passes successfully |
| 20 | + |
| 21 | +v::composite(v::intType())->assert(42); |
| 22 | +// Validation passes successfully |
| 23 | + |
| 24 | +v::composite(v::intType(), v::positive(), v::lessThan(100))->assert(42); |
| 25 | +// Validation passes successfully |
| 26 | + |
| 27 | +v::composite(v::intType(), v::positive())->assert(-5); |
| 28 | +// → -5 must be a positive number |
| 29 | +``` |
| 30 | + |
| 31 | +## Use cases |
| 32 | + |
| 33 | +This validator is useful for dynamically building validation chains or when you |
| 34 | +have a variable number of validators that need to be applied together. |
| 35 | + |
| 36 | +```php |
| 37 | +$validators = [v::stringType(), v::notSpaced()]; |
| 38 | + |
| 39 | +if (true) { // some condition |
| 40 | + $validators[] = v::email(); |
| 41 | +} |
| 42 | + |
| 43 | +v::composite(...$validators)->assert('respectpanda#example.com'); |
| 44 | +// → "respectpanda#example.com" must be a valid email address |
| 45 | +``` |
| 46 | + |
| 47 | +## Categorization |
| 48 | + |
| 49 | +- Composite |
| 50 | +- Nesting |
| 51 | + |
| 52 | +## Changelog |
| 53 | + |
| 54 | +| Version | Description | |
| 55 | +| ------: | :---------- | |
| 56 | +| 3.0.0 | Created | |
| 57 | + |
| 58 | +## See Also |
| 59 | + |
| 60 | +- [AllOf](AllOf.md) |
| 61 | +- [AlwaysValid](AlwaysValid.md) |
| 62 | +- [AnyOf](AnyOf.md) |
| 63 | +- [NoneOf](NoneOf.md) |
| 64 | +- [OneOf](OneOf.md) |
0 commit comments