Skip to content

Commit bd14af3

Browse files
Add route to show all rules the action
1 parent ac9e48c commit bd14af3

4 files changed

Lines changed: 49 additions & 0 deletions

File tree

routes/dynamic.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
'store',
1212
'update',
1313
]);
14+
Route::get('dynamic/{action}', [DynamicController::class, 'show']);
1415
});

src/Http/Controllers/DynamicController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace EdineiValdameri\Laravel\DynamicValidation\Http\Controllers;
66

77
use EdineiValdameri\Laravel\DynamicValidation\Http\Requests\DynamicFormRequest;
8+
use EdineiValdameri\Laravel\DynamicValidation\Http\Resources\RuleResource;
89
use EdineiValdameri\Laravel\DynamicValidation\Repositories\RuleRepository;
910
use Illuminate\Http\JsonResponse;
1011

@@ -35,4 +36,13 @@ public function update(DynamicFormRequest $request, int $id): JsonResponse
3536

3637
return response()->json($rule);
3738
}
39+
40+
public function show(string $action): JsonResponse
41+
{
42+
$rules = $this->repository->getRulesByAction($action);
43+
44+
return response()->json(
45+
RuleResource::collection($rules)
46+
);
47+
}
3848
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace EdineiValdameri\Laravel\DynamicValidation\Http\Resources;
6+
7+
use EdineiValdameri\Laravel\DynamicValidation\Models\Rule;
8+
use Illuminate\Contracts\Support\Arrayable;
9+
use Illuminate\Http\Request;
10+
use Illuminate\Http\Resources\Json\JsonResource;
11+
use JsonSerializable;
12+
13+
class RuleResource extends JsonResource
14+
{
15+
/**
16+
* @param Request $request
17+
* @return array<string, mixed>|Arrayable<int, Rule>|JsonSerializable
18+
*/
19+
public function toArray(Request $request)
20+
{
21+
return parent::toArray($request);
22+
}
23+
}

tests/Features/ControllerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,18 @@
4242
'rule' => 'nullable',
4343
]);
4444
});
45+
46+
it('validates the return of the dynamic controller\'s show method', function () {
47+
Rule::query()->create([
48+
'action' => 'test',
49+
'field' => 'test',
50+
'rule' => 'required',
51+
]);
52+
$response = $this->get('dynamic/test');
53+
$response->assertOk();
54+
$response->assertJsonFragment([
55+
'action' => 'test',
56+
'field' => 'test',
57+
'rule' => 'required',
58+
]);
59+
});

0 commit comments

Comments
 (0)