-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
204 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
package request | ||
|
||
type App struct { | ||
Slug string `json:"slug" form:"slug" validate:"required"` | ||
Slug string `json:"slug" form:"slug" validate:"required,not_exists=apps slug"` | ||
Channel string `json:"channel" form:"channel" validate:"required"` | ||
} | ||
|
||
type AppSlug struct { | ||
Slug string `json:"slug" form:"slug" validate:"required"` | ||
Slug string `json:"slug" form:"slug" validate:"required,exists=apps slug"` | ||
} | ||
|
||
type AppUpdateShow struct { | ||
Slug string `json:"slug" form:"slug" validate:"required"` | ||
Show bool `json:"show" form:"show" validate:"required"` | ||
Slug string `json:"slug" form:"slug" validate:"required,exists=apps slug"` | ||
Show bool `json:"show" form:"show"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package rule | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/go-playground/validator/v10" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Exists struct { | ||
DB *gorm.DB | ||
} | ||
|
||
func NewExists(db *gorm.DB) *Exists { | ||
return &Exists{DB: db} | ||
} | ||
|
||
// Exists 格式 `exists=categories,id,other_field` | ||
func (r *Exists) Exists(fl validator.FieldLevel) bool { | ||
requestValue := fl.Field().Interface() | ||
params := strings.Fields(fl.Param()) | ||
if len(params) < 2 { | ||
return false | ||
} | ||
|
||
tableName := params[0] | ||
fieldNames := params[1:] | ||
|
||
query := r.DB.Table(tableName).Where(fmt.Sprintf("%s = ?", fieldNames[0]), requestValue) | ||
for _, fieldName := range fieldNames[1:] { | ||
query = query.Or(fmt.Sprintf("%s = ?", fieldName), requestValue) | ||
} | ||
|
||
var count int64 | ||
err := query.Count(&count).Error | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return count != 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package rule | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/go-playground/validator/v10" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type NotExists struct { | ||
DB *gorm.DB | ||
} | ||
|
||
func NewNotExists(db *gorm.DB) *NotExists { | ||
return &NotExists{DB: db} | ||
} | ||
|
||
// NotExists 格式 `not_exists=categories,id,other_field` | ||
func (r *NotExists) NotExists(fl validator.FieldLevel) bool { | ||
requestValue := fl.Field().Interface() | ||
params := strings.Fields(fl.Param()) | ||
if len(params) < 2 { | ||
return false | ||
} | ||
|
||
tableName := params[0] | ||
fieldNames := params[1:] | ||
|
||
query := r.DB.Table(tableName).Where(fmt.Sprintf("%s = ?", fieldNames[0]), requestValue) | ||
for _, fieldName := range fieldNames[1:] { | ||
query = query.Or(fmt.Sprintf("%s = ?", fieldName), requestValue) | ||
} | ||
|
||
var count int64 | ||
err := query.Count(&count).Error | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return count == 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package rule | ||
|
||
import ( | ||
"regexp" | ||
|
||
"github.com/go-playground/validator/v10" | ||
) | ||
|
||
type Regexp struct{} | ||
|
||
func NewRegexp() *Regexp { | ||
return &Regexp{} | ||
} | ||
|
||
func (r *Regexp) Regexp(fl validator.FieldLevel) bool { | ||
// 从标签中获取正则,格式类似于 `regexp=^[a-zA-Z0-9_]+$` | ||
pattern := fl.Param() | ||
value := fl.Field().String() | ||
|
||
re, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return re.MatchString(value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package rule | ||
|
||
import ( | ||
ut "github.com/go-playground/universal-translator" | ||
"github.com/go-playground/validator/v10" | ||
|
||
"github.com/TheTNB/panel/internal/app" | ||
) | ||
|
||
func RegisterRules(v *validator.Validate) error { | ||
if err := v.RegisterValidation("exists", NewExists(app.Orm).Exists); err != nil { | ||
return err | ||
} | ||
if err := v.RegisterValidation("not_exists", NewNotExists(app.Orm).NotExists); err != nil { | ||
return err | ||
} | ||
if err := v.RegisterValidation("regexp", NewRegexp().Regexp); err != nil { | ||
return err | ||
} | ||
|
||
if err := v.RegisterTranslation("exists", *app.Translator, | ||
func(ut ut.Translator) error { | ||
return ut.Add("exists", "{0} 不存在", true) | ||
}, | ||
func(ut ut.Translator, fe validator.FieldError) string { | ||
t, _ := ut.T("exists", fe.Field()) | ||
return t | ||
}); err != nil { | ||
return err | ||
} | ||
if err := v.RegisterTranslation("not_exists", *app.Translator, | ||
func(ut ut.Translator) error { | ||
return ut.Add("not_exists", "{0} 已存在", true) | ||
}, | ||
func(ut ut.Translator, fe validator.FieldError) string { | ||
t, _ := ut.T("not_exists", fe.Field()) | ||
return t | ||
}); err != nil { | ||
return err | ||
} | ||
if err := v.RegisterTranslation("regexp", *app.Translator, | ||
func(ut ut.Translator) error { | ||
return ut.Add("regexp", "{0} 格式不正确", true) | ||
}, | ||
func(ut ut.Translator, fe validator.FieldError) string { | ||
t, _ := ut.T("regexp", fe.Field()) | ||
return t | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.