Skip to content

[Bug] Implement BrewerySort validation rule and integrate into Li… #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/V1/ListBreweries.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Controllers\Controller;
use App\Http\Resources\V1\BreweryResource;
use App\Models\Brewery;
use App\Rules\BrewerySort as BrewerySortRule;
use App\Rules\BreweryType as BreweryTypeRule;
use App\Rules\Coordinates as CoordinatesRule;
use Illuminate\Http\Request;
Expand All @@ -20,7 +21,7 @@ public function __invoke(Request $request)
$request->validate([
'per_page' => ['sometimes', 'required', 'integer', 'min:1', 'max:200'],
'page' => ['integer', 'min:1'],
'sort' => ['string'],
'sort' => ['string', new BrewerySortRule],

// filters
'by_city' => ['sometimes', 'required', 'string', 'min:3', 'max:255'],
Expand Down
65 changes: 65 additions & 0 deletions app/Rules/BrewerySort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class BrewerySort implements ValidationRule
{
/**
* Valid field names for sorting.
*/
protected array $validFields = [
'id', 'name', 'brewery_type', 'city', 'state_province',
'country', 'postal_code', 'phone', 'website_url',
'created_at', 'updated_at',
];

/**
* Valid sort directions.
*/
protected array $validDirections = ['asc', 'desc'];

/**
* Run the validation rule.
*
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
// Split by comma to handle multiple sort fields
$sortParams = explode(',', $value);

foreach ($sortParams as $param) {
$param = trim($param);

// If the parameter contains a colon, it includes a sort direction
if (str_contains($param, ':')) {
[$field, $direction] = explode(':', $param, 2);
$field = trim($field);
$direction = trim($direction);

// Check if the field is valid
if (! in_array($field, $this->validFields)) {
$fail("The sort field '$field' is not valid. Valid fields are: ".implode(', ', $this->validFields));

return;
}

// Check if the direction is valid
if (! in_array(strtolower($direction), $this->validDirections)) {
$fail("The sort direction '$direction' is not valid. Valid directions are: ".implode(', ', $this->validDirections));

return;
}
}

if (! str_contains($param, ':') && ! in_array($param, $this->validFields)) {
$fail("The sort field '$param' is not valid. Valid fields are: ".implode(', ', $this->validFields));

return;
}
}
}
}
Loading