ch4.5
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package data
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gitea.local.lab/Lbenedar/greenlight/internal/validator"
|
||||
)
|
||||
|
||||
type Movie struct {
|
||||
ID int64 `json:"id"`
|
||||
@@ -11,3 +15,20 @@ type Movie struct {
|
||||
Genres []string `json:"genres,omitempty"`
|
||||
Version int32 `json:"version"`
|
||||
}
|
||||
|
||||
func ValidateMovie(v *validator.Validator, movie *Movie) {
|
||||
v.Check(movie.Title != "", "title", "must be provided")
|
||||
v.Check(len(movie.Title) <= 500, "title", "must not be more than 500 bytes long")
|
||||
|
||||
v.Check(movie.Year != 0, "year", "must be provided")
|
||||
v.Check(movie.Year >= 1988, "year", "must be greater than 1988")
|
||||
v.Check(movie.Year <= int32(time.Now().Year()), "year", "must not be in the future")
|
||||
|
||||
v.Check(movie.Runtime != 0, "runtime", "must be provided")
|
||||
v.Check(movie.Runtime > 0, "runtime", "must be a positive integer")
|
||||
|
||||
v.Check(movie.Genres != nil, "genres", "must be provided")
|
||||
v.Check(len(movie.Genres) >= 1, "genres", "must contain at least 1 genre")
|
||||
v.Check(len(movie.Genres) <= 5, "genres", "must not contain more than 5 genres")
|
||||
v.Check(validator.Unique(movie.Genres), "genres", "must not contain duplicate values")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user