瀏覽代碼

Add description for array validation rule

shalvah 6 年之前
父節點
當前提交
983bb5bc19

+ 7 - 9
src/Mpociot/ApiDoc/Generators/AbstractGenerator.php

@@ -178,19 +178,15 @@ abstract class AbstractGenerator
     protected function simplifyRules($rules)
     {
         // this will split all string rules into arrays of strings
-        $rules = Validator::make([], $rules)->getRules();
-
-        if (count($rules) === 0) {
-            return $rules;
-        }
+        $newRules = Validator::make([], $rules)->getRules();
 
         // Laravel will ignore the nested array rules unless the key referenced exists and is an array
-        // So we'll to create an empty array for each array attribute
-        $values = collect($rules)
+        // So we'll create an empty array for each array attribute
+        $values = collect($newRules)
             ->filter(function ($values) {
                 return in_array('array', $values);
             })->map(function ($val, $key) {
-                return [''];
+                return [str_random()];
             })->all();
 
         // Now this will return the complete ruleset.
@@ -371,6 +367,7 @@ abstract class AbstractGenerator
      */
     protected function parseRule($rule, $attribute, &$attributeData, $seed, $routeData)
     {
+        if (starts_with($attribute, 'array.')) { dd(array_keys($routeData)); }
         $faker = Factory::create();
         $faker->seed(crc32($seed));
 
@@ -532,8 +529,9 @@ abstract class AbstractGenerator
                 $attributeData['type'] = $rule;
                 break;
             case 'array':
-                $attributeData['value'] = $faker->word;
+                $attributeData['value'] = [$faker->word];
                 $attributeData['type'] = $rule;
+                $attributeData['description'][] = Description::parse($rule)->getDescription();
                 break;
             case 'date':
                 $attributeData['value'] = $faker->date();

+ 1 - 0
src/resources/lang/en/rules.php

@@ -5,6 +5,7 @@ return [
     'alpha' => 'Only alphabetic characters allowed',
     'alpha_dash' => 'Allowed: alpha-numeric characters, as well as dashes and underscores.',
     'alpha_num' => 'Only alpha-numeric characters allowed',
+    'array' => 'Must be an array',
     'in' => ':attribute',
     'not_in' => 'Not in: :attribute',
     'min' => 'Minimum: `:attribute`',

+ 1 - 1
tests/ApiDocGeneratorTest.php

@@ -127,7 +127,7 @@ class ApiDocGeneratorTest extends TestCase
                 case 'array':
                     $this->assertFalse($attribute['required']);
                     $this->assertSame('array', $attribute['type']);
-                    $this->assertCount(0, $attribute['description']);
+                    $this->assertCount(1, $attribute['description']);
                     break;
                 case 'between':
                     $this->assertFalse($attribute['required']);