mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-21 18:20:50 +00:00
feat ✨: 添加字段验证
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
vd "github.com/bytedance/go-tagexpr/v2/validator"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -8,8 +9,8 @@ import (
|
||||
)
|
||||
|
||||
type ObjectById struct {
|
||||
Id int `uri:"id"`
|
||||
Ids []int `json:"ids"`
|
||||
Id int `uri:"id" vd:"($>0||len(Ids)$==0)"`
|
||||
Ids []int `json:"ids" vd:"($==0||len(Ids)$>0)'"`
|
||||
}
|
||||
|
||||
func (s *ObjectById) Bind(ctx *gin.Context) error {
|
||||
@@ -36,6 +37,10 @@ func (s *ObjectById) Bind(ctx *gin.Context) error {
|
||||
s.Ids = append(s.Ids, s.Id)
|
||||
}
|
||||
}
|
||||
if err = vd.Validate(s); err != nil {
|
||||
log.Errorf("Validate error: %s", err.Error())
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -45,4 +50,4 @@ func (s *ObjectById) GetId() interface{} {
|
||||
return s.Ids
|
||||
}
|
||||
return s.Id
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user