Skip to content

Commit 9065ca2

Browse files
committed
Forgot to validate
1 parent fa40d82 commit 9065ca2

3 files changed

Lines changed: 15 additions & 11 deletions

File tree

app/Services/CommentService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ public function getPaginatedComments(string $commentableKey, int $commentableId,
3535
'index' => $index,
3636
'commentable_key' => $commentableKey,
3737
'pagination_limit' => $paginationLimit,
38+
'sort_by' => $sortBy,
3839
], [
3940
'index' => ['required', 'integer', 'min:0'],
4041
'commentable_key' => ['required', Rule::in(config('comment.commentable_keys'))],
4142
'pagination_limit' => ['required', 'integer', 'max:'.config('comment.pagination_limit')],
43+
'sort_by' => ['required', 'string'],
4244
]);
4345

4446
$commentableType = $this->modelResolver->getModelClass($commentableKey);

app/Services/ComputerScienceResourceFilter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public function validate(array $request)
5050
'updated_from' => ['nullable', 'date'],
5151
'updated_to' => ['nullable', 'date'],
5252

53-
'sort_by' => ['nullable', 'string'],
54-
'reverse' => ['nullable', 'string'],
53+
'sort_by' => ['string'],
54+
'reverse' => ['string'],
5555
];
5656

5757
$validator = Validator::make($request, $rules);
@@ -60,6 +60,7 @@ public function validate(array $request)
6060

6161
public function applyFilters($query, array $filters)
6262
{
63+
$this->validate($filters);
6364

6465
// Eager load relations
6566
$query->with(['tags', 'votes', 'upvoteSummary', 'reviewSummary', 'commentsCountRelationship']);

tests/Feature/ComputerScienceResourceFilterTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ public static function invalidFieldProvider(): array
6363
'programming_languages not array' => ['programming_languages', 'not-an-array'],
6464
'programming_languages item too long' => ['programming_languages', [str_repeat('a', 51)]],
6565
'programming_languages not distinct' => ['programming_languages', ['js', 'js']],
66-
'community_rating too low' => ['community_rating', 0],
67-
'community_rating too high' => ['community_rating', 5],
66+
'community too low' => ['community', 0],
67+
'community too high' => ['community', 5],
6868
'teaching_clarity not integer' => ['teaching_clarity', 'high'],
6969
'engagement invalid' => ['engagement', 100],
7070
'practicality invalid' => ['practicality', -1],
7171
'user_friendliness invalid' => ['user_friendliness', 999],
7272
'updates not integer' => ['updates', 'often'],
7373
'updates too high' => ['updates', 6],
74+
'overall too low' => ['overall', 0],
75+
'overall too high' => ['overall', 5],
7476
'created_from not a date' => ['created_from', 'not-a-date'],
7577
'created_to not a date' => ['created_to', '2023-02-30'],
7678
'updated_from not a date' => ['updated_from', 'not-a-date'],
@@ -95,7 +97,6 @@ public function test_cannot_filter_with_invalid_fields(string $field, mixed $inv
9597
public static function filterProvider(): array
9698
{
9799
return [
98-
99100
'by name' => [['name' => 'Graph Theory']],
100101

101102
'by description' => [['description' => 'unit testing best practices']],
@@ -114,7 +115,7 @@ public static function filterProvider(): array
114115
]],
115116

116117
'by ratings' => [[
117-
'community_rating' => 4,
118+
'community' => 4,
118119
'teaching_clarity' => 3,
119120
'engagement' => 2,
120121
'practicality' => 4,
@@ -139,12 +140,12 @@ public static function filterProvider(): array
139140
'name' => 'Graph',
140141
'description' => 'algorithm analysis',
141142
'platforms' => ['podcast', 'website'],
142-
'difficulty' => 'industry_simple',
143-
'pricing' => 'free',
143+
'difficulty' => ['industry_simple'],
144+
'pricing' => ['free'],
144145
'topics' => ['algorithms', 'recursion', 'data-structures'],
145146
'programming_languages' => ['python'],
146147
'general_tags' => ['interactive', 'educational', 'advanced'],
147-
'community_rating' => 4,
148+
'community' => 4,
148149
'teaching_clarity' => 4,
149150
'overall' => 4,
150151
'created_from' => '2025-01-15',
@@ -198,14 +199,14 @@ public function test_apply_filters(array $filters)
198199

199200
// Difficulty
200201
if (! empty($filters['difficulty'])) {
201-
foreach ((array) $filters['difficulty'] as $difficulty) {
202+
foreach ($filters['difficulty'] as $difficulty) {
202203
$this->assertContains($difficulty, $bindings);
203204
}
204205
}
205206

206207
// Pricing
207208
if (! empty($filters['pricing'])) {
208-
foreach ((array) $filters['pricing'] as $pricing) {
209+
foreach ($filters['pricing'] as $pricing) {
209210
$this->assertContains($pricing, $bindings);
210211
}
211212
}

0 commit comments

Comments
 (0)