feat ✨: 添加字段验证
This commit is contained in:
1 parent
221a47423d
commit
14db1e802c
8 files changed
+72
-33
No files matched your search
@@ -1,6 +1,8 @@
|
|||||||
package dto
|
package dto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
vd "github.com/bytedance/go-tagexpr/v2/validator"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-admin-team/go-admin-core/sdk/api"
|
"github.com/go-admin-team/go-admin-core/sdk/api"
|
||||||
|
|
||||||
@@ -11,9 +13,9 @@ import (
|
|||||||
|
|
||||||
type SysCategorySearch struct {
|
type SysCategorySearch struct {
|
||||||
dto.Pagination `search:"-"`
|
dto.Pagination `search:"-"`
|
||||||
Name string `form:"name" search:"type:exact;column:name;table:sys_category" comment:"名称"`
|
Name string `form:"name" search:"type:exact;column:name;table:sys_category" comment:"名称" vd:"?"`
|
||||||
Status string `form:"status" search:"type:exact;column:status;table:sys_category" comment:"状态"`
|
Status string `form:"status" search:"type:exact;column:status;table:sys_category" comment:"状态" vd:"?"`
|
||||||
CateId int `form:"cateId" search:"type:exact;column:cate_id;table:sys_category" comment:"分类id"`
|
CateId int `form:"cateId" search:"type:exact;column:cate_id;table:sys_category" comment:"分类id" vd:"?"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *SysCategorySearch) GetNeedSearch() interface{} {
|
func (m *SysCategorySearch) GetNeedSearch() interface{} {
|
||||||
@@ -22,10 +24,16 @@ func (m *SysCategorySearch) GetNeedSearch() interface{} {
|
|||||||
|
|
||||||
func (m *SysCategorySearch) Bind(ctx *gin.Context) error {
|
func (m *SysCategorySearch) Bind(ctx *gin.Context) error {
|
||||||
log := api.GetRequestLogger(ctx)
|
log := api.GetRequestLogger(ctx)
|
||||||
|
|
||||||
err := ctx.ShouldBind(m)
|
err := ctx.ShouldBind(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("ShouldBind error: %s", err.Error())
|
log.Debugf("ShouldBind error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err = vd.Validate(m); err != nil {
|
||||||
|
log.Errorf("Validate error: %s", err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,23 +44,28 @@ func (m *SysCategorySearch) Generate() dto.Index {
|
|||||||
|
|
||||||
type SysCategoryControl struct {
|
type SysCategoryControl struct {
|
||||||
ID int `uri:"Id" comment:"标识"`
|
ID int `uri:"Id" comment:"标识"`
|
||||||
Name string `json:"name" comment:"名称"`
|
Name string `json:"name" comment:"名称" vd:"len($)>0 && $!=' '; msg:'invalid name: 不能是空字符串'"`
|
||||||
Img string `json:"img" comment:"图标"`
|
Img string `json:"img" comment:"图标" vd:"?"`
|
||||||
Sort int `json:"sort" comment:"排序"`
|
Sort int `json:"sort" comment:"排序" vd:"?"`
|
||||||
Status int `json:"status" comment:"状态"`
|
Status int `json:"status" comment:"状态" vd:"$>0; msg:'invalid status: 状态无效'"`
|
||||||
Remark string `json:"remark" comment:"备注"`
|
Remark string `json:"remark" comment:"备注" vd:"?"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SysCategoryControl) Bind(ctx *gin.Context) error {
|
func (s *SysCategoryControl) Bind(ctx *gin.Context) error {
|
||||||
log := api.GetRequestLogger(ctx)
|
log := api.GetRequestLogger(ctx)
|
||||||
err := ctx.ShouldBindUri(s)
|
err := ctx.ShouldBindUri(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("ShouldBindUri error: %s", err.Error())
|
log.Errorf("ShouldBindUri error: %s", err.Error())
|
||||||
return err
|
return errors.New("数据绑定出错")
|
||||||
}
|
}
|
||||||
err = ctx.ShouldBind(s)
|
err = ctx.ShouldBind(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("ShouldBind error: %s", err.Error())
|
log.Errorf("ShouldBind error: %s", err.Error())
|
||||||
|
err = errors.New("数据绑定出错")
|
||||||
|
}
|
||||||
|
if err1 := vd.Validate(s); err != nil {
|
||||||
|
log.Errorf("Validate error: %s", err1.Error())
|
||||||
|
return err1
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -78,7 +91,7 @@ func (s *SysCategoryControl) GetId() interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SysCategoryById struct {
|
type SysCategoryById struct {
|
||||||
dto.ObjectById
|
dto.ObjectById `vd:"?"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SysCategoryById) Generate() dto.Control {
|
func (s *SysCategoryById) Generate() dto.Control {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ func CreateAction(control dto.Control) gin.HandlerFunc {
|
|||||||
req := control.Generate()
|
req := control.Generate()
|
||||||
err = req.Bind(c)
|
err = req.Bind(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
|
response.Error(c, http.StatusUnprocessableEntity, err, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var object models.ActiveRecord
|
var object models.ActiveRecord
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dto
|
package dto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
vd "github.com/bytedance/go-tagexpr/v2/validator"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -8,8 +9,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ObjectById struct {
|
type ObjectById struct {
|
||||||
Id int `uri:"id"`
|
Id int `uri:"id" vd:"($>0||len(Ids)$==0)"`
|
||||||
Ids []int `json:"ids"`
|
Ids []int `json:"ids" vd:"($==0||len(Ids)$>0)'"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ObjectById) Bind(ctx *gin.Context) error {
|
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)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-4
@@ -31,10 +31,14 @@ settings:
|
|||||||
driver: mysql
|
driver: mysql
|
||||||
# 数据库连接字符串 mysql 缺省信息 charset=utf8&parseTime=True&loc=Local&timeout=1000ms
|
# 数据库连接字符串 mysql 缺省信息 charset=utf8&parseTime=True&loc=Local&timeout=1000ms
|
||||||
source: user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
|
source: user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
|
||||||
databases:
|
# databases:
|
||||||
registers:
|
# 'locaohost:8000':
|
||||||
- sources:
|
# driver: mysql
|
||||||
- user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
|
# # 数据库连接字符串 mysql 缺省信息 charset=utf8&parseTime=True&loc=Local&timeout=1000ms
|
||||||
|
# source: user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
|
||||||
|
# registers:
|
||||||
|
# - sources:
|
||||||
|
# - user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local&timeout=1000ms
|
||||||
gen:
|
gen:
|
||||||
# 代码生成读取的数据库名称
|
# 代码生成读取的数据库名称
|
||||||
dbname: dbname
|
dbname: dbname
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ require (
|
|||||||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
|
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
|
||||||
github.com/alibaba/sentinel-golang v0.6.1
|
github.com/alibaba/sentinel-golang v0.6.1
|
||||||
github.com/aliyun/aliyun-oss-go-sdk v0.0.0-20190307165228-86c17b95fcd5
|
github.com/aliyun/aliyun-oss-go-sdk v0.0.0-20190307165228-86c17b95fcd5
|
||||||
|
github.com/bytedance/go-tagexpr/v2 v2.7.10
|
||||||
github.com/casbin/casbin/v2 v2.25.1
|
github.com/casbin/casbin/v2 v2.25.1
|
||||||
github.com/gin-gonic/gin v1.6.3
|
github.com/gin-gonic/gin v1.6.3
|
||||||
github.com/go-admin-team/go-admin-core v1.3.3
|
github.com/go-admin-team/go-admin-core v1.3.3
|
||||||
@@ -27,5 +28,5 @@ require (
|
|||||||
gorm.io/driver/mysql v1.0.4-0.20201206014609-ae5fd10184f6
|
gorm.io/driver/mysql v1.0.4-0.20201206014609-ae5fd10184f6
|
||||||
gorm.io/driver/postgres v1.0.6-0.20201208020313-1ed927cfab53
|
gorm.io/driver/postgres v1.0.6-0.20201208020313-1ed927cfab53
|
||||||
gorm.io/driver/sqlite v1.1.5-0.20201206014648-c84401fbe3ba
|
gorm.io/driver/sqlite v1.1.5-0.20201206014648-c84401fbe3ba
|
||||||
gorm.io/gorm v1.21.3
|
gorm.io/gorm v1.21.6
|
||||||
)
|
)
|
||||||
+27
-11
@@ -1,6 +1,8 @@
|
|||||||
package dto
|
package dto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
vd "github.com/bytedance/go-tagexpr/v2/validator"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-admin-team/go-admin-core/sdk/api"
|
"github.com/go-admin-team/go-admin-core/sdk/api"
|
||||||
|
|
||||||
@@ -10,12 +12,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type {{.ClassName}}Search struct {
|
type {{.ClassName}}Search struct {
|
||||||
dto.Pagination `search:"-"`
|
dto.Pagination `search:"-" vd:"-"`
|
||||||
{{ $tablename := .TBName -}}
|
{{ $tablename := .TBName -}}
|
||||||
{{ range .Columns -}}
|
{{ range .Columns -}}
|
||||||
{{$z := .IsQuery}}
|
{{$z := .IsQuery}}
|
||||||
{{- if ($z) -}}
|
{{- if ($z) -}}
|
||||||
{{.GoField}} {{.GoType}} `form:"{{.JsonField}}" search:"type:{{if eq .QueryType "EQ"}}exact{{ else if eq .QueryType "NE"}}iexact{{ else if eq .QueryType "LIKE"}}contains{{ else if eq .QueryType "GT"}}gt{{ else if eq .QueryType "GTE"}}gte{{ else if eq .QueryType "LT"}}lt{{ else if eq .QueryType "LTE"}}lte{{- end }};column:{{.ColumnName}};table:{{$tablename}}" comment:"{{.ColumnComment}}"`
|
{{.GoField}} {{.GoType}} `form:"{{.JsonField}}" vd:"?" search:"type:{{if eq .QueryType "EQ"}}exact{{ else if eq .QueryType "NE"}}iexact{{ else if eq .QueryType "LIKE"}}contains{{ else if eq .QueryType "GT"}}gt{{ else if eq .QueryType "GTE"}}gte{{ else if eq .QueryType "LT"}}lt{{ else if eq .QueryType "LTE"}}lte{{- end }};column:{{.ColumnName}};table:{{$tablename}}" comment:"{{.ColumnComment}}"`
|
||||||
{{ end -}}
|
{{ end -}}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
}
|
}
|
||||||
@@ -33,7 +35,11 @@ func (m *{{.ClassName}}Search) Bind(ctx *gin.Context) error {
|
|||||||
log := api.GetRequestLogger(ctx)
|
log := api.GetRequestLogger(ctx)
|
||||||
err := ctx.ShouldBind(m)
|
err := ctx.ShouldBind(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("ShouldBind error: %s", err.Error())
|
log.Errorf("ShouldBind error: %s", err.Error())
|
||||||
|
}
|
||||||
|
if err1 := vd.Validate(s); err != nil {
|
||||||
|
log.Errorf("Validate error: %s", err1.Error())
|
||||||
|
return err1
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -42,14 +48,14 @@ type {{.ClassName}}Control struct {
|
|||||||
{{ range .Columns -}}
|
{{ range .Columns -}}
|
||||||
{{$x := .Pk}}
|
{{$x := .Pk}}
|
||||||
{{- if ($x) }}
|
{{- if ($x) }}
|
||||||
{{.GoField}} {{.GoType}} `uri:"{{.JsonField}}" comment:"{{.ColumnComment}}"` // {{.ColumnComment}}
|
{{.GoField}} {{.GoType}} `uri:"{{.JsonField}}" comment:"{{.ColumnComment}}" vd:"?"` // {{.ColumnComment}}
|
||||||
{{- else if eq .GoField "CreatedAt" -}}
|
{{- else if eq .GoField "CreatedAt" -}}
|
||||||
{{- else if eq .GoField "UpdatedAt" -}}
|
{{- else if eq .GoField "UpdatedAt" -}}
|
||||||
{{- else if eq .GoField "DeletedAt" -}}
|
{{- else if eq .GoField "DeletedAt" -}}
|
||||||
{{- else if eq .GoField "CreateBy" -}}
|
{{- else if eq .GoField "CreateBy" -}}
|
||||||
{{- else if eq .GoField "UpdateBy" -}}
|
{{- else if eq .GoField "UpdateBy" -}}
|
||||||
{{- else }}
|
{{- else }}
|
||||||
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"`
|
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}" vd:"?"`
|
||||||
{{end -}}
|
{{end -}}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
}
|
}
|
||||||
@@ -63,12 +69,17 @@ func (s *{{.ClassName}}Control) Bind(ctx *gin.Context) error {
|
|||||||
log := api.GetRequestLogger(ctx)
|
log := api.GetRequestLogger(ctx)
|
||||||
err := ctx.ShouldBindUri(s)
|
err := ctx.ShouldBindUri(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("ShouldBindUri error: %s", err.Error())
|
log.Errorf("ShouldBindUri error: %s", err.Error())
|
||||||
return err
|
return errors.New("数据绑定出错")
|
||||||
}
|
}
|
||||||
err = ctx.ShouldBind(s)
|
err = ctx.ShouldBind(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("ShouldBind error: %s", err.Error())
|
log.Errorf("ShouldBind error: %s", err.Error())
|
||||||
|
err = errors.New("数据绑定出错")
|
||||||
|
}
|
||||||
|
if err1 := vd.Validate(s); err != nil {
|
||||||
|
log.Errorf("Validate error: %s", err1.Error())
|
||||||
|
return err1
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -111,12 +122,17 @@ func (s *{{.ClassName}}ById) Bind(ctx *gin.Context) error {
|
|||||||
log := api.GetRequestLogger(ctx)
|
log := api.GetRequestLogger(ctx)
|
||||||
err := ctx.ShouldBindUri(s)
|
err := ctx.ShouldBindUri(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("ShouldBindUri error: %s", err.Error())
|
log.Errorf("ShouldBindUri error: %s", err.Error())
|
||||||
return err
|
return errors.New("数据绑定出错")
|
||||||
}
|
}
|
||||||
err = ctx.ShouldBind(s)
|
err = ctx.ShouldBind(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("ShouldBind error: %s", err.Error())
|
log.Errorf("ShouldBind error: %s", err.Error())
|
||||||
|
err = errors.New("数据绑定出错")
|
||||||
|
}
|
||||||
|
if err1 := vd.Validate(s); err != nil {
|
||||||
|
log.Errorf("Validate error: %s", err1.Error())
|
||||||
|
return err1
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ func init() {
|
|||||||
routerCheckRole = append(routerCheckRole, register{{.ClassName}}Router)
|
routerCheckRole = append(routerCheckRole, register{{.ClassName}}Router)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 需认证的路由代码
|
// register{{.ClassName}}Router
|
||||||
func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
||||||
api := &{{.ModuleName}}.{{.ClassName}}{}
|
api := &{{.ModuleName}}.{{.ClassName}}{}
|
||||||
r := v1.Group("/{{.ModuleName}}").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
|
r := v1.Group("/{{.ModuleName}}").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ func init() {
|
|||||||
routerCheckRole = append(routerCheckRole, register{{.ClassName}}Router)
|
routerCheckRole = append(routerCheckRole, register{{.ClassName}}Router)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 需认证的路由代码
|
// register{{.ClassName}}Router
|
||||||
func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
||||||
api := &{{.ModuleName}}.{{.ClassName}}{}
|
api := &{{.ModuleName}}.{{.ClassName}}{}
|
||||||
r := v1.Group("/{{.ModuleName}}").Use(authMiddleware.MiddlewareFunc())
|
r := v1.Group("/{{.ModuleName}}").Use(authMiddleware.MiddlewareFunc())
|
||||||
|
|||||||
Reference in new issue
Block a user