Skip to content

Commit

Permalink
Merge pull request #158 from RonasIT/77-implement-list-exists-validat…
Browse files Browse the repository at this point in the history
…ion-rule

#77: refactor list_exists rule
  • Loading branch information
DenTray authored Dec 12, 2024
2 parents d4a3681 + 1d1e14e commit f9478a5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
12 changes: 8 additions & 4 deletions src/HelpersServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ protected function extendValidator(): void
});

Validator::extend('list_exists', function ($attribute, $value, $parameters) {

if (count($parameters) < 1) {
return false;
}
Expand All @@ -76,12 +75,17 @@ protected function extendValidator(): void
$keyField = Arr::get($parameters, 1, 'id');

if (!empty(Arr::get($parameters, 2))) {
$value = collect($value)->pluck(Arr::get($parameters, 2));
$value = Arr::pluck($value, Arr::get($parameters, 2));
}

return DB::table($table)
$value = array_unique($value);

$existingValueCount = DB::table($table)
->whereIn($keyField, $value)
->exists();
->distinct()
->count($keyField);

return $existingValueCount === count($value);
});
}

Expand Down
19 changes: 16 additions & 3 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testUniqueExceptOfAuthorizedUserFail()

public function testListExists()
{
$this->mockListExists(true);
$this->mockListExists([1, 2, 3]);

$validator = Validator::make(
['ids' => [1, 2, 3]],
Expand All @@ -77,9 +77,22 @@ public function testListExists()
$this->assertTrue($validator->passes());
}

public function testListExistsIfDuplicateValues()
{
$this->mockListExists([1, 2, 3]);

$validator = Validator::make([
'ids' => [1, 2, 3, 3],
], [
'ids' => 'list_exists:clients,user_id',
]);

$this->assertTrue($validator->passes());
}

public function testListExistsByArray()
{
$this->mockListExists(true);
$this->mockListExists([1, 2, 3]);

$validator = Validator::make(
[
Expand Down Expand Up @@ -108,7 +121,7 @@ public function testListExistsByArray()

public function testListExistsFailedValidation()
{
$this->mockListExists(false);
$this->mockListExists([1, 2]);

$validator = Validator::make(
['ids' => [1, 2, 3]],
Expand Down
10 changes: 5 additions & 5 deletions tests/support/Traits/SqlMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,14 @@ protected function mockExistsUsersExceptAuthorizedByArray(
}

protected function mockListExists(
bool $isExist,
array $result,
string $table = 'clients',
string $keyField = 'user_id',
): void {
$this->mockSelectExists(
"select exists(select * from \"{$table}\" where \"{$keyField}\" in (?, ?, ?)) as \"exists\"",
$isExist,
[1, 2, 3]
$this->mockSelect(
query: "select count(distinct \"{$keyField}\") as aggregate from \"{$table}\" where \"{$keyField}\" in (?, ?, ?)",
result: [[ 'aggregate' => count($result) ]],
bindings: [1, 2, 3],
);
}

Expand Down

0 comments on commit f9478a5

Please sign in to comment.