Allow short names

This commit is contained in:
zikaeroh 2020-05-25 11:51:25 -07:00
parent 13d547622e
commit d4d6154645
4 changed files with 32 additions and 19 deletions

View File

@ -32,6 +32,7 @@ export interface LoginFormProps {
errorMessage?: string; errorMessage?: string;
} }
// Sync rules with protocol.go.
export function LoginForm(props: LoginFormProps) { export function LoginForm(props: LoginFormProps) {
const classes = useStyles(); const classes = useStyles();
const { control, handleSubmit, errors, setValue, register } = useForm<LoginFormData>({}); const { control, handleSubmit, errors, setValue, register } = useForm<LoginFormData>({});
@ -54,7 +55,7 @@ export function LoginForm(props: LoginFormProps) {
label="Nickname" label="Nickname"
defaultValue="" defaultValue=""
error={!!errors.nickname} error={!!errors.nickname}
rules={{ required: true, minLength: 3, maxLength: 16 }} rules={{ required: true, minLength: 1, maxLength: 16 }}
fullWidth={true} fullWidth={true}
inputProps={noComplete} inputProps={noComplete}
autoFocus autoFocus
@ -71,7 +72,7 @@ export function LoginForm(props: LoginFormProps) {
label="Room name" label="Room name"
defaultValue="" defaultValue=""
error={!!errors.roomName} error={!!errors.roomName}
rules={{ required: true, minLength: 3, maxLength: 16 }} rules={{ required: true, minLength: 1, maxLength: 20 }}
fullWidth={true} fullWidth={true}
inputProps={noComplete} inputProps={noComplete}
/> />

View File

@ -23,16 +23,20 @@ type RoomRequest struct {
Create bool `json:"create"` Create bool `json:"create"`
} }
func (r *RoomRequest) Valid() bool { func (r *RoomRequest) Valid() (msg string, valid bool) {
if len(r.RoomName) < 3 || len(r.RoomName) > 16 { if len(r.RoomName) == 0 {
return false return "Room name cannot be empty.", false
}
if len(r.RoomName) > 20 {
return "Room name too long.", false
} }
if len(r.RoomPass) == 0 { if len(r.RoomPass) == 0 {
return false return "Room pass cannot be empty.", false
} }
return true return "", true
} }
//easyjson:json //easyjson:json
@ -58,20 +62,24 @@ type WSQuery struct {
Nickname string `queryparam:"nickname"` Nickname string `queryparam:"nickname"`
} }
func (w *WSQuery) Valid() bool { func (w *WSQuery) Valid() (msg string, valid bool) {
if w.RoomID == "" { if w.RoomID == "" {
return false return "Room ID cannot be empty.", false
} }
if w.PlayerID == uuid.Nil { if w.PlayerID == uuid.Nil {
return false return "Player ID cannot be empty", false
} }
if len(w.Nickname) < 3 || len(w.Nickname) > 16 { if len(w.Nickname) == 0 {
return false return "Nickname cannot be empty.", false
} }
return true if len(w.Nickname) > 16 {
return "Nickname too long.", false
}
return "", true
} }
//easyjson:json //easyjson:json

View File

@ -370,7 +370,7 @@ func (r *Room) handleNote(playerID game.PlayerID, note *protocol.ClientNote) err
} }
// Sync with protocol.go's validation method. // 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 return nil
} }

14
main.go
View File

@ -128,15 +128,19 @@ func main() {
return return
} }
if !req.Valid() { w.Header().Add("Content-Type", "application/json")
httpErr(w, http.StatusBadRequest)
if msg, valid := req.Valid(); !valid {
resp := &protocol.RoomResponse{
Error: stringPtr(msg),
}
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(resp)
return return
} }
resp := &protocol.RoomResponse{} resp := &protocol.RoomResponse{}
w.Header().Add("Content-Type", "application/json")
if req.Create { if req.Create {
room, err := srv.CreateRoom(req.RoomName, req.RoomPass) room, err := srv.CreateRoom(req.RoomName, req.RoomPass)
if err != nil { if err != nil {
@ -176,7 +180,7 @@ func main() {
return return
} }
if !query.Valid() { if _, valid := query.Valid(); !valid {
httpErr(w, http.StatusBadRequest) httpErr(w, http.StatusBadRequest)
return return
} }