Adding ValidateFilters method in filters.go and use it in our listMoviesHandler

This commit is contained in:
Maxime Delporte
2025-11-12 11:09:57 +01:00
parent bf7c53e565
commit 95d9de60b4
2 changed files with 25 additions and 3 deletions

View File

@@ -1,7 +1,21 @@
package data
import "greenlight.craftr.fr/internal/validator"
type Filters struct {
Page int
PageSize int
Sort string
Page int
PageSize int
Sort string
SortSafelist []string
}
func ValidateFilters(v *validator.Validator, f Filters) {
// Check that the page and page_size parameters contain sensible values
v.Check(f.Page > 0, "page", "must be greater than 0")
v.Check(f.Page <= 10_000_000, "page", "must be a maximum of 10 million")
v.Check(f.PageSize > 0, "page_size", "must be greater than 0")
v.Check(f.PageSize <= 100, "page_size", "must be a maximum of 100")
// Check that the sort parameter matches a value in the safelist
v.Check(validator.PermittedValue(f.Sort, f.SortSafelist...), "sort", "invalid sort value")
}