mirror of https://github.com/synctv-org/synctv
Feat: req validate
parent
2752ab48ac
commit
bf1334d1b3
@ -0,0 +1,18 @@
|
||||
package model
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
type Decoder interface {
|
||||
Decode(ctx *gin.Context) error
|
||||
Validate() error
|
||||
}
|
||||
|
||||
func Decode(ctx *gin.Context, decoder Decoder) error {
|
||||
if err := decoder.Decode(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := decoder.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
json "github.com/json-iterator/go"
|
||||
"github.com/synctv-org/synctv/room"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUrlTooLong = errors.New("url too long")
|
||||
ErrEmptyName = errors.New("empty name")
|
||||
ErrNameTooLong = errors.New("name too long")
|
||||
ErrTypeTooLong = errors.New("type too long")
|
||||
|
||||
ErrId = errors.New("id must be greater than 0")
|
||||
|
||||
ErrEmptyIds = errors.New("empty ids")
|
||||
)
|
||||
|
||||
type PushMovieReq room.BaseMovieInfo
|
||||
|
||||
func (p *PushMovieReq) Decode(ctx *gin.Context) error {
|
||||
return json.NewDecoder(ctx.Request.Body).Decode(p)
|
||||
}
|
||||
|
||||
func (p *PushMovieReq) Validate() error {
|
||||
if len(p.Url) > 1024 {
|
||||
return ErrUrlTooLong
|
||||
}
|
||||
|
||||
if p.Name == "" {
|
||||
return ErrEmptyName
|
||||
} else if len(p.Name) > 1024 {
|
||||
return ErrNameTooLong
|
||||
}
|
||||
|
||||
if len(p.Type) > 32 {
|
||||
return ErrTypeTooLong
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type IdReq struct {
|
||||
Id uint64 `json:"id"`
|
||||
}
|
||||
|
||||
func (i *IdReq) Decode(ctx *gin.Context) error {
|
||||
return json.NewDecoder(ctx.Request.Body).Decode(i)
|
||||
}
|
||||
|
||||
func (i *IdReq) Validate() error {
|
||||
if i.Id <= 0 {
|
||||
return ErrId
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type EditMovieReq struct {
|
||||
IdReq
|
||||
PushMovieReq
|
||||
}
|
||||
|
||||
func (e *EditMovieReq) Decode(ctx *gin.Context) error {
|
||||
return json.NewDecoder(ctx.Request.Body).Decode(e)
|
||||
}
|
||||
|
||||
func (e *EditMovieReq) Validate() error {
|
||||
if err := e.IdReq.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := e.PushMovieReq.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type IdsReq struct {
|
||||
Ids []uint64 `json:"ids"`
|
||||
}
|
||||
|
||||
func (i *IdsReq) Decode(ctx *gin.Context) error {
|
||||
return json.NewDecoder(ctx.Request.Body).Decode(i)
|
||||
}
|
||||
|
||||
func (i *IdsReq) Validate() error {
|
||||
if len(i.Ids) == 0 {
|
||||
return ErrEmptyIds
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type SwapMovieReq struct {
|
||||
Id1 uint64 `json:"id1"`
|
||||
Id2 uint64 `json:"id2"`
|
||||
}
|
||||
|
||||
func (s *SwapMovieReq) Decode(ctx *gin.Context) error {
|
||||
return json.NewDecoder(ctx.Request.Body).Decode(s)
|
||||
}
|
||||
|
||||
func (s *SwapMovieReq) Validate() error {
|
||||
if s.Id1 <= 0 || s.Id2 <= 0 {
|
||||
return ErrId
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
|
||||
json "github.com/json-iterator/go"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrEmptyRoomId = errors.New("empty room id")
|
||||
ErrRoomIdTooLong = errors.New("room id too long")
|
||||
ErrRoomIdHasInvalidChar = errors.New("room id has invalid char")
|
||||
|
||||
ErrEmptyPassword = errors.New("empty password")
|
||||
ErrPasswordTooLong = errors.New("password too long")
|
||||
|
||||
ErrEmptyUsername = errors.New("empty username")
|
||||
ErrUsernameTooLong = errors.New("username too long")
|
||||
ErrUsernameHasInvalidChar = errors.New("username has invalid char")
|
||||
)
|
||||
|
||||
var (
|
||||
alphaNumReg = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
|
||||
)
|
||||
|
||||
type CreateRoomReq struct {
|
||||
RoomId string `json:"roomId"`
|
||||
Password string `json:"password"`
|
||||
Username string `json:"username"`
|
||||
UserPassword string `json:"userPassword"`
|
||||
Hidden bool `json:"hidden"`
|
||||
}
|
||||
|
||||
func (c *CreateRoomReq) Decode(ctx *gin.Context) error {
|
||||
return json.NewDecoder(ctx.Request.Body).Decode(c)
|
||||
}
|
||||
|
||||
func (c *CreateRoomReq) Validate() error {
|
||||
if c.RoomId == "" {
|
||||
return ErrEmptyRoomId
|
||||
} else if len(c.RoomId) > 32 {
|
||||
return ErrRoomIdTooLong
|
||||
} else if !alphaNumReg.MatchString(c.RoomId) {
|
||||
return ErrRoomIdHasInvalidChar
|
||||
}
|
||||
|
||||
if len(c.Password) > 32 {
|
||||
return ErrPasswordTooLong
|
||||
}
|
||||
|
||||
if c.Username == "" {
|
||||
return ErrEmptyUsername
|
||||
} else if len(c.Username) > 32 {
|
||||
return ErrUsernameTooLong
|
||||
} else if !alphaNumReg.MatchString(c.Username) {
|
||||
return ErrUsernameHasInvalidChar
|
||||
}
|
||||
|
||||
if c.UserPassword == "" {
|
||||
return ErrEmptyPassword
|
||||
} else if len(c.UserPassword) > 32 {
|
||||
return ErrPasswordTooLong
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type RoomListResp struct {
|
||||
RoomId string `json:"roomId"`
|
||||
PeopleNum int64 `json:"peopleNum"`
|
||||
NeedPassword bool `json:"needPassword"`
|
||||
Creator string `json:"creator"`
|
||||
CreateAt int64 `json:"createAt"`
|
||||
}
|
||||
|
||||
type LoginRoomReq struct {
|
||||
RoomId string `json:"roomId"`
|
||||
Password string `json:"password"`
|
||||
Username string `json:"username"`
|
||||
UserPassword string `json:"userPassword"`
|
||||
}
|
||||
|
||||
func (l *LoginRoomReq) Decode(ctx *gin.Context) error {
|
||||
return json.NewDecoder(ctx.Request.Body).Decode(l)
|
||||
}
|
||||
|
||||
func (l *LoginRoomReq) Validate() error {
|
||||
if l.RoomId == "" {
|
||||
return ErrEmptyRoomId
|
||||
}
|
||||
|
||||
if l.Username == "" {
|
||||
return ErrEmptyUsername
|
||||
}
|
||||
|
||||
if l.UserPassword == "" {
|
||||
return ErrEmptyPassword
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type SetRoomPasswordReq struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func (s *SetRoomPasswordReq) Decode(ctx *gin.Context) error {
|
||||
return json.NewDecoder(ctx.Request.Body).Decode(s)
|
||||
}
|
||||
|
||||
func (s *SetRoomPasswordReq) Validate() error {
|
||||
if len(s.Password) > 32 {
|
||||
return ErrPasswordTooLong
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type UsernameReq struct {
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
func (u *UsernameReq) Decode(ctx *gin.Context) error {
|
||||
return json.NewDecoder(ctx.Request.Body).Decode(u)
|
||||
}
|
||||
|
||||
func (u *UsernameReq) Validate() error {
|
||||
if u.Username == "" {
|
||||
return ErrEmptyUsername
|
||||
} else if len(u.Username) > 32 {
|
||||
return ErrUsernameTooLong
|
||||
} else if !alphaNumReg.MatchString(u.Username) {
|
||||
return ErrUsernameHasInvalidChar
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
json "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
type SetUserPasswordReq struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func (s *SetUserPasswordReq) Decode(ctx *gin.Context) error {
|
||||
return json.NewDecoder(ctx.Request.Body).Decode(s)
|
||||
}
|
||||
|
||||
func (s *SetUserPasswordReq) Validate() error {
|
||||
if s.Password == "" {
|
||||
return ErrEmptyPassword
|
||||
} else if len(s.Password) > 32 {
|
||||
return ErrPasswordTooLong
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue