Skip to content

Commit 5e8e24c

Browse files
authored
Merge pull request #348 from shalvah/add-description-array-validation-rule
Add description for array validation rule
2 parents e8ae8d8 + 983bb5b commit 5e8e24c

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

src/Mpociot/ApiDoc/Generators/AbstractGenerator.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,15 @@ protected function getParameters($routeData, $routeAction, $bindings)
178178
protected function simplifyRules($rules)
179179
{
180180
// this will split all string rules into arrays of strings
181-
$rules = Validator::make([], $rules)->getRules();
182-
183-
if (count($rules) === 0) {
184-
return $rules;
185-
}
181+
$newRules = Validator::make([], $rules)->getRules();
186182

187183
// Laravel will ignore the nested array rules unless the key referenced exists and is an array
188-
// So we'll to create an empty array for each array attribute
189-
$values = collect($rules)
184+
// So we'll create an empty array for each array attribute
185+
$values = collect($newRules)
190186
->filter(function ($values) {
191187
return in_array('array', $values);
192188
})->map(function ($val, $key) {
193-
return [''];
189+
return [str_random()];
194190
})->all();
195191

196192
// Now this will return the complete ruleset.
@@ -371,6 +367,7 @@ protected function splitValuePairs($parameters, $first = 'is ', $last = 'or ')
371367
*/
372368
protected function parseRule($rule, $attribute, &$attributeData, $seed, $routeData)
373369
{
370+
if (starts_with($attribute, 'array.')) { dd(array_keys($routeData)); }
374371
$faker = Factory::create();
375372
$faker->seed(crc32($seed));
376373

@@ -532,8 +529,9 @@ protected function parseRule($rule, $attribute, &$attributeData, $seed, $routeDa
532529
$attributeData['type'] = $rule;
533530
break;
534531
case 'array':
535-
$attributeData['value'] = $faker->word;
532+
$attributeData['value'] = [$faker->word];
536533
$attributeData['type'] = $rule;
534+
$attributeData['description'][] = Description::parse($rule)->getDescription();
537535
break;
538536
case 'date':
539537
$attributeData['value'] = $faker->date();

src/resources/lang/en/rules.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'alpha' => 'Only alphabetic characters allowed',
66
'alpha_dash' => 'Allowed: alpha-numeric characters, as well as dashes and underscores.',
77
'alpha_num' => 'Only alpha-numeric characters allowed',
8+
'array' => 'Must be an array',
89
'in' => ':attribute',
910
'not_in' => 'Not in: :attribute',
1011
'min' => 'Minimum: `:attribute`',

tests/ApiDocGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function testCanParseFormRequestRules()
127127
case 'array':
128128
$this->assertFalse($attribute['required']);
129129
$this->assertSame('array', $attribute['type']);
130-
$this->assertCount(0, $attribute['description']);
130+
$this->assertCount(1, $attribute['description']);
131131
break;
132132
case 'between':
133133
$this->assertFalse($attribute['required']);

0 commit comments

Comments
 (0)