Skip to content

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

@yz89122

Description

@yz89122
  • 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 "".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions