Validator: Getting json tag field name

Created on 24 Jun 2017  路  3Comments  路  Source: go-playground/validator

Package version eg. v8, v9:

v9

Issue, Question or Enhancement:

How can I get the json field name when validation fails?

Code sample, to showcase or reproduce:

In the example below, I want to be able to get the signup_email json name, not Email in the validation error

package main

import (
    "fmt"

    "gopkg.in/go-playground/validator.v9"
)

// User contains user information
type User struct {
    Email          string     `json:"signup_email" validate:"required,email"`
}

// use a single instance of Validate, it caches struct info
var validate *validator.Validate

func main() {

    validate = validator.New()

    user := &User{
        Email:          "Badger.Smith",
    }

    // returns InvalidValidationError for bad validation input, nil or ValidationErrors ( []FieldError )
    err := validate.Struct(user)
    if err != nil {

        // this check is only needed when your code could produce
        // an invalid value for validation such as interface with nil
        // value most including myself do not usually have code like this.
        if _, ok := err.(*validator.InvalidValidationError); ok {
            fmt.Println(err)
            return
        }

        for _, err := range err.(validator.ValidationErrors) {

            fmt.Println(err.Namespace())
            fmt.Println(err.Field())
            fmt.Println(err.StructNamespace()) // can differ when a custom TagNameFunc is registered or
            fmt.Println(err.StructField())     // by passing alt name to ReportError like below
            fmt.Println(err.Tag())
            fmt.Println(err.ActualTag())
            fmt.Println(err.Kind())
            fmt.Println(err.Type())
            fmt.Println(err.Value())
            fmt.Println(err.Param())
            fmt.Println()
        }

        // from here you can create your own error messages in whatever language you wish
        return
    }

    // save user to database
}

and here's the output:

User.Email
Email
User.Email
Email
email
email
string
string
Badger.Smith```

```go

Most helpful comment

Hey @0505gonzalez ya I really have to create an example for that, You just register a tag name function like here https://github.com/go-playground/validator/blob/v9/validator_test.go#L366

That example is for json but could use any other tag too.

All 3 comments

Hey @0505gonzalez ya I really have to create an example for that, You just register a tag name function like here https://github.com/go-playground/validator/blob/v9/validator_test.go#L366

That example is for json but could use any other tag too.

Got it, worked. Thanks.

Thanks @deankarn, that worked. That link no longer works though as it's pointing to the current version of the file. Here is the historic one: https://github.com/go-playground/validator/blob/fb68f39656d7ebf8aa339ad4917aa0c260ecc237/validator_test.go#L366

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexyans picture alexyans  路  3Comments

thaonx picture thaonx  路  3Comments

Jake-Convictional picture Jake-Convictional  路  4Comments

scisci picture scisci  路  5Comments

phamduong picture phamduong  路  6Comments