Add more tests for Each rule

This commit is contained in:
Dominick Johnson 2024-12-19 15:43:13 -07:00 committed by Henrique Moody
commit e465810efe
No known key found for this signature in database
GPG key ID: 221E9281655813A6

View file

@ -239,3 +239,55 @@ test('With array template and name, default', expectAll(
'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',
],
]
));