Skip to content

Commit

Permalink
Support No-example in inline validators
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Apr 8, 2023
1 parent 56d589a commit 5fbe5fc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Extracting/ParsesValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public function getParametersFromValidationRules(array $validationRules, array $
}

// Make sure the user-specified example overwrites others.
if (isset($userSpecifiedParameterInfo['example'])) {
if ($this->shouldCastUserExample()) {
if (array_key_exists('example', $userSpecifiedParameterInfo)) {
if ($userSpecifiedParameterInfo['example'] != null && $this->shouldCastUserExample()) {
// Examples in comments are strings, we need to cast them properly
$parameterData['example'] = $this->castToType($userSpecifiedParameterInfo['example'], $parameterData['type'] ?? 'string');
} else {
Expand Down
18 changes: 9 additions & 9 deletions src/Extracting/Strategies/GetFromInlineValidatorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,23 @@ enum_exists($enum) && method_exists($enum, 'tryFrom')
continue;
}

$description = $example = null;
$dataFromComment = [];
$comments = join("\n", array_map(
fn($comment) => ltrim(ltrim($comment->getReformattedText(), "/")),
$item->getComments()
)
);
));

if ($comments) {
$description = trim(str_replace(['No-example.', 'No-example'], '', $comments));
$example = null;
if (preg_match('/(.*\s+|^)Example:\s*([\s\S]+)\s*/s', $description, $matches)) {
$description = trim($matches[1]);
$example = $matches[2];
if (str_contains($comments, 'No-example')) $dataFromComment['example'] = null;

$dataFromComment['description'] = trim(str_replace(['No-example.', 'No-example'], '', $comments));
if (preg_match('/(.*\s+|^)Example:\s*([\s\S]+)\s*/s', $dataFromComment['description'], $matches)) {
$dataFromComment['description'] = trim($matches[1]);
$dataFromComment['example'] = $matches[2];
}
}

$customParameterData[$paramName] = compact('description', 'example');
$customParameterData[$paramName] = $dataFromComment;
}

return [$rules, $customParameterData];
Expand Down
13 changes: 6 additions & 7 deletions tests/Fixtures/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public function withInlineRequestValidate(Request $request)
'room_id' => ['string', 'in:3,5,6'],
// Whether to ban the user forever. Example: false
'forever' => 'boolean',
// Just need something here
// Just need something here. No-example
'another_one' => 'numeric',
'even_more_param' => 'array',
'book.name' => 'string',
Expand All @@ -452,7 +452,7 @@ public function withInlineRequestValidateNoAssignment(Request $request)
'room_id' => ['string', 'in:3,5,6'],
// Whether to ban the user forever. Example: false
'forever' => 'boolean',
// Just need something here
// Just need something here. No-example
'another_one' => 'numeric',
'even_more_param' => 'array',
'book.name' => 'string',
Expand All @@ -478,7 +478,7 @@ public function withInlineRequestValidateQueryParams(Request $request)
'room_id' => ['string', 'in:3,5,6'],
// Whether to ban the user forever. Example: false
'forever' => 'boolean',
// Just need something here
// Just need something here. No-example
'another_one' => 'numeric',
'even_more_param' => 'array',
'book.name' => 'string',
Expand All @@ -504,7 +504,7 @@ public function withInlineValidatorMake(Request $request)
'room_id' => ['string', 'in:3,5,6'],
// Whether to ban the user forever. Example: false
'forever' => 'boolean',
// Just need something here
// Just need something here. No-example
'another_one' => 'numeric',
'even_more_param' => 'array',
'book.name' => 'string',
Expand Down Expand Up @@ -532,7 +532,7 @@ public function withInlineRequestValidateWithBag(Request $request)
'room_id' => ['string', 'in:3,5,6'],
// Whether to ban the user forever. Example: false
'forever' => 'boolean',
// Just need something here
// Just need something here. No-example
'another_one' => 'numeric',
'even_more_param' => 'array',
'book.name' => 'string',
Expand All @@ -548,7 +548,6 @@ public function withInlineRequestValidateWithBag(Request $request)
// Do stuff
}


public function withInlineThisValidate(Request $request)
{
$this->validate($request, [
Expand All @@ -558,7 +557,7 @@ public function withInlineThisValidate(Request $request)
'room_id' => ['string', 'in:3,5,6'],
// Whether to ban the user forever. Example: false
'forever' => 'boolean',
// Just need something here
// Just need something here. No-example
'another_one' => 'numeric',
'even_more_param' => 'array',
'book.name' => 'string',
Expand Down
1 change: 1 addition & 0 deletions tests/Strategies/GetFromInlineValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class GetFromInlineValidatorTest extends BaseLaravelTest
'type' => 'number',
'required' => false,
'description' => 'Just need something here.',
'example' => null,
],
'even_more_param' => [
'type' => 'object',
Expand Down

0 comments on commit 5fbe5fc

Please sign in to comment.