From e465810efeb00b61b8f0bcdeee12ff66344fe2eb Mon Sep 17 00:00:00 2001 From: Dominick Johnson Date: Thu, 19 Dec 2024 15:43:13 -0700 Subject: [PATCH] Add more tests for `Each` rule --- tests/feature/Rules/EachTest.php | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/feature/Rules/EachTest.php b/tests/feature/Rules/EachTest.php index b2f19a2a0..7e1d29d31 100644 --- a/tests/feature/Rules/EachTest.php +++ b/tests/feature/Rules/EachTest.php @@ -239,3 +239,55 @@ 'intType.3' => 'Wrapped must be an integer', ] )); + +test('Chained wrapped rule', expectAll( + fn() => v::each(v::between(5, 7)->odd())->assert([2, 4]), + '2 must be between 5 and 7', + <<<'FULL_MESSAGE' + - Each item in `[2, 4]` must be valid + - All of the required rules must pass for 2 + - 2 must be between 5 and 7 + - 2 must be an odd number + - All of the required rules must pass for 4 + - 4 must be between 5 and 7 + - 4 must be an odd number + FULL_MESSAGE, + [ + '__root__' => 'Each item in `[2, 4]` must be valid', + 'allOf.1' => [ + '__root__' => 'All of the required rules must pass for 2', + 'between' => '2 must be between 5 and 7', + 'odd' => '2 must be an odd number', + ], + 'allOf.2' => [ + '__root__' => 'All of the required rules must pass for 4', + 'between' => '4 must be between 5 and 7', + 'odd' => '4 must be an odd number', + ], + ] +)); + +test('Multiple nested rules', expectAll( + fn() => v::each(v::arrayType()->key('my_int', v::intType()->odd()))->assert([['not_int' => 'wrong'], ['my_int' => 2], 'not an array']), + 'my_int must be present', + <<<'FULL_MESSAGE' + - Each item in `[["not_int": "wrong"], ["my_int": 2], "not an array"]` must be valid + - These rules must pass for `["not_int": "wrong"]` + - my_int must be present + - These rules must pass for `["my_int": 2]` + - my_int must be an odd number + - All of the required rules must pass for "not an array" + - "not an array" must be an array + - my_int must be present + FULL_MESSAGE, + [ + '__root__' => 'Each item in `[["not_int": "wrong"], ["my_int": 2], "not an array"]` must be valid', + 'allOf.1' => 'my_int must be present', + 'allOf.2' => 'my_int must be an odd number', + 'allOf.3' => [ + '__root__' => 'All of the required rules must pass for "not an array"', + 'arrayType' => '"not an array" must be an array', + 'my_int' => 'my_int must be present', + ], + ] +));