Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Extracting/ParsesValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

trait ParsesValidationRules
{
use ParamHelpers;
use ParamHelpers;

public static \stdClass $MISSING_VALUE;

Expand Down Expand Up @@ -548,6 +548,24 @@ protected function processRule($rule, $ruleArguments, array &$parameterData, arr
case 'nullable':
$parameterData['nullable'] = true;
break;
case 'unique':
$table = $ruleArguments[0] ?? 'table';
$column = isset($ruleArguments[1])
? ($ruleArguments[1] === 'NULL' ? 'id' : $ruleArguments[1])
: 'id'; // If the column is not specified, assume it is 'id'.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think doing this may cause some issues for people with some other column. Can we change this to not mention a column instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it stands, it does not require the column to be provided.
Rule::unique('vouchers') It is the equivalent Rule::unique('vouchers', 'id')
The ID column is assumed, because Laravel assumes that it is also ID.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just go with "Must not be taken" (which is Rails' default unique validation message). Database table and column names are implementation details that API consumers should neither know or care about.


// For cases where an ID is passed to be ignored in an update.
if (count($ruleArguments) >= 3 && !empty($ruleArguments[2]) && $ruleArguments[2] !== 'NULL') {
$except = $ruleArguments[2];
$idColumn = $ruleArguments[3] ?? 'id';
$parameterData['description'] .= " Must be unique in the <code>{$table}</code> table for column <code>{$column}</code> (ignoring record with <code>{$idColumn}</code> = <code>{$except}</code>).";
} else {
$parameterData['description'] .= " Must be unique in the <code>{$table}</code> table for column <code>{$column}</code>.";
}

$parameterData['setter'] = fn() => $this->getFaker()->unique()->word();
$parameterData['type'] = 'string';
break;
case 'exists':
$parameterData['description'] .= " The <code>{$ruleArguments[1]}</code> of an existing record in the {$ruleArguments[0]} table.";
break;
Expand Down
33 changes: 31 additions & 2 deletions tests/Unit/ValidationRuleParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public function parse($validationRules, $customParameterData = []): array
*/
public function can_parse_supported_rules(array $ruleset, array $customInfo, array $expected)
{
// Needed for `exists` rule
// Needed for `exists` and `unique` rules
Schema::create('users', function ($table) {
$table->id();
$table->string('email')->unique();
});

$results = $this->strategy->parse($ruleset, $customInfo);
Expand All @@ -55,7 +56,11 @@ public function can_parse_supported_rules(array $ruleset, array $customInfo, arr
}

// 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;
// Skip validation for exists and unique rules as they require database data
if (is_string($ruleset[$parameterName]) &&
(str_contains($ruleset[$parameterName], "exists") || str_contains($ruleset[$parameterName], "unique"))) {
return;
}

$exampleData = [$parameterName => $results[$parameterName]['example']];
$validator = Validator::make($exampleData, $ruleset);
Expand Down Expand Up @@ -138,6 +143,30 @@ public static function supportedRules()
// Second is custom information (from bodyParameters() or comments)
// Third is expected result

yield 'unique' => [
['unique_param' => 'unique:users,email'],
[],
[
'description' => 'Must be unique in the <code>users</code> table for column <code>email</code>.',
'type' => 'string',
],
];
yield 'unique (simple)' => [
['unique_simple_param' => 'unique:users'],
[],
[
'description' => 'Must be unique in the <code>users</code> table for column <code>unique_simple_param</code>.',
'type' => 'string',
],
];
yield 'unique (with except)' => [
['unique_except_param' => 'unique:users,email,1,id'],
[],
[
'description' => 'Must be unique in the <code>users</code> table for column <code>email</code> (ignoring record with <code>id</code> = <code>1</code>).',
'type' => 'string',
],
];
yield 'string' => [
['string_param' => 'string'],
['string_param' => ['description' => $description]],
Expand Down
Loading