Files
gf/util/gvalid/gvalid_validator_check_struct.go

350 lines
11 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 (
"context"
"reflect"
"strings"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/errors/gcode"
2022-03-11 10:24:42 +08:00
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/os/gstructs"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gmeta"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/util/gutil"
)
func (v *Validator) doCheckStruct(ctx context.Context, object interface{}) Error {
var (
errorMaps = make(map[string]map[string]error) // Returning error.
fieldToAliasNameMap = make(map[string]string) // Field names to alias name map.
2021-11-15 16:50:30 +08:00
resultSequenceRules = make([]fieldRule, 0)
2022-03-11 10:24:42 +08:00
isEmptyData = empty.IsEmpty(v.data)
isEmptyAssoc = empty.IsEmpty(v.assoc)
)
fieldMap, err := gstructs.FieldMap(gstructs.FieldMapInput{
2021-08-03 22:21:20 +08:00
Pointer: object,
PriorityTagArray: aliasNameTagPriority,
RecursiveOption: gstructs.RecursiveOptionEmbedded,
2021-08-03 22:21:20 +08:00
})
if err != nil {
return newValidationErrorByStr(internalObjectErrRuleName, err)
}
// It here must use gstructs.TagFields not gstructs.FieldMap to ensure error sequence.
tagFields, err := gstructs.TagFields(object, structTagPriority)
2020-12-09 21:00:30 +08:00
if err != nil {
return newValidationErrorByStr(internalObjectErrRuleName, err)
2020-12-09 21:00:30 +08:00
}
// If there's no struct tag and validation rules, it does nothing and returns quickly.
2022-03-11 10:24:42 +08:00
if len(tagFields) == 0 && v.messages == nil && isEmptyData && isEmptyAssoc {
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-08-01 23:50:44 +08:00
inputParamMap map[string]interface{}
checkRules = make([]fieldRule, 0)
nameToRuleMap = make(map[string]string) // just for internally searching index purpose.
customMessage = make(CustomMsg) // Custom rule error message map.
2021-11-22 22:43:09 +08:00
checkValueData = v.assoc // Ready to be validated data, which can be type of .
2020-04-20 22:36:28 +08:00
)
if checkValueData == nil {
checkValueData = object
}
2021-08-01 23:50:44 +08:00
switch assertValue := 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:
2021-08-01 23:50:44 +08:00
for _, tag := range assertValue {
2022-03-02 20:00:40 +08:00
name, rule, msg := ParseTagValue(tag)
2019-06-19 09:06:52 +08:00
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, "|")
)
for k, ruleKey := 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(ruleKey, ":")
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
}
}
2021-08-01 23:50:44 +08:00
nameToRuleMap[name] = rule
checkRules = append(checkRules, fieldRule{
Name: name,
Rule: rule,
})
2019-06-19 09:06:52 +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:
2021-08-01 23:50:44 +08:00
nameToRuleMap = assertValue
for name, rule := range assertValue {
checkRules = append(checkRules, fieldRule{
Name: name,
Rule: rule,
})
}
2019-06-19 09:06:52 +08:00
}
2020-12-09 21:00:30 +08:00
// If there's no struct tag and validation rules, it does nothing and returns quickly.
2022-03-11 10:24:42 +08:00
if len(tagFields) == 0 && len(checkRules) == 0 && isEmptyData && isEmptyAssoc {
2020-12-09 21:00:30 +08:00
return nil
}
// Input parameter map handling.
if v.assoc == nil || !v.useAssocInsteadOfObjectAttributes {
inputParamMap = make(map[string]interface{})
} else {
2021-11-22 22:43:09 +08:00
inputParamMap = gconv.Map(v.assoc)
}
// Checks and extends the parameters map with struct alias tag.
if !v.useAssocInsteadOfObjectAttributes {
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()
}
}
}
2021-08-01 23:50:44 +08:00
// 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 tagFields {
var (
isMeta bool
2022-03-02 20:00:40 +08:00
fieldName = field.Name() // Attribute name.
name, rule, msg = ParseTagValue(field.TagValue) // The `name` is different from `attribute alias`, which is used for validation only.
)
if len(name) == 0 {
if value, ok := fieldToAliasNameMap[fieldName]; ok {
// It uses alias name of the attribute if its alias name tag exists.
name = value
} else {
// It or else uses the attribute name directly.
name = fieldName
}
} else {
// It uses the alias name from validation rule.
fieldToAliasNameMap[fieldName] = name
}
2020-05-10 17:49:23 +08:00
// It here extends the params map using alias names.
// Note that the variable `name` might be alias name or attribute name.
2021-05-11 19:20:39 +08:00
if _, ok := inputParamMap[name]; !ok {
if !v.useAssocInsteadOfObjectAttributes {
inputParamMap[name] = field.Value.Interface()
} else {
if name != fieldName {
if foundKey, foundValue := gutil.MapPossibleItemByKey(inputParamMap, fieldName); foundKey != "" {
inputParamMap[name] = foundValue
}
}
}
}
2021-08-01 23:50:44 +08:00
if _, ok := nameToRuleMap[name]; !ok {
if _, ok = nameToRuleMap[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.
2021-08-01 23:50:44 +08:00
nameToRuleMap[name] = nameToRuleMap[fieldName]
delete(nameToRuleMap, fieldName)
for index, checkRuleItem := range checkRules {
if fieldName == checkRuleItem.Name {
checkRuleItem.Name = name
checkRules[index] = checkRuleItem
break
}
}
} else {
2021-08-01 23:50:44 +08:00
nameToRuleMap[name] = rule
if fieldValue := field.Value.Interface(); fieldValue != nil {
_, isMeta = fieldValue.(gmeta.Meta)
}
2021-08-01 23:50:44 +08:00
checkRules = append(checkRules, fieldRule{
Name: name,
Rule: rule,
IsMeta: isMeta,
FieldKind: field.OriginalKind(),
2021-08-01 23:50:44 +08:00
})
2019-07-13 11:47:20 +08:00
}
} else {
// The input rules can overwrite the rules in struct tag.
continue
}
2021-08-01 23:50:44 +08:00
if len(msg) > 0 {
2020-05-10 17:49:23 +08:00
var (
msgArray = strings.Split(msg, "|")
ruleArray = strings.Split(rule, "|")
)
for k, ruleKey := 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(ruleKey, ":")
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, msgName := range msg {
if a, ok := fieldToAliasNameMap[k]; ok {
2020-05-10 17:49:23 +08:00
// Overwrite the key of field name.
customMessage[a] = msgName
} else {
customMessage[k] = msgName
2019-06-19 09:06:52 +08:00
}
}
}
// Temporary variable for value.
2022-03-11 10:24:42 +08:00
var value interface{}
// It checks the struct recursively if its attribute is a struct/struct slice.
for _, field := range fieldMap {
// No validation interface implements check.
if _, ok := field.Value.Interface().(iNoValidation); ok {
continue
}
// No validation field tag check.
if _, ok := field.TagLookup(noValidationTagName); ok {
continue
}
if field.IsEmbedded() {
if err = v.doCheckStruct(ctx, field.Value); err != nil {
// It merges the errors into single error map.
for k, m := range err.(*validationError).errors {
errorMaps[k] = m
}
}
} else {
if field.TagValue != "" {
fieldToAliasNameMap[field.Name()] = field.TagValue
}
switch field.OriginalKind() {
case reflect.Map, reflect.Struct, reflect.Slice, reflect.Array:
2022-05-26 15:28:17 +08:00
// Recursively check attribute slice/map.
_, value = gutil.MapPossibleItemByKey(inputParamMap, field.Name())
v.doCheckValueRecursively(ctx, doCheckValueRecursivelyInput{
Value: value,
2022-05-26 15:28:17 +08:00
Kind: field.OriginalKind(),
Type: field.Type().Type,
ErrorMaps: errorMaps,
ResultSequenceRules: &resultSequenceRules,
})
}
}
if v.bail && len(errorMaps) > 0 {
break
}
}
if v.bail && len(errorMaps) > 0 {
return newValidationError(gcode.CodeValidationFailed, resultSequenceRules, errorMaps)
}
// The following logic is the same as some of CheckMap but with sequence support.
2021-08-01 23:50:44 +08:00
for _, checkRuleItem := range checkRules {
if !checkRuleItem.IsMeta {
_, value = gutil.MapPossibleItemByKey(inputParamMap, checkRuleItem.Name)
if value == nil {
if aliasName := fieldToAliasNameMap[checkRuleItem.Name]; aliasName != "" {
_, value = gutil.MapPossibleItemByKey(inputParamMap, aliasName)
}
}
}
// Empty json string checks according to mapping field kind.
if value != nil {
switch checkRuleItem.FieldKind {
case reflect.Struct, reflect.Map:
if gconv.String(value) == emptyJsonObjectStr {
value = ""
}
case reflect.Slice, reflect.Array:
if gconv.String(value) == emptyJsonArrayStr {
value = ""
}
}
}
// It checks each rule and its value in loop.
if validatedError := v.doCheckValue(ctx, doCheckValueInput{
2021-08-01 23:50:44 +08:00
Name: checkRuleItem.Name,
Value: value,
Rule: checkRuleItem.Rule,
Messages: customMessage[checkRuleItem.Name],
DataRaw: checkValueData,
DataMap: inputParamMap,
}); validatedError != nil {
_, errorItem := validatedError.FirstItem()
// ============================================================
// Only in map and struct validations:
// If value is nil or empty string and has no required* rules,
// it clears the error message.
// ============================================================
if !checkRuleItem.IsMeta && (value == nil || gconv.String(value) == "") {
2019-06-19 09:06:52 +08:00
required := false
// rule => error
2021-08-01 23:50:44 +08:00
for ruleKey := range errorItem {
// Default required rules.
2021-08-01 23:50:44 +08:00
if _, ok := mustCheckRulesEvenValueEmpty[ruleKey]; ok {
2019-06-19 09:06:52 +08:00
required = true
break
}
// All custom validation rules are required rules.
if _, ok := customRuleFuncMap[ruleKey]; ok {
required = true
break
}
2019-06-19 09:06:52 +08:00
}
if !required {
continue
}
}
2021-08-01 23:50:44 +08:00
if _, ok := errorMaps[checkRuleItem.Name]; !ok {
errorMaps[checkRuleItem.Name] = make(map[string]error)
2019-06-19 09:06:52 +08:00
}
2021-08-01 23:50:44 +08:00
for ruleKey, errorItemMsgMap := range errorItem {
errorMaps[checkRuleItem.Name][ruleKey] = errorItemMsgMap
2019-06-19 09:06:52 +08:00
}
// Bail feature.
2021-08-01 23:50:44 +08:00
if v.bail {
2021-08-01 22:12:44 +08:00
break
}
2019-06-19 09:06:52 +08:00
}
}
if len(errorMaps) > 0 {
2021-11-15 16:50:30 +08:00
return newValidationError(
gcode.CodeValidationFailed,
append(checkRules, resultSequenceRules...),
errorMaps,
)
2019-06-19 09:06:52 +08:00
}
return nil
}