File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -29,18 +29,21 @@ public function __construct(private RuleHandlerResolverInterface $ruleHandlerRes
2929
3030 /**
3131 * @param DataSetInterface|mixed|RulesProviderInterface $data
32- * @param iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]> $rules
32+ * @param iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]>|null $rules
3333 */
34- public function validate ($ data , iterable $ rules = [] ): Result
34+ public function validate (mixed $ data , ? iterable $ rules = null ): Result
3535 {
3636 $ data = $ this ->normalizeDataSet ($ data );
37- if ($ rules === [] && $ data instanceof RulesProviderInterface) {
37+ if ($ rules === null && $ data instanceof RulesProviderInterface) {
3838 $ rules = $ data ->getRules ();
3939 }
4040
41- $ context = new ValidationContext ($ this , $ data );
4241 $ compoundResult = new Result ();
42+ if (empty ($ rules )) {
43+ return $ compoundResult ;
44+ }
4345
46+ $ context = new ValidationContext ($ this , $ data );
4447 $ results = [];
4548
4649 foreach ($ rules as $ attribute => $ attributeRules ) {
Original file line number Diff line number Diff line change @@ -12,8 +12,11 @@ interface ValidatorInterface
1212 /**
1313 * Validate data set against rules set for data set attributes.
1414 *
15- * @param DataSetInterface|mixed|RulesProviderInterface $data Data set to validate.
16- * @param iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]> $rules Rules to apply.
15+ * @param DataSetInterface|mixed|RulesProviderInterface $data Data set to validate. If {@see RulesProviderInterface}
16+ * instance provided and rules are not specified explicitly, they are read from the
17+ * {@see RulesProviderInterface::getRules()}.
18+ * @param iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]>|null $rules Rules to apply. If specified,
19+ * rules are not read from data set even if it is an instance of {@see RulesProviderInterface}.
1720 */
18- public function validate (mixed $ data , iterable $ rules = [] ): Result ;
21+ public function validate (mixed $ data , ? iterable $ rules = null ): Result ;
1922}
Original file line number Diff line number Diff line change @@ -50,4 +50,30 @@ public function testExplicitRulesHavePriority(): void
5050
5151 $ this ->assertTrue ($ result ->isValid ());
5252 }
53+
54+ public function testEmptyExplicitRulesHavePriority (): void
55+ {
56+ $ dataSet = new RulesProvidedDataSet (
57+ [
58+ 'username ' => 'test123 ' ,
59+ 'age ' => 42 ,
60+ ],
61+ [
62+ 'username ' => [
63+ new Regex ('^[a-z]+$ ' ),
64+ new HasLength (max: 3 ),
65+ ],
66+ 'age ' => [
67+ new Number (max: 25 ),
68+ ],
69+ ]
70+ );
71+ $ validator = FakeValidatorFactory::make ();
72+ $ result = $ validator ->validate (
73+ $ dataSet ,
74+ []
75+ );
76+
77+ $ this ->assertTrue ($ result ->isValid ());
78+ }
5379}
You can’t perform that action at this time.
0 commit comments