generated from VLADIMIR/template
fix
This commit is contained in:
@@ -30,6 +30,7 @@ func TestTeamRegistrationHandler(t *testing.T) {
|
||||
{"extra object", "secret", "Bearer secret", "42", "delivery", valid + ` {}`, nil, 400, false},
|
||||
{"unknown field", "secret", "Bearer secret", "42", "delivery", `{"extra":1}`, nil, 400, false},
|
||||
{"jsonrpc", "secret", "Bearer secret", "42", "delivery", `{"jsonrpc":"2.0","method":"register","params":{"name":"Команда","email":"captain@example.org","participantsCount":5},"id":1}`, nil, 200, true},
|
||||
{"jsonrpc string count", "secret", "Bearer secret", "42", "delivery", `{"jsonrpc":"2.0","method":"register","params":{"name":"Команда","email":"captain@example.org","participantsCount":"5"},"id":1}`, nil, 200, true},
|
||||
{"too large", "secret", "Bearer secret", "42", "delivery", `{"name":"` + strings.Repeat("a", 17000) + `"}`, nil, 400, false},
|
||||
{"invalid fields", "secret", "Bearer secret", "42", "delivery", valid, game_service.ErrInvalidRegistration, 400, true},
|
||||
{"closed", "secret", "Bearer secret", "42", "delivery", valid, teams_repo.ErrRegistrationClosed, 409, true},
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package game_service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/mail"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
@@ -16,6 +19,37 @@ type Registration struct {
|
||||
ParticipantsCount int32 `json:"participantsCount"`
|
||||
}
|
||||
|
||||
func (r *Registration) UnmarshalJSON(data []byte) error {
|
||||
var payload struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
ParticipantsCount json.RawMessage `json:"participantsCount"`
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&payload); err != nil {
|
||||
return err
|
||||
}
|
||||
r.Name = payload.Name
|
||||
r.Email = payload.Email
|
||||
if len(payload.ParticipantsCount) == 0 {
|
||||
return nil
|
||||
}
|
||||
if err := json.Unmarshal(payload.ParticipantsCount, &r.ParticipantsCount); err == nil {
|
||||
return nil
|
||||
}
|
||||
var value string
|
||||
if err := json.Unmarshal(payload.ParticipantsCount, &value); err != nil {
|
||||
return errors.New(`поле "participantsCount" должно быть целым числом или строкой с целым числом`)
|
||||
}
|
||||
count, err := strconv.ParseInt(value, 10, 32)
|
||||
if err != nil {
|
||||
return errors.New(`поле "participantsCount" должно быть целым числом или строкой с целым числом`)
|
||||
}
|
||||
r.ParticipantsCount = int32(count)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registration) Validate() error {
|
||||
r.Name = strings.TrimSpace(r.Name)
|
||||
r.Email = strings.TrimSpace(r.Email)
|
||||
|
||||
Reference in New Issue
Block a user