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-21 22:23:07 +08:00
|
|
|
"strings"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/internal/structs"
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
2019-07-04 11:11:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-01-01 14:57:57 +08:00
|
|
|
structTagPriority = []string{"gvalid", "valid", "v"} // structTagPriority specifies the validation tag priority array.
|
|
|
|
aliasNameTagPriority = []string{"param", "params", "p"} // aliasNameTagPriority specifies the alias tag priority array.
|
2018-11-13 00:12:35 +08:00
|
|
|
)
|
|
|
|
|
2020-05-10 17:49:23 +08:00
|
|
|
// CheckStruct validates strcut and returns the error result.
|
|
|
|
//
|
|
|
|
// The parameter <object> should be type of struct/*struct.
|
|
|
|
// 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-04-20 22:36:28 +08:00
|
|
|
func CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) *Error {
|
|
|
|
var (
|
|
|
|
params = make(map[string]interface{})
|
|
|
|
checkRules = make(map[string]string)
|
|
|
|
customMessage = make(CustomMsg)
|
|
|
|
fieldAliases = make(map[string]string) // Alias names for <messages> overwriting struct tag names.
|
|
|
|
errorRules = make([]string, 0) // Sequence rules.
|
|
|
|
errorMaps = make(ErrorMap) // Returned error
|
|
|
|
)
|
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 22:32:10 +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, ":")
|
2020-04-20 22:36:28 +08:00
|
|
|
if _, ok := customMessage[name]; !ok {
|
|
|
|
customMessage[name] = make(map[string]string)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2020-04-20 22:36:28 +08:00
|
|
|
customMessage[name].(map[string]string)[strings.TrimSpace(array[0])] = strings.TrimSpace(msgArray[k])
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
checkRules[name] = rule
|
|
|
|
errorRules = append(errorRules, name+"@"+rule)
|
|
|
|
}
|
2018-11-13 00:12:35 +08:00
|
|
|
|
2020-01-01 14:57:57 +08:00
|
|
|
// Map type rules does not support sequence.
|
|
|
|
// Format: map[key]rule
|
2019-06-19 09:06:52 +08:00
|
|
|
case map[string]string:
|
|
|
|
checkRules = v
|
|
|
|
}
|
2020-01-01 14:57:57 +08:00
|
|
|
// Checks and extends the parameters map with struct alias tag.
|
|
|
|
for nameOrTag, field := range structs.MapField(object, aliasNameTagPriority, true) {
|
|
|
|
params[nameOrTag] = field.Value()
|
|
|
|
params[field.Name()] = field.Value()
|
|
|
|
}
|
2020-01-04 15:35:21 +08:00
|
|
|
// It here must use structs.TagFields not structs.MapField to ensure error sequence.
|
|
|
|
for _, field := range structs.TagFields(object, structTagPriority, true) {
|
2019-06-19 09:06:52 +08:00
|
|
|
fieldName := field.Name()
|
2020-05-10 17:49:23 +08:00
|
|
|
// sequence tag == struct tag
|
|
|
|
// The name here is alias of field name.
|
2020-01-04 15:35:21 +08:00
|
|
|
name, rule, msg := parseSequenceTag(field.Tag)
|
|
|
|
if len(name) == 0 {
|
|
|
|
name = fieldName
|
|
|
|
} else {
|
|
|
|
fieldAliases[fieldName] = name
|
|
|
|
}
|
2020-05-10 17:49:23 +08:00
|
|
|
// It here extends the params map using alias names.
|
2020-01-04 15:35:21 +08:00
|
|
|
if _, ok := params[name]; !ok {
|
|
|
|
params[name] = field.Value()
|
|
|
|
}
|
|
|
|
if _, ok := checkRules[name]; !ok {
|
|
|
|
if _, ok := checkRules[fieldName]; ok {
|
2020-05-10 17:49:23 +08:00
|
|
|
// If there's alias name,
|
|
|
|
// use alias name as its key and remove the field name key.
|
2020-01-04 15:35:21 +08:00
|
|
|
checkRules[name] = checkRules[fieldName]
|
|
|
|
delete(checkRules, fieldName)
|
2019-07-11 15:41:10 +08:00
|
|
|
} else {
|
2020-01-04 15:35:21 +08:00
|
|
|
checkRules[name] = rule
|
2019-07-13 11:47:20 +08:00
|
|
|
}
|
2020-01-04 15:35:21 +08:00
|
|
|
errorRules = append(errorRules, name+"@"+rule)
|
|
|
|
} else {
|
2020-05-10 17:49:23 +08:00
|
|
|
// The passed rules can overwrite the rules in struct tag.
|
2020-01-04 15:35:21 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(msg) > 0 {
|
2020-05-10 17:49:23 +08:00
|
|
|
var (
|
|
|
|
msgArray = strings.Split(msg, "|")
|
|
|
|
ruleArray = strings.Split(rule, "|")
|
|
|
|
)
|
2020-01-04 15:35:21 +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.
|
2020-01-04 15:35:21 +08:00
|
|
|
if len(msgArray) <= k {
|
|
|
|
continue
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2020-01-04 15:35:21 +08:00
|
|
|
if len(msgArray[k]) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
array := strings.Split(v, ":")
|
2020-04-20 22:36:28 +08:00
|
|
|
if _, ok := customMessage[name]; !ok {
|
|
|
|
customMessage[name] = make(map[string]string)
|
2019-07-13 10:47:52 +08:00
|
|
|
}
|
2020-04-20 22:36:28 +08:00
|
|
|
customMessage[name].(map[string]string)[strings.TrimSpace(array[0])] = strings.TrimSpace(msgArray[k])
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 15:41:10 +08:00
|
|
|
|
2020-05-10 17:49:23 +08:00
|
|
|
// Custom error messages,
|
|
|
|
// which have the most priority than <rules> and struct tag.
|
2020-04-20 22:36:28 +08:00
|
|
|
if len(messages) > 0 && len(messages[0]) > 0 {
|
|
|
|
for k, v := range messages[0] {
|
2019-07-17 20:07:43 +08:00
|
|
|
if a, ok := fieldAliases[k]; ok {
|
2020-05-10 17:49:23 +08:00
|
|
|
// Overwrite the key of field name.
|
2020-04-20 22:36:28 +08:00
|
|
|
customMessage[a] = v
|
2019-07-17 20:07:43 +08:00
|
|
|
} else {
|
2020-04-20 22:36:28 +08:00
|
|
|
customMessage[k] = v
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-13 00:12:35 +08:00
|
|
|
|
2020-05-10 17:49:23 +08:00
|
|
|
// The following logic is the same as some of CheckMap.
|
2019-06-21 22:23:07 +08:00
|
|
|
var value interface{}
|
2019-06-19 09:06:52 +08:00
|
|
|
for key, rule := range checkRules {
|
|
|
|
value = nil
|
|
|
|
if v, ok := params[key]; ok {
|
|
|
|
value = v
|
|
|
|
}
|
2020-06-14 17:28:48 +08:00
|
|
|
if e := doCheck(key, value, rule, customMessage[key], params); e != nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
_, item := e.FirstItem()
|
2020-05-10 17:49:23 +08:00
|
|
|
// If value is nil or empty string and has no required* rules,
|
|
|
|
// clear the error message.
|
2019-06-19 09:06:52 +08:00
|
|
|
if value == nil || gconv.String(value) == "" {
|
|
|
|
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
|
|
|
}
|