search_query_set.go
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package query
import (
"fmt"
"github.com/beego/beego/v2/adapter/utils"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type SearchQuerySetQuery struct {
Type string `cname:"类型" json:"type"`
Types []string `cname:"类型" json:"types"`
Flag string `cname:"标识" json:"flag"`
Status int `cname:"状态 1:启用 2:关闭" json:"status"`
MatchName string `cname:"匹配名称" json:"matchName"`
SortByName string `json:"sortByName"`
SortByTime string `json:"sortByTime"`
}
func (searchQuerySetQuery *SearchQuerySetQuery) Valid(validation *validation.Validation) {
optionsValues := []string{"asc", "desc"}
if searchQuerySetQuery.SortByName != "" && !utils.InSlice(strings.ToLower(searchQuerySetQuery.SortByName), optionsValues) {
validation.Error("排序值有误 ASC|DESC")
}
if searchQuerySetQuery.SortByTime != "" && !utils.InSlice(strings.ToLower(searchQuerySetQuery.SortByTime), optionsValues) {
validation.Error("排序值有误 ASC|DESC")
}
}
func (searchQuerySetQuery *SearchQuerySetQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(searchQuerySetQuery)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(searchQuerySetQuery).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}