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;
}
// Sync rules with protocol.go.
export function LoginForm(props: LoginFormProps) {
const classes = useStyles();
const { control, handleSubmit, errors, setValue, register } = useForm<LoginFormData>({});
@ -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}
/>

View File

@ -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

View File

@ -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
}

14
main.go
View File

@ -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
}