gf/util/gvalid/gvalid_validator_check_map.go

137 lines
3.5 KiB
Go
Raw Normal View History

2021-01-12 10:46:39 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gvalid
import (
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/util/gconv"
2021-01-12 10:46:39 +08:00
"strings"
)
2020-05-10 17:49:23 +08:00
// CheckMap validates map and returns the error result. It returns nil if with successful validation.
// The parameter `params` should be type of map.
func (v *Validator) CheckMap(params interface{}) Error {
return v.doCheckMap(params)
}
func (v *Validator) doCheckMap(params interface{}) Error {
2020-12-09 21:00:30 +08:00
// If there's no validation rules, it does nothing and returns quickly.
if params == nil || v.rules == nil {
2020-12-09 21:00:30 +08:00
return nil
2019-06-19 09:06:52 +08:00
}
2020-05-10 16:48:00 +08:00
var (
checkRules = make(map[string]string)
customMsgs = make(CustomMsg)
errorRules = make([]string, 0)
errorMaps = make(map[string]map[string]string)
2020-05-10 16:48:00 +08:00
)
switch v := v.rules.(type) {
2020-05-10 17:49:23 +08:00
// Sequence tag: []sequence tag
// Sequence has order for error results.
2019-06-19 09:06:52 +08:00
case []string:
for _, tag := range v {
name, rule, msg := parseSequenceTag(tag)
if len(name) == 0 {
continue
}
if len(msg) > 0 {
2020-05-10 17:49:23 +08:00
var (
msgArray = strings.Split(msg, "|")
ruleArray = strings.Split(rule, "|")
)
2019-06-19 09:06:52 +08:00
for k, v := range ruleArray {
2020-05-10 17:49:23 +08:00
// If length of custom messages is lesser than length of rules,
// the rest rules use the default error messages.
2019-06-19 09:06:52 +08:00
if len(msgArray) <= k {
continue
}
if len(msgArray[k]) == 0 {
continue
}
array := strings.Split(v, ":")
if _, ok := customMsgs[name]; !ok {
customMsgs[name] = make(map[string]string)
}
customMsgs[name].(map[string]string)[strings.TrimSpace(array[0])] = strings.TrimSpace(msgArray[k])
}
}
checkRules[name] = rule
errorRules = append(errorRules, name+"@"+rule)
}
2020-05-10 17:49:23 +08:00
// No sequence rules: map[field]rule
2019-06-19 09:06:52 +08:00
case map[string]string:
checkRules = v
}
2020-12-09 21:00:30 +08:00
// If there's no validation rules, it does nothing and returns quickly.
if len(checkRules) == 0 {
return nil
}
data := gconv.Map(params)
if data == nil {
return newErrorStr(
2021-05-19 13:29:40 +08:00
internalParamsErrRuleName,
2020-12-09 21:00:30 +08:00
"invalid params type: convert to map failed",
)
}
if msg, ok := v.messages.(CustomMsg); ok && len(msg) > 0 {
2019-06-19 09:06:52 +08:00
if len(customMsgs) > 0 {
for k, v := range msg {
2019-06-19 09:06:52 +08:00
customMsgs[k] = v
}
} else {
customMsgs = msg
2019-06-19 09:06:52 +08:00
}
}
2019-06-21 22:23:07 +08:00
var value interface{}
2019-06-19 09:06:52 +08:00
for key, rule := range checkRules {
if len(rule) == 0 {
continue
}
value = nil
if v, ok := data[key]; ok {
value = v
}
// It checks each rule and its value in loop.
if e := v.doCheckValue(key, value, rule, customMsgs[key], params, data); e != nil {
2019-06-19 09:06:52 +08:00
_, item := e.FirstItem()
2020-08-20 23:25:36 +08:00
// ===========================================================
// Only in map and struct validations, if value is nil or empty
// string and has no required* rules, it clears the error message.
2020-08-20 23:25:36 +08:00
// ===========================================================
if gconv.String(value) == "" {
2019-06-19 09:06:52 +08:00
required := false
// rule => error
2019-06-21 22:23:07 +08:00
for k := range item {
// Default required rules.
2019-06-19 09:06:52 +08:00
if _, ok := mustCheckRulesEvenValueEmpty[k]; ok {
required = true
break
}
// Custom rules are also required in default.
if f := v.getRuleFunc(k); f != nil {
required = true
break
}
2019-06-19 09:06:52 +08:00
}
if !required {
continue
}
}
if _, ok := errorMaps[key]; !ok {
errorMaps[key] = make(map[string]string)
}
for k, v := range item {
errorMaps[key][k] = v
}
}
}
if len(errorMaps) > 0 {
return newError(errorRules, errorMaps)
}
return nil
}