Feat: model validate update

pull/21/head
zijiren233 2 years ago
parent 43d40ab357
commit a026f82300

@ -26,13 +26,13 @@ func (p *PushMovieReq) Decode(ctx *gin.Context) error {
} }
func (p *PushMovieReq) Validate() error { func (p *PushMovieReq) Validate() error {
if len(p.Url) > 1024 { if len(p.Url) > 8192 {
return ErrUrlTooLong return ErrUrlTooLong
} }
if p.Name == "" { if p.Name == "" {
return ErrEmptyName return ErrEmptyName
} else if len(p.Name) > 1024 { } else if len(p.Name) > 512 {
return ErrNameTooLong return ErrNameTooLong
} }

@ -27,8 +27,9 @@ var (
) )
var ( var (
alphaNumReg = regexp.MustCompile(`^[a-zA-Z0-9_\-]+$`) alnumReg = regexp.MustCompile(`^[[:alnum:]]+$`)
alphaNumChineseReg = regexp.MustCompile(`^[\p{Han}a-zA-Z0-9_\-]+$`) alnumPrintReg = regexp.MustCompile(`^[[:print:][:alnum:]]+$`)
alnumPrintHanReg = regexp.MustCompile(`^[[:print:][:alnum:]\p{Han}]+$`)
) )
type FormatEmptyPasswordError string type FormatEmptyPasswordError string
@ -52,14 +53,14 @@ func (c *CreateRoomReq) Validate() error {
return ErrEmptyRoomName return ErrEmptyRoomName
} else if len(c.RoomName) > 32 { } else if len(c.RoomName) > 32 {
return ErrRoomNameTooLong return ErrRoomNameTooLong
} else if !alphaNumChineseReg.MatchString(c.RoomName) { } else if !alnumPrintHanReg.MatchString(c.RoomName) {
return ErrRoomNameHasInvalidChar return ErrRoomNameHasInvalidChar
} }
if c.Password != "" { if c.Password != "" {
if len(c.Password) > 32 { if len(c.Password) > 32 {
return ErrPasswordTooLong return ErrPasswordTooLong
} else if !alphaNumReg.MatchString(c.Password) { } else if !alnumPrintReg.MatchString(c.Password) {
return ErrPasswordHasInvalidChar return ErrPasswordHasInvalidChar
} }
} else if conf.Conf.Room.MustPassword { } else if conf.Conf.Room.MustPassword {
@ -106,7 +107,7 @@ func (s *SetRoomPasswordReq) Decode(ctx *gin.Context) error {
func (s *SetRoomPasswordReq) Validate() error { func (s *SetRoomPasswordReq) Validate() error {
if len(s.Password) > 32 { if len(s.Password) > 32 {
return ErrPasswordTooLong return ErrPasswordTooLong
} else if !alphaNumReg.MatchString(s.Password) { } else if !alnumPrintReg.MatchString(s.Password) {
return ErrPasswordHasInvalidChar return ErrPasswordHasInvalidChar
} }
return nil return nil

@ -20,7 +20,7 @@ func (s *SetUserPasswordReq) Validate() error {
return FormatEmptyPasswordError("user") return FormatEmptyPasswordError("user")
} else if len(s.Password) > 32 { } else if len(s.Password) > 32 {
return ErrPasswordTooLong return ErrPasswordTooLong
} else if !alphaNumReg.MatchString(s.Password) { } else if !alnumPrintReg.MatchString(s.Password) {
return ErrPasswordHasInvalidChar return ErrPasswordHasInvalidChar
} }
return nil return nil
@ -64,7 +64,7 @@ func (s *SignupUserReq) Validate() error {
return errors.New("username is empty") return errors.New("username is empty")
} else if len(s.Username) > 32 { } else if len(s.Username) > 32 {
return ErrUsernameTooLong return ErrUsernameTooLong
} else if !alphaNumChineseReg.MatchString(s.Username) { } else if !alnumPrintHanReg.MatchString(s.Username) {
return ErrUsernameHasInvalidChar return ErrUsernameHasInvalidChar
} }
@ -72,7 +72,7 @@ func (s *SignupUserReq) Validate() error {
return FormatEmptyPasswordError("user") return FormatEmptyPasswordError("user")
} else if len(s.Password) > 32 { } else if len(s.Password) > 32 {
return ErrPasswordTooLong return ErrPasswordTooLong
} else if !alphaNumReg.MatchString(s.Password) { } else if !alnumPrintReg.MatchString(s.Password) {
return ErrPasswordHasInvalidChar return ErrPasswordHasInvalidChar
} }
return nil return nil

Loading…
Cancel
Save