2021-01-12 10:46:39 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). 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-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/internal/structs"
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
2021-05-12 00:01:52 +08:00
|
|
|
"github.com/gogf/gf/util/gutil"
|
2021-03-10 23:28:34 +08:00
|
|
|
"reflect"
|
2021-01-12 10:46:39 +08:00
|
|
|
"strings"
|
2019-07-04 11:11:41 +08:00
|
|
|
)
|
|
|
|
|
2021-05-11 20:57:30 +08:00
|
|
|
// doCheckStructWithParamMapInput is used for struct validation for internal function.
|
|
|
|
type doCheckStructWithParamMapInput struct {
|
2021-05-12 00:01:52 +08:00
|
|
|
Object interface{} // Can be type of struct/*struct.
|
|
|
|
ParamMap interface{} // Validation parameter map. Note that it acts different according attribute `UseParamMapInsteadOfObjectValue`.
|
|
|
|
UseParamMapInsteadOfObjectValue bool // Using `ParamMap` as its validation source instead of values from `Object`.
|
|
|
|
CustomRules interface{} // Custom validation rules.
|
|
|
|
CustomErrorMessageMap CustomMsg // Custom error message map for validation rules.
|
2021-05-11 20:57:30 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-03-10 23:28:34 +08:00
|
|
|
// CheckStruct validates struct and returns the error result.
|
2020-05-10 17:49:23 +08:00
|
|
|
//
|
2021-03-23 17:53:20 +08:00
|
|
|
// 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.
|
2021-05-11 20:57:30 +08:00
|
|
|
func (v *Validator) CheckStruct(object interface{}, customRules interface{}, customErrorMessageMap ...CustomMsg) *Error {
|
|
|
|
var message CustomMsg
|
|
|
|
if len(customErrorMessageMap) > 0 {
|
|
|
|
message = customErrorMessageMap[0]
|
|
|
|
}
|
|
|
|
return v.doCheckStructWithParamMap(&doCheckStructWithParamMapInput{
|
2021-05-12 00:01:52 +08:00
|
|
|
Object: object,
|
|
|
|
ParamMap: nil,
|
|
|
|
UseParamMapInsteadOfObjectValue: false,
|
|
|
|
CustomRules: customRules,
|
|
|
|
CustomErrorMessageMap: message,
|
2021-05-11 20:57:30 +08:00
|
|
|
})
|
2021-05-11 19:20:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckStructWithParamMap validates struct with given parameter map 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.
|
2021-05-11 20:57:30 +08:00
|
|
|
func (v *Validator) CheckStructWithParamMap(object interface{}, paramMap interface{}, customRules interface{}, customErrorMessageMap ...CustomMsg) *Error {
|
|
|
|
var message CustomMsg
|
|
|
|
if len(customErrorMessageMap) > 0 {
|
|
|
|
message = customErrorMessageMap[0]
|
|
|
|
}
|
|
|
|
return v.doCheckStructWithParamMap(&doCheckStructWithParamMapInput{
|
2021-05-12 00:01:52 +08:00
|
|
|
Object: object,
|
|
|
|
ParamMap: paramMap,
|
|
|
|
UseParamMapInsteadOfObjectValue: true,
|
|
|
|
CustomRules: customRules,
|
|
|
|
CustomErrorMessageMap: message,
|
2021-05-11 20:57:30 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validator) doCheckStructWithParamMap(input *doCheckStructWithParamMapInput) *Error {
|
2021-03-10 23:28:34 +08:00
|
|
|
var (
|
2021-05-11 19:20:39 +08:00
|
|
|
errorMaps = make(ErrorMap) // Returning error.
|
2021-03-10 23:28:34 +08:00
|
|
|
)
|
2021-05-12 00:15:53 +08:00
|
|
|
fieldMap, err := structs.FieldMap(input.Object, aliasNameTagPriority, true)
|
2021-03-10 23:28:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return newErrorStr("invalid_object", err.Error())
|
|
|
|
}
|
|
|
|
// It checks the struct recursively the its attribute is also a struct.
|
2021-05-11 19:20:39 +08:00
|
|
|
for _, field := range fieldMap {
|
2021-03-10 23:28:34 +08:00
|
|
|
if field.OriginalKind() == reflect.Struct {
|
2021-05-11 20:57:30 +08:00
|
|
|
if err := v.CheckStruct(field.Value, input.CustomRules, input.CustomErrorMessageMap); err != nil {
|
2021-03-10 23:28:34 +08:00
|
|
|
// It merges the errors into single error map.
|
|
|
|
for k, m := range err.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.
|
2021-05-11 20:57:30 +08:00
|
|
|
tagField, err := structs.TagFields(input.Object, structTagPriority)
|
2020-12-09 21:00:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return newErrorStr("invalid_object", err.Error())
|
|
|
|
}
|
|
|
|
// If there's no struct tag and validation rules, it does nothing and returns quickly.
|
2021-05-11 20:57:30 +08:00
|
|
|
if len(tagField) == 0 && input.CustomRules == 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)
|
2021-03-23 17:53:20 +08:00
|
|
|
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.
|
|
|
|
)
|
2021-05-11 20:57:30 +08:00
|
|
|
switch v := input.CustomRules.(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-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
|
|
|
|
}
|
2021-05-11 20:57:30 +08:00
|
|
|
// Input parameter map handling.
|
2021-05-12 00:01:52 +08:00
|
|
|
if input.ParamMap == nil || !input.UseParamMapInsteadOfObjectValue {
|
2021-05-11 20:57:30 +08:00
|
|
|
inputParamMap = make(map[string]interface{})
|
|
|
|
} else {
|
|
|
|
inputParamMap = gconv.Map(input.ParamMap)
|
|
|
|
}
|
2020-01-01 14:57:57 +08:00
|
|
|
// Checks and extends the parameters map with struct alias tag.
|
2021-05-12 00:01:52 +08:00
|
|
|
if !input.UseParamMapInsteadOfObjectValue {
|
2021-05-11 20:57:30 +08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2020-01-01 14:57:57 +08:00
|
|
|
}
|
2021-05-11 20:57:30 +08:00
|
|
|
|
2020-11-08 14:25:17 +08:00
|
|
|
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.
|
2020-11-08 15:44:04 +08:00
|
|
|
name, rule, msg := parseSequenceTag(field.TagValue)
|
2020-01-04 15:35:21 +08:00
|
|
|
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 {
|
2021-05-12 00:01:52 +08:00
|
|
|
if !input.UseParamMapInsteadOfObjectValue {
|
|
|
|
inputParamMap[name] = field.Value.Interface()
|
|
|
|
}
|
2020-01-04 15:35:21 +08:00
|
|
|
}
|
|
|
|
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 {
|
2021-05-12 00:01:52 +08:00
|
|
|
// The input 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,
|
2021-03-23 17:53:20 +08:00
|
|
|
// which have the most priority than `rules` and struct tag.
|
2021-05-11 20:57:30 +08:00
|
|
|
if len(input.CustomErrorMessageMap) > 0 {
|
|
|
|
for k, v := range input.CustomErrorMessageMap {
|
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 {
|
2021-05-12 00:01:52 +08:00
|
|
|
_, value = gutil.MapPossibleItemByKey(inputParamMap, key)
|
2020-10-21 00:08:36 +08:00
|
|
|
// It checks each rule and its value in loop.
|
2021-05-11 19:20:39 +08:00
|
|
|
if e := v.doCheck(key, value, rule, customMessage[key], inputParamMap); e != nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
_, item := e.FirstItem()
|
2021-03-10 23:28:34 +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.
|
|
|
|
// ===================================================================
|
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 {
|
2020-10-21 00:08:36 +08:00
|
|
|
// Default required rules.
|
2019-06-19 09:06:52 +08:00
|
|
|
if _, ok := mustCheckRulesEvenValueEmpty[k]; ok {
|
|
|
|
required = true
|
|
|
|
break
|
|
|
|
}
|
2020-10-21 00:08:36 +08:00
|
|
|
// 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
|
2018-11-13 00:12:35 +08:00
|
|
|
}
|