Skip to content

Commit

Permalink
feat: Add parsing support for exists rule in parseRule switch case (#886
Browse files Browse the repository at this point in the history
)

* feat: add parsing support for exists rule in parseRule switch case

* feat: add test support for exists rule in supportedRules yield

* Add schema creation

* Improve message

* Skip example comparison

---------

Co-authored-by: Shalvah <shalvah@users.noreply.github.com>
  • Loading branch information
lotfimostafa and shalvah authored Sep 25, 2024
1 parent a26d9c9 commit 8a6c644
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Extracting/ParsesValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,9 @@ protected function parseRule($rule, array &$parameterData, bool $independentOnly
case 'different':
$parameterData['description'] .= " The value and <code>{$arguments[0]}</code> must be different.";
break;

case 'exists':
$parameterData['description'] .= " The <code>{$arguments[1]}</code> of an existing record in the {$arguments[0]} table.";
break;
default:
// Other rules not supported
break;
Expand Down
17 changes: 16 additions & 1 deletion tests/Unit/ValidationRuleParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Knuckles\Scribe\Tests\Unit;

use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Translation\Translator;
use Illuminate\Validation\Rule;
Expand Down Expand Up @@ -40,6 +41,11 @@ public function parse($validationRules, $customParameterData = []): array
*/
public function can_parse_supported_rules(array $ruleset, array $customInfo, array $expected)
{
// Needed for `exists` rule
Schema::create('users', function ($table) {
$table->id();
});

$results = $this->strategy->parse($ruleset, $customInfo);

$parameterName = array_keys($ruleset)[0];
Expand All @@ -49,7 +55,9 @@ public function can_parse_supported_rules(array $ruleset, array $customInfo, arr
$this->assertEquals($expected['type'], $results[$parameterName]['type']);
}

// Validate that the generated values actually pass validation
// Validate that the generated values actually pass validation (for rules where we can generate some data)
if (is_string($ruleset[$parameterName]) && str_contains($ruleset[$parameterName], "exists")) return;

$exampleData = [$parameterName => $results[$parameterName]['example']];
$validator = Validator::make($exampleData, $ruleset);
try {
Expand Down Expand Up @@ -439,6 +447,13 @@ public static function supportedRules()
'description' => 'Must be accepted.',
],
];
yield 'exists' => [
['exists_param' => 'exists:users,id'],
[],
[
'description' => 'The <code>id</code> of an existing record in the users table.',
],
];
yield 'unsupported' => [
['unsupported_param' => [new DummyValidationRule, 'bail']],
['unsupported_param' => ['description' => $description]],
Expand Down

0 comments on commit 8a6c644

Please sign in to comment.