Skip to content

Commit aa37df6

Browse files
committed
♻️ changed feedback
1 parent 751c195 commit aa37df6

File tree

8 files changed

+39
-13
lines changed

8 files changed

+39
-13
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"require-dev": {
1818
"guzzlehttp/guzzle": "^6.0|^7.0",
1919
"orchestra/testbench": "^9|^10",
20-
"phpunit/phpunit": "^10|^11|^12",
20+
"phpunit/phpunit": "^10|^11",
2121
"laravel/scout": "^10"
2222
},
2323
"autoload": {

src/Rules/Resource/ResourceCustomRules.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ public function buildValidationRules(string $attribute, mixed $value): array
1111
{
1212
$request = app(RestRequest::class);
1313

14-
if ($value['operation'] === 'create') {
14+
$operation = is_array($value) ? ($value['operation'] ?? null) : null;
15+
16+
if ($operation === 'create') {
1517
$rules = $this->resource->createRules($request);
16-
} elseif ($value['operation'] === 'update') {
18+
} elseif ($operation === 'update') {
1719
$rules = $this->resource->updateRules($request);
1820
} else {
19-
// No rules needed
20-
return [];
21-
}
21+
// No rules needed for unknown/missing operations
22+
return [];
23+
}
2224

2325
$rules = array_merge_recursive(
2426
$rules,

src/Rules/Resource/ResourceRelationOrNested.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
1919
$relationResource = $this->resource->relation($relation)?->resource();
2020

2121
if ($relationResource === null) {
22-
$fail('The relation is not allowed');
22+
$fail('The relation is not valid or allowed for this resource.');
2323
}
2424
}
2525

src/Rules/Search/Search.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public function buildValidationRules(string $attribute, mixed $value): array
1515

1616
return [
1717
$attribute.'.limit' => ['sometimes', 'integer', Rule::in($this->resource->getLimits($request))],
18-
$attribute.'page' => ['sometimes', 'integer'],
18+
$attribute.'.page' => ['sometimes', 'integer'],
1919
$attribute.'.filters' => ['sometimes', 'array'],
20-
$attribute.'gates' => ['sometimes', 'array', Rule::in(['viewAny', 'view', 'create', 'update', 'delete', 'restore', 'forceDelete'])],
20+
$attribute.'.gates' => ['sometimes', 'array', Rule::in(['viewAny', 'view', 'create', 'update', 'delete', 'restore', 'forceDelete'])],
2121
$attribute.'.filters.*' => (new SearchFilter())->setResource($this->resource),
2222
$attribute.'.scopes' => ['sometimes', 'array', $isScoutMode ? 'prohibited' : ''],
2323
$attribute.'.scopes.*' => (new SearchScope())->setResource($this->resource),

src/Rules/Search/SearchFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function buildValidationRules(string $attribute, mixed $value): array
1818
Rule::in($this->resource->getScoutFields($request)) :
1919
(new ResourceFieldOrNested())->setResource($this->resource);
2020

21-
$allowedOperators = app(RestRequest::class)->isScoutMode() ?
21+
$allowedOperators = $isScoutMode ?
2222
['=', 'in', 'not in'] :
2323
['=', '!=', '>', '>=', '<', '<=', 'like', 'not like', 'in', 'not in'];
2424

@@ -47,7 +47,7 @@ public function buildValidationRules(string $attribute, mixed $value): array
4747
],
4848
$attribute.'.type' => !$isScoutMode ? [
4949
'sometimes',
50-
Rule::in('or', 'and'),
50+
Rule::in(['or', 'and']),
5151
] : [
5252
'prohibited',
5353
],

src/Rules/Search/SearchInstructionField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function buildValidationRules(string $attribute, mixed $value): array
2626
];
2727
}
2828

29-
public function setInstruction(Instruction $instruction)
29+
public function setInstruction(Instruction $instruction): static
3030
{
3131
$this->instruction = $instruction;
3232

tests/Feature/Controllers/SearchLimitingOperationsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Controllers;
3+
namespace Lomkit\Rest\Tests\Feature\Controllers;
44

55
use Illuminate\Support\Facades\Gate;
66
use Lomkit\Rest\Tests\Feature\TestCase;

tests/Feature/Controllers/SearchPaginateOperationsTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ public function test_getting_a_list_of_resources_paginating_unauthorized_limit()
3131
$response->assertExactJsonStructure(['message', 'errors' => ['search.limit']]);
3232
}
3333

34+
public function test_getting_a_list_of_resources_using_string_page(): void
35+
{
36+
ModelFactory::new()->count(2)->create();
37+
38+
Gate::policy(Model::class, GreenPolicy::class);
39+
40+
$response = $this->post(
41+
'/api/models/search',
42+
[
43+
'search' => [
44+
'page' => 'hello',
45+
],
46+
],
47+
['Accept' => 'application/json']
48+
);
49+
50+
$response->assertStatus(422);
51+
$response->assertExactJsonStructure(['message', 'errors' => ['search.page']]);
52+
}
53+
3454
public function test_getting_a_list_of_resources_paginating_second_page(): void
3555
{
3656
$matchingModel = ModelFactory::new()->create()->fresh();
@@ -50,6 +70,10 @@ public function test_getting_a_list_of_resources_paginating_second_page(): void
5070
['Accept' => 'application/json']
5171
);
5272

73+
$response->assertJsonFragment([
74+
'current_page' => 2,
75+
]);
76+
5377
$this->assertResourcePaginated(
5478
$response,
5579
[$matchingModel2],

0 commit comments

Comments
 (0)