2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-11-13 00:12:35 +08:00
|
|
|
//
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-11-13 00:12:35 +08:00
|
|
|
|
|
|
|
package gvalid
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"strings"
|
2019-06-21 22:23:07 +08:00
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/util/gconv"
|
2018-11-13 00:12:35 +08:00
|
|
|
)
|
|
|
|
|
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 <rules> can be type of []string/map[string]string. It supports sequence in error result
|
|
|
|
// if <rules> is type of []string.
|
|
|
|
// The optional parameter <messages> specifies the custom error messages for specified keys and rules.
|
2020-05-10 16:48:00 +08:00
|
|
|
func CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) *Error {
|
2019-06-19 09:06:52 +08:00
|
|
|
data := gconv.Map(params)
|
|
|
|
if data == nil {
|
2020-03-17 22:23:29 +08:00
|
|
|
return newErrorStr(
|
|
|
|
"invalid_params",
|
2020-05-10 16:48:00 +08:00
|
|
|
"invalid params type: convert to map failed",
|
2020-03-17 22:23:29 +08:00
|
|
|
)
|
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(ErrorMap)
|
|
|
|
)
|
2019-06-19 09:06:52 +08:00
|
|
|
switch 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)
|
|
|
|
}
|
2018-11-13 00:12:35 +08:00
|
|
|
|
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-05-10 16:48:00 +08:00
|
|
|
if len(messages) > 0 && len(messages[0]) > 0 {
|
2019-06-19 09:06:52 +08:00
|
|
|
if len(customMsgs) > 0 {
|
2020-05-10 16:48:00 +08:00
|
|
|
for k, v := range messages[0] {
|
2019-06-19 09:06:52 +08:00
|
|
|
customMsgs[k] = v
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-10 16:48:00 +08:00
|
|
|
customMsgs = messages[0]
|
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
|
|
|
|
}
|
2020-06-14 17:28:48 +08:00
|
|
|
if e := doCheck(key, value, rule, customMsgs[key], data); e != nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
_, item := e.FirstItem()
|
2020-08-20 23:25:36 +08:00
|
|
|
// ===========================================================
|
2020-05-10 17:49:23 +08:00
|
|
|
// If value is nil or empty string and has no required* rules,
|
|
|
|
// clear the error message.
|
2020-08-20 23:25:36 +08:00
|
|
|
// ===========================================================
|
2020-06-14 17:28:48 +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 {
|
2019-06-19 09:06:52 +08:00
|
|
|
if _, ok := mustCheckRulesEvenValueEmpty[k]; ok {
|
|
|
|
required = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
2018-11-13 00:12:35 +08:00
|
|
|
}
|