chore: update user setting validator

This commit is contained in:
Steven
2022-08-20 21:51:28 +08:00
parent 3b1bb4a95d
commit c60bb12424
3 changed files with 62 additions and 4 deletions

View File

@ -118,9 +118,8 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
if err := json.NewDecoder(c.Request().Body).Decode(userSettingUpsert); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user setting upsert request").SetInternal(err)
}
if userSettingUpsert.Key.String() == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid user setting key")
if err := userSettingUpsert.Validate(); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid user setting format").SetInternal(err)
}
userSettingUpsert.UserID = userID
@ -191,6 +190,10 @@ func (s *Server) registerUserRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err)
}
if userPatch.Email != nil && !common.ValidateEmail(*userPatch.Email) {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid email format")
}
if userPatch.Password != nil && *userPatch.Password != "" {
passwordHash, err := bcrypt.GenerateFromPassword([]byte(*userPatch.Password), bcrypt.DefaultCost)
if err != nil {