|
2 | 2 |
|
3 | 3 | use Lemmon\Validator; |
4 | 4 |
|
| 5 | +it('should filter empty values and reindex array', function () { |
| 6 | + $validator = Validator::isArray()->filterEmpty(); |
| 7 | + |
| 8 | + $input = ['apple', '', 'banana', null, 'cherry']; |
| 9 | + $result = $validator->validate($input); |
| 10 | + |
| 11 | + expect($result)->toBe(['apple', 'banana', 'cherry']); |
| 12 | + expect(array_keys($result))->toBe([0, 1, 2]); // Properly reindexed |
| 13 | +}); |
| 14 | + |
| 15 | +it('should preserve valid falsy values when filtering', function () { |
| 16 | + $validator = Validator::isArray()->filterEmpty(); |
| 17 | + |
| 18 | + $input = ['hello', '', 0, null, false, 'world']; |
| 19 | + $result = $validator->validate($input); |
| 20 | + |
| 21 | + expect($result)->toBe(['hello', 0, false, 'world']); |
| 22 | + expect(array_keys($result))->toBe([0, 1, 2, 3]); // Properly reindexed |
| 23 | +}); |
| 24 | + |
| 25 | +it('should filter empty values with item validator', function () { |
| 26 | + $validator = Validator::isArray()->items(Validator::isString())->filterEmpty(); |
| 27 | + |
| 28 | + $input = ['hello', '', 'world', null, 'test']; |
| 29 | + $result = $validator->validate($input); |
| 30 | + |
| 31 | + expect($result)->toBe(['hello', 'world', 'test']); |
| 32 | + expect(array_keys($result))->toBe([0, 1, 2]); // Properly reindexed |
| 33 | +}); |
| 34 | + |
| 35 | +it('should handle empty array after filtering', function () { |
| 36 | + $validator = Validator::isArray()->filterEmpty(); |
| 37 | + |
| 38 | + $input = ['', null, '', null]; |
| 39 | + $result = $validator->validate($input); |
| 40 | + |
| 41 | + expect($result)->toBe([]); |
| 42 | +}); |
| 43 | + |
| 44 | +it('should work without filtering when not enabled', function () { |
| 45 | + $validator = Validator::isArray(); |
| 46 | + |
| 47 | + $input = ['apple', '', 'banana', null, 'cherry']; |
| 48 | + $result = $validator->validate($input); |
| 49 | + |
| 50 | + expect($result)->toBe(['apple', '', 'banana', null, 'cherry']); // No filtering |
| 51 | +}); |
| 52 | + |
5 | 53 | it('should validate plain arrays', function () { |
6 | 54 | $validator = Validator::isArray(); |
7 | 55 |
|
|
0 commit comments