8000 [12.x] Introduce Rule::anyOf() for Validating Against Multiple Rule S… · laravel/framework@08960db · GitHub
[go: up one dir, main page]

Skip to content

Commit 08960db

Browse files
brianferritaylorotwellchristianascone
authored
[12.x] Introduce Rule::anyOf() for Validating Against Multiple Rule Sets (#55191)
* [12.x] introduce `Rule::oneOf()` (##54880) * chore: apply styleCI * feat: add nested oneOf validation test * chore: apply styleCI * refactor: rename `oneof` into `anyof` to fit implementation * fix: wrong failure message * feat: update base tests * feat: add test case * chore: apply styleCI * formatting * feat: allow string fields * feat: add test and clean nested rules * chore: apply styleCI * failing test * Validation tests (#1) * feat: add more validation tests * wip: add failing test * wip: add basic string rule validations * chore: rename object fields for better debugging * refactor: rename ruleSets to rules * fix: respect array rule validation --------- Co-authored-by: Christian Ascone <ascone.christian@gmail.com> * fix: this should be passing because AnyOf has no type relevance and 'required' only checks to see if the field has something in it --------- Co-authored-by: Christian Ascone <ascone.christian@gmail.com> --------- Co-authored-by: Christian Ascone <ascone.christian@gmail.com> * chore: correspond with recent changes https://github.com/brianferri/framework/pull/1/commits/de3b902a950b8f5ba8edaafa273f91d7c6ade295 * chore: remove unused private property * feat: attribute mapping in favor of potentially indexed mapping * feat: add more tests * refactor(tests): remove unnecessary amount of tests, rename parameter properties to be more descriptive/analogous to use cases * chore: apply styleCI * feat: add tests to verify compliance with dot notation nesting validator * formatting * fix: remove messages * fix(wip): regression introduced in 14598f6 #55191 (comment) * feat: implement star rule counter tests for simple and nested rules Co-authored-by: Christian Ascone <ascone.christian@gmail.com> * chore: apply styleCI --------- Co-authored-by: Taylor Otwell <taylor@laravel.com> Co-authored-by: Christian Ascone <ascone.christian@gmail.com>
1 parent d07ba17 commit 08960db

File tree

4 files changed

+485
-0
lines changed

4 files changed

+485
-0
lines changed

src/Illuminate/Translation/lang/en/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'alpha' => 'The :attribute field must only contain letters.',
2222
'alpha_dash' => 'The :attribute field must only contain letters, numbers, dashes, and underscores.',
2323
'alpha_num' => 'The :attribute field must only contain letters and numbers.',
24+
'any_of' => 'The :attribute field is invalid.',
2425
'array' => 'The :attribute field must be an array.',
2526
'ascii' => 'The :attribute field must only contain single-byte alphanumeric characters and symbols.',
2627
'before' => 'The :attribute field must be a date before :date.',

src/Illuminate/Validation/Rule.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\Support\Arrayable;
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Traits\Macroable;
8+
use Illuminate\Validation\Rules\AnyOf;
89
use Illuminate\Validation\Rules\ArrayRule;
910
use Illuminate\Validation\Rules\Can;
1011
use Illuminate\Validation\Rules\Date;
@@ -246,6 +247,19 @@ public static function numeric()
246247
return new Numeric;
247248
}
248249

250+
/**
251+
* Get an "any of" rule builder instance.
252+
*
253+
* @param array
254+
* @return \Illuminate\Validation\Rules\AnyOf
255+
*
256+
* @throws \InvalidArgumentException
257+
*/
258+
public static function anyOf($rules)
259+
{
260+
return new AnyOf($rules);
261+
}
262+
249263
/**
250264
* Compile a set of rules for an attribute.
251265
*
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Illuminate\Validation\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
use Illuminate\Contracts\Validation\ValidatorAwareRule;
7+
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Facades\Validator;
9+
use InvalidArgumentException;
10+
11+
class AnyOf implements Rule, ValidatorAwareRule
12+
{
13+
/**
14+
* The rules to match against.
15+
*
16+
* @var array
17+
*/
18+
protected array $rules = [];
19+
20+
/**
21+
* The validator performing the validation.
22+
*
23+
* @var \Illuminate\Validation\Validator
24+
*/
25+
protected $validator;
26+
27+
/**
28+
* Sets the validation rules to match against.
29+
*
30+
* @param Illuminate\Contracts\Validation\ValidationRule[][] $rules
31+
*
32+
* @throws \InvalidArgumentException
33+
*/
34+
public function __construct($rules)
35+
{
36+
if (! is_array($rules)) {
37+
throw new InvalidArgumentException('The provided value must be an array of validation rules.');
38+
}
39+
40+
$this->rules = $rules;
41+
}
42+
43+
/**
44+
* Determine if the validation rule passes.
45+
*
46+
* @param string $attribute
47+
* @param mixed $value
48+
* @return bool
49+
*/
50+
public function passes($attribute, $value)
51+
{
52+
foreach ($this->rules as $rule) {
53+
$validator = Validator::make(
54+
Arr::isAssoc(Arr::wrap($value)) ? $value : [$value],
55+
Arr::isAssoc(Arr::wrap($rule)) ? $rule : [$rule],
56+
$this->validator->customMessages,
57+
$this->validator->customAttributes
58+
);
59+
60+
if ($validator->passes()) {
61+
return true;
62+
}
63+
}
64+
65+
return false;
66+
}
67+
68+
/**
69+
* Get the validation error messages.
70+
*
71+
* @return array
72+
*/
73+
public function message()
74+
{
75+
$message = $this->validator->getTranslator()->get('validation.any_of');
76+
77+
return $message === 'validation.any_of'
78+
? ['The :attribute field is invalid.']
79+
: $message;
80+
}
81+
82+
/**
83+
* Set the current validator.
84+
*
85+
* @param \Illuminate\Contracts\Validation\Validator $validator
86+
* @return $this
87+
*/
88+
public function setValidator($validator)
89+
{
90+
$this->validator = $validator;
91+
92+
return $this;
93+
}
94+
}

0 commit comments

Comments
 (0)
0