Skip to content

Commit

Permalink
Add method to generate validation rules for nested array properties
Browse files Browse the repository at this point in the history
  • Loading branch information
t1sh0o committed Aug 14, 2018
1 parent 0925f83 commit fdca5ee
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/Mpociot/ApiDoc/Generators/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,17 @@ protected function getDocblockResponse($tags)
*/
protected function getParameters($routeData, $routeAction, $bindings)
{
$validator = Validator::make([], $this->getRouteRules($routeAction['uses'], $bindings));
foreach ($validator->getRules() as $attribute => $rules) {
$rules = $this->simplifyRules($this->getRouteRules($routeAction['uses'], $bindings));

foreach ($rules as $attribute => $rules) {
$attributeData = [
'required' => false,
'type' => null,
'default' => '',
'value' => '',
'description' => [],
];

foreach ($rules as $ruleName => $rule) {
$this->parseRule($rule, $attribute, $attributeData, $routeData['id']);
}
Expand All @@ -97,6 +99,30 @@ protected function getParameters($routeData, $routeAction, $bindings)
return $routeData;
}

/**
* Format the validation rules as plain array
*
* @param array $rules
* @return array
*/
protected function simplifyRules($rules)
{
$simplifiedRules = Validator::make([], $rules)->getRules();

if (count($simplifiedRules) === 0) {
return $simplifiedRules;
}

$values = collect($simplifiedRules)
->filter(function ($values) {
return in_array('array', $values);
})->map(function ($val, $key) {
return [''];
})->all();

return Validator::make($values, $rules)->getRules();
}

/**
* @param $route
* @param $bindings
Expand Down

0 comments on commit fdca5ee

Please sign in to comment.