From 6cc4d4c41d25f3b5ad288de0b9a083dc0b409f72 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 17 Jun 2022 16:10:32 +0200 Subject: [PATCH] fix(api): don't panic if PATCH /users/@me does not have "fields" set --- backend/routes/user/patch_user.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/backend/routes/user/patch_user.go b/backend/routes/user/patch_user.go index 168b437..cb00bc7 100644 --- a/backend/routes/user/patch_user.go +++ b/backend/routes/user/patch_user.go @@ -71,20 +71,22 @@ func (s *Server) patchUser(w http.ResponseWriter, r *http.Request) error { } } - // max 25 fields - if len(*req.Fields) > db.MaxFields { - return server.APIError{ - Code: server.ErrBadRequest, - Details: fmt.Sprintf("Too many fields (max %d, current %d)", db.MaxFields, len(*req.Fields)), - } - } - - // validate all fields - for i, field := range *req.Fields { - if s := field.Validate(); s != "" { + if (req.Fields) != nil { + // max 25 fields + if len(*req.Fields) > db.MaxFields { return server.APIError{ Code: server.ErrBadRequest, - Details: fmt.Sprintf("field %d: %s", i, s), + Details: fmt.Sprintf("Too many fields (max %d, current %d)", db.MaxFields, len(*req.Fields)), + } + } + + // validate all fields + for i, field := range *req.Fields { + if s := field.Validate(); s != "" { + return server.APIError{ + Code: server.ErrBadRequest, + Details: fmt.Sprintf("field %d: %s", i, s), + } } } }