Package
https://github.com/go-playground/validator
Package version eg. v8, v9:
V9
Issue, Question or Enhancement:
Question: Followed the tutorial, however, i get invalid memory address or nil pointer dereference
error
Code sample, to showcase or reproduce:
import "gopkg.in/go-playground/validator.v9"
var validate *validator.Validate
func V1Register(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var params RegisterReq
if err := decoder.Decode(¶ms); err != nil {
log.Logger.Error("%s", err)
}
user := &models.User{
Id: 10,
Email: params.Email,
Username: "asddas",
ProfileId: 22,
Password: "rotroot",
UserType: 10,
}
if err := models.CreateUser(user); err != nil {
log.Logger.Notice("%s", err)
}
log.Logger.Info("%s", params.Email)
}
func CreateUser(user *User) (err error) {
log.Logger.Info("%s", user)
err = validate.Struct(user)
if err != nil {
return err
}
err = database.DBCon.Insert(user)
if err != nil {
return err
}
return nil
}
Thanks!