Skip to content

Commit

Permalink
fixed parsing of nested fields in validation rules (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelflash authored Nov 19, 2023
1 parent 90d86bc commit 7b5d1b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Extracting/ParsesValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,14 @@ protected function normaliseRules(array $rules): array
// Now this will return the complete ruleset.
// Nested array parameters will be present, with '*' replaced by '0'
$newRules = Validator::make($testData, $rules)->getRules();

// Transform the key names back from 'ids.0' to 'ids.*'

return collect($newRules)->mapWithKeys(function ($val, $paramName) use ($rules) {
// Transform the key names back from '__asterisk__' to '*'
if (Str::contains($paramName, '__asterisk__')) {
$paramName = str_replace('__asterisk__', '*', $paramName);
}

// Transform the key names back from 'ids.0' to 'ids.*'
if (Str::contains($paramName, '.0')) {
$genericArrayKeyName = str_replace('.0', '.*', $paramName);

Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/ValidationRuleParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ public function can_transform_arrays_and_objects()
$this->assertEquals('string[]', $results['array_of_objects_with_array[].another[].one.field1']['type']);
$this->assertEquals('integer', $results['array_of_objects_with_array[].another[].one.field2']['type']);
$this->assertEquals('number', $results['array_of_objects_with_array[].another[].two.field2']['type']);

$ruleset = [
'*.foo' => 'required|array',
'*.foo.*' => 'required|array',
'*.foo.*.bar' => 'required',
];
$results = $this->strategy->parse($ruleset);
$this->assertCount(3, $results);
$this->assertEquals('object', $results['*']['type']);
$this->assertEquals('object[]', $results['*.foo']['type']);
$this->assertEquals('string', $results['*.foo[].bar']['type']);
}

public static function supportedRules()
Expand Down

0 comments on commit 7b5d1b6

Please sign in to comment.