feat: validation, postgress connection,
This commit is contained in:
@@ -2,6 +2,8 @@ package data
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"greenlight.debuggingjon.dev/internal/validator"
|
||||
)
|
||||
|
||||
type Movie struct {
|
||||
@@ -13,3 +15,20 @@ type Movie struct {
|
||||
Genres []string `json:"genres,omitempty"` // Add the omitempty directive
|
||||
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 >= 1888, "year", "must be greater than 1888")
|
||||
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