gf/util/gvalid/gvalid_validator_check_struct.go

235 lines
7.0 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/internal/structs"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
2021-01-12 10:46:39 +08:00
"strings"
)
// CheckStruct validates struct and returns the error result.
// The parameter `object` should be type of struct/*struct.
func (v *Validator) CheckStruct(object interface{}) Error {
return v.doCheckStruct(object)
}
func (v *Validator) doCheckStruct(object interface{}) Error {
var (
2021-05-17 19:59:34 +08:00
// Returning error.
errorMaps = make(map[string]map[string]string)
)
fieldMap, err := structs.FieldMap(object, aliasNameTagPriority, true)
if err != nil {
2021-05-19 13:29:40 +08:00
return newErrorStr(internalObjectErrRuleName, err.Error())
}
2021-05-17 19:59:34 +08:00
// It checks the struct recursively the its attribute is an embedded struct.
2021-05-11 19:20:39 +08:00
for _, field := range fieldMap {
2021-05-17 19:59:34 +08:00
if field.IsEmbedded() {
// No validation interface implements check.
if _, ok := field.Value.Interface().(apiNoValidation); ok {
continue
}
if _, ok := field.TagLookup(noValidationTagName); ok {
continue
}
if err := v.doCheckStruct(field.Value); err != nil {
// It merges the errors into single error map.
for k, m := range err.(*validationError).errors {
errorMaps[k] = m
}
}
}
}
2021-02-08 17:57:21 +08:00
// It here must use structs.TagFields not structs.FieldMap to ensure error sequence.
tagField, err := structs.TagFields(object, structTagPriority)
2020-12-09 21:00:30 +08:00
if err != nil {
2021-05-19 13:29:40 +08:00
return newErrorStr(internalObjectErrRuleName, err.Error())
2020-12-09 21:00:30 +08:00
}
// If there's no struct tag and validation rules, it does nothing and returns quickly.
if len(tagField) == 0 && v.messages == nil {
2020-12-09 21:00:30 +08:00
return nil
}
2021-05-11 19:20:39 +08:00
2020-04-20 22:36:28 +08:00
var (
2021-05-11 19:20:39 +08:00
inputParamMap map[string]interface{}
2020-04-20 22:36:28 +08:00
checkRules = make(map[string]string)
customMessage = make(CustomMsg)
fieldAliases = make(map[string]string) // Alias names for `messages` overwriting struct tag names.
2020-04-20 22:36:28 +08:00
errorRules = make([]string, 0) // Sequence rules.
)
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 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)
}
// 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-12-09 21:00:30 +08:00
// If there's no struct tag and validation rules, it does nothing and returns quickly.
if len(tagField) == 0 && len(checkRules) == 0 {
return nil
}
// Input parameter map handling.
if v.data == nil || !v.useDataInsteadOfObjectAttributes {
inputParamMap = make(map[string]interface{})
} else {
inputParamMap = gconv.Map(v.data)
}
// Checks and extends the parameters map with struct alias tag.
if !v.useDataInsteadOfObjectAttributes {
for nameOrTag, field := range fieldMap {
inputParamMap[nameOrTag] = field.Value.Interface()
if nameOrTag != field.Name() {
2021-05-11 19:20:39 +08:00
inputParamMap[field.Name()] = field.Value.Interface()
}
}
}
// Merge the custom validation rules with rules in struct tag.
// The custom rules has the most high priority that can overwrite the struct tag rules.
for _, field := range tagField {
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.
name, rule, msg := parseSequenceTag(field.TagValue)
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.
2021-05-11 19:20:39 +08:00
if _, ok := inputParamMap[name]; !ok {
if !v.useDataInsteadOfObjectAttributes {
inputParamMap[name] = field.Value.Interface()
}
}
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.
checkRules[name] = checkRules[fieldName]
delete(checkRules, fieldName)
} else {
checkRules[name] = rule
2019-07-13 11:47:20 +08:00
}
errorRules = append(errorRules, name+"@"+rule)
} else {
// The input rules can overwrite the rules in struct tag.
continue
}
if len(msg) > 0 {
2020-05-10 17:49:23 +08:00
var (
msgArray = strings.Split(msg, "|")
ruleArray = strings.Split(rule, "|")
)
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.
if len(msgArray) <= k {
continue
2019-06-19 09:06:52 +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)
}
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
}
}
}
2020-05-10 17:49:23 +08:00
// Custom error messages,
// which have the most priority than `rules` and struct tag.
if msg, ok := v.messages.(CustomMsg); ok && len(msg) > 0 {
for k, v := range msg {
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
} else {
2020-04-20 22:36:28 +08:00
customMessage[k] = v
2019-06-19 09:06:52 +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 = gutil.MapPossibleItemByKey(inputParamMap, key)
// It checks each rule and its value in loop.
if e := v.doCheckValue(key, value, rule, customMessage[key], inputParamMap); e != nil {
2019-06-19 09:06:52 +08:00
_, item := e.FirstItem()
// ===================================================================
// Only in map and struct validations, if value is nil or empty string
// and has no required* rules, it clears 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 {
// 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 _, ok := customRuleFuncMap[k]; ok {
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
}