From 983bb5bc19da57b89cdb00d793bd6bc6f77ac4de Mon Sep 17 00:00:00 2001 From: shalvah Date: Sun, 7 Oct 2018 02:07:06 +0100 Subject: [PATCH] Add description for array validation rule --- .../ApiDoc/Generators/AbstractGenerator.php | 16 +++++++--------- src/resources/lang/en/rules.php | 1 + tests/ApiDocGeneratorTest.php | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php b/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php index 2f2a21c1..401c5e64 100644 --- a/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php +++ b/src/Mpociot/ApiDoc/Generators/AbstractGenerator.php @@ -178,19 +178,15 @@ protected function getParameters($routeData, $routeAction, $bindings) 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 @@ protected function splitValuePairs($parameters, $first = 'is ', $last = 'or ') */ 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 @@ protected function parseRule($rule, $attribute, &$attributeData, $seed, $routeDa $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(); diff --git a/src/resources/lang/en/rules.php b/src/resources/lang/en/rules.php index c3493267..5bab0bf7 100644 --- a/src/resources/lang/en/rules.php +++ b/src/resources/lang/en/rules.php @@ -5,6 +5,7 @@ '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`', diff --git a/tests/ApiDocGeneratorTest.php b/tests/ApiDocGeneratorTest.php index 64bee058..b3407514 100644 --- a/tests/ApiDocGeneratorTest.php +++ b/tests/ApiDocGeneratorTest.php @@ -127,7 +127,7 @@ public function testCanParseFormRequestRules() 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']);