From d4d615464563ef6e57a4dccf08742aaadc35401d Mon Sep 17 00:00:00 2001 From: zikaeroh <48577114+zikaeroh@users.noreply.github.com> Date: Mon, 25 May 2020 11:51:25 -0700 Subject: [PATCH] Allow short names --- frontend/src/components/loginForm.tsx | 5 +++-- internal/protocol/protocol.go | 30 +++++++++++++++++---------- internal/server/server.go | 2 +- main.go | 14 ++++++++----- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/frontend/src/components/loginForm.tsx b/frontend/src/components/loginForm.tsx index 81e6c20..e2076ec 100644 --- a/frontend/src/components/loginForm.tsx +++ b/frontend/src/components/loginForm.tsx @@ -32,6 +32,7 @@ export interface LoginFormProps { errorMessage?: string; } +// Sync rules with protocol.go. export function LoginForm(props: LoginFormProps) { const classes = useStyles(); const { control, handleSubmit, errors, setValue, register } = useForm({}); @@ -54,7 +55,7 @@ export function LoginForm(props: LoginFormProps) { label="Nickname" defaultValue="" error={!!errors.nickname} - rules={{ required: true, minLength: 3, maxLength: 16 }} + rules={{ required: true, minLength: 1, maxLength: 16 }} fullWidth={true} inputProps={noComplete} autoFocus @@ -71,7 +72,7 @@ export function LoginForm(props: LoginFormProps) { label="Room name" defaultValue="" error={!!errors.roomName} - rules={{ required: true, minLength: 3, maxLength: 16 }} + rules={{ required: true, minLength: 1, maxLength: 20 }} fullWidth={true} inputProps={noComplete} /> diff --git a/internal/protocol/protocol.go b/internal/protocol/protocol.go index 6668d53..4789416 100644 --- a/internal/protocol/protocol.go +++ b/internal/protocol/protocol.go @@ -23,16 +23,20 @@ type RoomRequest struct { Create bool `json:"create"` } -func (r *RoomRequest) Valid() bool { - if len(r.RoomName) < 3 || len(r.RoomName) > 16 { - return false +func (r *RoomRequest) Valid() (msg string, valid bool) { + if len(r.RoomName) == 0 { + return "Room name cannot be empty.", false + } + + if len(r.RoomName) > 20 { + return "Room name too long.", false } if len(r.RoomPass) == 0 { - return false + return "Room pass cannot be empty.", false } - return true + return "", true } //easyjson:json @@ -58,20 +62,24 @@ type WSQuery struct { Nickname string `queryparam:"nickname"` } -func (w *WSQuery) Valid() bool { +func (w *WSQuery) Valid() (msg string, valid bool) { if w.RoomID == "" { - return false + return "Room ID cannot be empty.", false } if w.PlayerID == uuid.Nil { - return false + return "Player ID cannot be empty", false } - if len(w.Nickname) < 3 || len(w.Nickname) > 16 { - return false + if len(w.Nickname) == 0 { + return "Nickname cannot be empty.", false } - return true + if len(w.Nickname) > 16 { + return "Nickname too long.", false + } + + return "", true } //easyjson:json diff --git a/internal/server/server.go b/internal/server/server.go index b434e86..c50c5b3 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -370,7 +370,7 @@ func (r *Room) handleNote(playerID game.PlayerID, note *protocol.ClientNote) err } // Sync with protocol.go's validation method. - if len(params.Nickname) < 3 || len(params.Nickname) > 16 { + if len(params.Nickname) == 0 || len(params.Nickname) > 16 { return nil } diff --git a/main.go b/main.go index dabb137..b7068b9 100644 --- a/main.go +++ b/main.go @@ -128,15 +128,19 @@ func main() { return } - if !req.Valid() { - httpErr(w, http.StatusBadRequest) + w.Header().Add("Content-Type", "application/json") + + if msg, valid := req.Valid(); !valid { + resp := &protocol.RoomResponse{ + Error: stringPtr(msg), + } + w.WriteHeader(http.StatusBadRequest) + _ = json.NewEncoder(w).Encode(resp) return } resp := &protocol.RoomResponse{} - w.Header().Add("Content-Type", "application/json") - if req.Create { room, err := srv.CreateRoom(req.RoomName, req.RoomPass) if err != nil { @@ -176,7 +180,7 @@ func main() { return } - if !query.Valid() { + if _, valid := query.Valid(); !valid { httpErr(w, http.StatusBadRequest) return }