feat: more validators
This commit is contained in:
@@ -22,7 +22,7 @@ func TestCommand(t *testing.T) {
|
||||
Commands: []json.RawMessage{
|
||||
encodeCommand(&rest.CommandRaceVote{
|
||||
CommandMeta: rest.CommandMeta{Type: rest.CommandTypeRaceVote},
|
||||
Recipient: "AnotherRace",
|
||||
Acceptor: "AnotherRace",
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -63,42 +63,43 @@ func commandRaceQuit(actor string) (Command, error) {
|
||||
}
|
||||
|
||||
func commandRaceVote(actor string, c json.RawMessage) (Command, error) {
|
||||
var v rest.CommandRaceVote
|
||||
if err := json.Unmarshal(c, &v); err != nil {
|
||||
if v, err := unmarshallCommand(c, new(rest.CommandRaceVote)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.RaceVote(actor, v.Acceptor)
|
||||
}, nil
|
||||
}
|
||||
if err := validateCommand(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.RaceVote(actor, v.Recipient)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func commandRaceRelation(actor string, c json.RawMessage) (Command, error) {
|
||||
var v rest.CommandRaceRelation
|
||||
if err := json.Unmarshal(c, &v); err != nil {
|
||||
if v, err := unmarshallCommand(c, new(rest.CommandRaceRelation)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.RaceRelation(actor, v.Acceptor, v.Relation)
|
||||
}, nil
|
||||
}
|
||||
if err := validateCommand(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.RaceRelation(actor, v.Opponent, v.Relation)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func commandShipClassCreate(actor string, c json.RawMessage) (Command, error) {
|
||||
v := new(rest.CommandShipClassCreate)
|
||||
if v, err := unmarshallCommand(c, new(rest.CommandShipClassCreate)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.ShipClassCreate(actor, v.Name, v.Drive, int(v.Armament), v.Weapons, v.Shields, v.Cargo)
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func unmarshallCommand[T rest.DecodableCommand](c json.RawMessage, v *T) (*T, error) {
|
||||
if err := json.Unmarshal(c, v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateCommand(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.ShipClassCreate(actor, v.Name, v.Drive, int(v.Armament), v.Weapons, v.Shields, v.Cargo)
|
||||
}, nil
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func validateCommand(v any) error {
|
||||
|
||||
@@ -54,6 +54,12 @@ func setupRouter(executor handler.CommandExecutor) *gin.Engine {
|
||||
if err := v.RegisterValidation("ammoWeapons", armamentWithWeaponsValidator); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := v.RegisterValidation("entity", entityNameStringValidator); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := v.RegisterValidation("subject", productionTypeStringValidator); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
groupV1 := r.Group("/api/v1")
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/iliadenisov/galaxy/internal/util"
|
||||
)
|
||||
|
||||
var notBlankStringValidator validator.Func = func(fl validator.FieldLevel) bool {
|
||||
@@ -16,6 +17,27 @@ var notBlankStringValidator validator.Func = func(fl validator.FieldLevel) bool
|
||||
return true
|
||||
}
|
||||
|
||||
var entityNameStringValidator validator.Func = func(fl validator.FieldLevel) bool {
|
||||
s, ok := fl.Field().Interface().(string)
|
||||
if ok {
|
||||
if _, ok := util.ValidateTypeName(s); !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
var productionTypeStringValidator validator.Func = func(fl validator.FieldLevel) bool {
|
||||
v, ok := fl.Field().Interface().(string)
|
||||
if ok {
|
||||
f := fl.Parent().FieldByName(fl.Param())
|
||||
if s, ok := f.Interface().(string); ok && (s == "SHIP" || s == "SCIENCE") && len(strings.TrimSpace(v)) == 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
var armamentWithWeaponsValidator validator.Func = func(fl validator.FieldLevel) bool {
|
||||
var v, compareTo float64
|
||||
|
||||
|
||||
Reference in New Issue
Block a user