Selaa lähdekoodia

Update from base (#7)

* Update doc to address custom validation rules (#247)

* Add description for array validation rule

* Apply fixes from StyleCI

* Remove dd() call
Shalvah A 6 vuotta sitten
vanhempi
commit
3364d23884

+ 4 - 1
README.md

@@ -121,7 +121,7 @@ class ExampleController extends Controller {
 
 #### Form request validation rules
 
-To display a list of valid parameters, your API methods accepts, this package uses Laravels [Form Requests Validation](https://laravel.com/docs/5.2/validation#form-request-validation).
+To display a list of valid parameters, your API methods accepts, this package uses Laravel's [Form Requests Validation](https://laravel.com/docs/5.2/validation#form-request-validation).
 
 
 ```php
@@ -138,6 +138,9 @@ public function rules()
 
 **Result:** ![Form Request](http://marcelpociot.de/documentarian/form_request.png)
 
+### A note on custom validation rules
+This package only supports custom rules defined as classes. You'll also need to define a `__toString()` method in the class, which should return the description that would be displayed in the generated doc.
+
 #### Controller method doc block
 It is possible to override the results for the response. This will also show the responses for other request methods then GET.
 

+ 6 - 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.
@@ -532,8 +528,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']);