feat(valid): 更新验证功能支持多种绑定方式

- 修改 ValidToStruct 函数使用 c.Bind 替代 c.BindJSON
- 新增 ValidToMap 函数支持将参数验证后转换为 map 类型
- 更新 ValidToStructAndMap 函数使用 c.Bind 进行参数绑定
- 添加相应的错误处理和类型转换逻辑
- 更新项目缓存配置文件中的文件扫描路径信息
main
black1552 2026-02-04 16:19:36 +08:00
parent 8927551779
commit 258112d38e
2 changed files with 59 additions and 4 deletions

View File

@ -90,6 +90,7 @@
<entry key="NewsOne">
<value>
<set>
<option value="file://$PROJECT_DIR$/../gin_test/api/new.go" />
<option value="file://$PROJECT_DIR$/../gin_test/req/new.go" />
</set>
</value>
@ -97,6 +98,7 @@
<entry key="NewsSave">
<value>
<set>
<option value="file://$PROJECT_DIR$/../gin_test/api/new.go" />
<option value="file://$PROJECT_DIR$/../gin_test/req/new.go" />
</set>
</value>
@ -340,7 +342,7 @@
<entry key="file://$PROJECT_DIR$/valid/valid.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770188678912" />
<option name="lastModified" value="1770192971883" />
</ScannedPath>
</value>
</entry>
@ -366,6 +368,47 @@
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/api/home.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770190095707" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/api/new.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770191206242" />
<option name="schema">
<list>
<option value="NewsOne" />
<option value="NewsSave" />
</list>
</option>
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/controller/home/houeRouter.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770191001042" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/controller/home/index.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770191170351" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/controller/home/router.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770190557848" />
</ScannedPath>
</value>
</entry>
<entry key="file://$PROJECT_DIR$/../gin_test/req/new.go">
<value>
<ScannedPath>
@ -382,7 +425,7 @@
<entry key="file://$PROJECT_DIR$/../gin_test/router/router.go">
<value>
<ScannedPath>
<option name="lastModified" value="1770188593199" />
<option name="lastModified" value="1770191024510" />
</ScannedPath>
</value>
</entry>

View File

@ -10,7 +10,7 @@ import (
// ValidToStruct 验证参数并返回结构体
func ValidToStruct[T any](c *gin.Context) (object *T) {
obj := new(T)
if err := c.BindJSON(obj); err != nil {
if err := c.Bind(obj); err != nil {
panic(err)
}
if err := g.Validator().Data(obj).Run(c); err != nil {
@ -19,10 +19,22 @@ func ValidToStruct[T any](c *gin.Context) (object *T) {
return obj
}
// ValidToMap 验证参数并返回结构体
func ValidToMap[T any](c *gin.Context) (object map[string]any) {
obj := new(T)
if err := c.Bind(obj); err != nil {
panic(err)
}
if err := g.Validator().Data(obj).Run(c); err != nil {
panic(gerror.Current(err).Error())
}
return gconv.Map(obj)
}
// ValidToStructAndMap 验证参数并返回map
func ValidToStructAndMap[T any](c *gin.Context) (stru *T, object map[string]any) {
obj := new(T)
if err := c.BindJSON(obj); err != nil {
if err := c.Bind(obj); err != nil {
panic(err)
}
if err := g.Validator().Data(obj).Run(c); err != nil {