Skip to content

[12.x] Introduce Rule::anyOf() for Validating Against Multiple Rule Sets #55191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
20cc1a6
[12.x] introduce `Rule::oneOf()` (#https://github.com/laravel/framewo…
brianferri Mar 8, 2025
fe882b3
chore: apply styleCI
brianferri Mar 8, 2025
2aa5ef9
feat: add nested oneOf validation test
brianferri Mar 8, 2025
e9d7b83
chore: apply styleCI
brianferri Mar 8, 2025
a274de9
refactor: rename `oneof` into `anyof` to fit implementation
brianferri Mar 13, 2025
ed65fa2
fix: wrong failure message
brianferri Mar 13, 2025
735a8f6
feat: update base tests
brianferri Mar 13, 2025
5566984
feat: add test case
brianferri Mar 13, 2025
2df37b9
chore: apply styleCI
brianferri Mar 13, 2025
a19e327
formatting
taylorotwell Mar 17, 2025
f81e81c
feat: allow string fields
brianferri Mar 18, 2025
b504457
feat: add test and clean nested rules
brianferri Mar 19, 2025
c8c2147
chore: apply styleCI
brianferri Mar 19, 2025
61ad096
failing test
taylorotwell Mar 22, 2025
5c532bf
Validation tests (#1)
brianferri Mar 23, 2025
25ef030
chore: correspond with recent changes https://github.com/brianferri/f…
brianferri Mar 24, 2025
d1c8daf
chore: remove unused private property
brianferri Mar 24, 2025
14598f6
feat: attribute mapping in favor of potentially indexed mapping
brianferri Mar 26, 2025
e15e385
feat: add more tests
brianferri Mar 27, 2025
396e2a2
refactor(tests): remove unnecessary amount of tests, rename parameter…
brianferri Mar 29, 2025
76353fa
Merge remote-tracking branch 'upstream/12.x' into 12.x
brianferri Mar 29, 2025
49869ed
chore: apply styleCI
brianferri Mar 29, 2025
81817ed
feat: add tests to verify compliance with dot notation nesting validator
brianferri Mar 29, 2025
5630b32
formatting
taylorotwell Apr 1, 2025
a40b9d3
fix: remove messages
brianferri Apr 1, 2025
9366e99
fix(wip): regression introduced in 14598f62bf8305bbe2a94ee4e2848d6ec2…
brianferri Apr 1, 2025
d0edafb
feat: implement star rule counter tests for simple and nested rules
brianferri Apr 7, 2025
94b46ab
chore: apply styleCI
brianferri Apr 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Illuminate/Translation/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'alpha' => 'The :attribute field must only contain letters.',
'alpha_dash' => 'The :attribute field must only contain letters, numbers, dashes, and underscores.',
'alpha_num' => 'The :attribute field must only contain letters and numbers.',
'any_of' => 'The :attribute field is invalid.',
'array' => 'The :attribute field must be an array.',
'ascii' => 'The :attribute field must only contain single-byte alphanumeric characters and symbols.',
'before' => 'The :attribute field must be a date before :date.',
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Validation\Rules\AnyOf;
use Illuminate\Validation\Rules\ArrayRule;
use Illuminate\Validation\Rules\Can;
use Illuminate\Validation\Rules\Date;
Expand Down Expand Up @@ -246,6 +247,19 @@ public static function numeric()
return new Numeric;
}

/**
* Get an "any of" rule builder instance.
*
* @param array
* @return \Illuminate\Validation\Rules\AnyOf
*
* @throws \InvalidArgumentException
*/
public static function anyOf($rules)
{
return new AnyOf($rules);
}

/**
* Compile a set of rules for an attribute.
*
Expand Down
94 changes: 94 additions & 0 deletions src/Illuminate/Validation/Rules/AnyOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Illuminate\Validation\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
use InvalidArgumentException;

class AnyOf implements Rule, ValidatorAwareRule
{
/**
* The rules to match against.
*
* @var array
*/
protected array $rules = [];

/**
* The validator performing the validation.
*
* @var \Illuminate\Validation\Validator
*/
protected $validator;

/**
* Sets the validation rules to match against.
*
* @param Illuminate\Contracts\Validation\ValidationRule[][] $rules
*
* @throws \InvalidArgumentException
*/
public function __construct($rules)
{
if (! is_array($rules)) {
throw new InvalidArgumentException('The provided value must be an array of validation rules.');
}

$this->rules = $rules;
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
foreach ($this->rules as $rule) {
$validator = Validator::make(
Arr::isAssoc(Arr::wrap($value)) ? $value : [$value],
Arr::isAssoc(Arr::wrap($rule)) ? $rule : [$rule],
$this->validator->customMessages,
$this->validator->customAttributes
);

if ($validator->passes()) {
return true;
}
}

return false;
}

/**
* Get the validation error messages.
*
* @return array
*/
public function message()
{
$message = $this->validator->getTranslator()->get('validation.any_of');

return $message === 'validation.any_of'
? ['The :attribute field is invalid.']
: $message;
}

/**
* Set the current validator.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return $this
*/
public function setValidator($validator)
{
$this->validator = $validator;

return $this;
}
}
Loading
Loading