Skip to content

Custom rule function FieldLevel.FieldName() return empty string on Validate.ValidateMap() #805

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
2 tasks done
yz89122 opened this issue Jul 28, 2021 · 2 comments
Open
2 tasks done

Comments

@yz89122
Copy link

yz89122 commented Jul 28, 2021

  • I have looked at the documentation here first?
  • I have looked at the examples provided that may showcase my question here?

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

Custom rule FieldLevel.FieldName() return empty string on Validate.ValidateMap()

Code sample, to showcase or reproduce:

main.go

package main

import (
	"fmt"
	"reflect"

	"github.com/go-playground/validator/v10"
)

func main() {
	validate := validator.New()
	validate.RegisterTagNameFunc(func(field reflect.StructField) string {
		fmt.Println("TagNameFunc")
		return "test"
	})
	validate.RegisterValidation("presented", func(fl validator.FieldLevel) bool {
		fmt.Println("presented")
		fmt.Println("fl.FieldName()", fl.FieldName())
		return false
	}, true)
	data := map[string]interface{}{
		"field": "nil",
	}
	rule := map[string]interface{}{
		"field": "presented",
	}
	result := validate.ValidateMap(data, rule)
	fmt.Println("result", result)
	err := result["field"]
	fmt.Println(reflect.TypeOf(err))
	fmt.Println(err)
}

output:

presented
fl.FieldName() 
result map[field:Key: '' Error:Field validation for '' failed on the 'presented' tag]
validator.ValidationErrors
Key: '' Error:Field validation for '' failed on the 'presented' tag

Expected

FieldLevel.FieldName() will return string "field" instead of empty string "".

@kokoraka
Copy link

kokoraka commented Oct 5, 2021

It's also happened to me.

In my case it's happened when I'm trying to validate map using ValidateMap and then trying to cast the result into validation.ValidationErrors.

The error.Field() and error.StructField() always returning empty string.
Probably this is a bug when validating a map, the key (field name) is not returned back.

@timo-klarshift
Copy link

timo-klarshift commented Jan 26, 2022

I am also having this issue: I use ValidateMap but cannot get the field name

I am using this workaround:

// Get all validation results
	validationResults := V.ValidateMap(*in, params.validationRules)

	// Check if there are validation errors
	allErrors := make([]error, 0)

	if len(validationResults) > 0 {
		for property, v := range validationResults {

			if validationErr, ok := v.(validator.ValidationErrors); ok {
				for _, fieldErr := range validationErr {

					var requirement = fieldErr.Tag()
					if fieldErr.Param() != "" {
						requirement = fieldErr.Tag() + "(" + fieldErr.Param() + ")"
					}

					allErrors = append(
						allErrors,
						fmt.Errorf(
							"field [%s] requires %s but is %+v",
							property,
							requirement,
							fieldErr.Value(),
						),
					)
				}
			}
		}
	}

	if len(allErrors) > 0 {
		return &ValidationError{
			Message: fmt.Sprintf("Validation failed (%d error(s))", len(allErrors)),
			Errors:  allErrors,
		}
	}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants