Jelajahi Sumber

fixed parsing of nested fields in validation rules (#749)

Raphael F 1 tahun lalu
induk
melakukan
7b5d1b6cb9

+ 7 - 2
src/Extracting/ParsesValidationRules.php

@@ -145,9 +145,14 @@ trait ParsesValidationRules
         // 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);
 

+ 11 - 0
tests/Unit/ValidationRuleParsingTest.php

@@ -110,6 +110,17 @@ class ValidationRuleParsingTest extends BaseLaravelTest
         $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()