chore: update userinfo validator (#868)

* chore: update userinfo validator

* chore: update actions

* chore: update
This commit is contained in:
boojack
2022-12-27 21:51:43 +08:00
committed by GitHub
parent c160bed403
commit f888c62840
6 changed files with 90 additions and 9 deletions

View File

@ -2,6 +2,8 @@ package api
import (
"fmt"
"github.com/usememos/memos/common"
)
// Role is the type of a role.
@ -61,9 +63,23 @@ func (create UserCreate) Validate() error {
if len(create.Username) < 4 {
return fmt.Errorf("username is too short, minimum length is 4")
}
if len(create.Username) > 32 {
return fmt.Errorf("username is too long, maximum length is 32")
}
if len(create.Password) < 4 {
return fmt.Errorf("password is too short, minimum length is 4")
}
if len(create.Nickname) > 64 {
return fmt.Errorf("nickname is too long, maximum length is 64")
}
if create.Email != "" {
if len(create.Email) > 256 {
return fmt.Errorf("email is too long, maximum length is 256")
}
if common.ValidateEmail(create.Email) {
return fmt.Errorf("invalid email format")
}
}
return nil
}
@ -85,6 +101,31 @@ type UserPatch struct {
OpenID *string
}
func (patch UserPatch) Validate() error {
if patch.Username != nil && len(*patch.Username) < 4 {
return fmt.Errorf("username is too short, minimum length is 4")
}
if patch.Username != nil && len(*patch.Username) > 32 {
return fmt.Errorf("username is too long, maximum length is 32")
}
if patch.Password != nil && len(*patch.Password) < 4 {
return fmt.Errorf("password is too short, minimum length is 4")
}
if patch.Nickname != nil && len(*patch.Nickname) > 64 {
return fmt.Errorf("nickname is too long, maximum length is 64")
}
if patch.Email != nil {
if len(*patch.Email) > 256 {
return fmt.Errorf("email is too long, maximum length is 256")
}
if common.ValidateEmail(*patch.Email) {
return fmt.Errorf("invalid email format")
}
}
return nil
}
type UserFind struct {
ID *int `json:"id"`