title: How to return custom error if validation failed with Gin
body: <p>For example I've got the following <code>struct</code></p>
  <pre><code>type Address struct {
      City string `json:&quot;city&quot; binding:&quot;required&quot;`
      AddressLine string `json:&quot;address_line&quot; binding:&quot;required&quot;`
  }
  </code></pre>
  <p>and I've got the following function to handle request from users</p>
  <pre><code>func AddressCreate(c *gin.Context) {
      var address Address
      if err := c.BindJSON(&amp;address); err == nil {
          // if everything is good save to database
          // and return success message
          db.Create(&amp;address)
          c.JSON(http.StatusOK, gin.H {&quot;status&quot;:&quot;success&quot;})
      } else {
          c.JSON(http.StatusBadRequest, err)
      }
  }
  </code></pre>
  <p>Expected behavior is to return JSON, formatted this way</p>
  <pre><code>[
       {
           &quot;city&quot;:&quot;required&quot;
       }
       {
           &quot;address_line&quot;:&quot;required&quot;
       }
  ]
  </code></pre>
  <p>But I'm getting an error formatted like this</p>
  <pre><code>&quot;Address.City&quot;: {
      &quot;FieldNamespace&quot;: &quot;Address.City&quot;,
      &quot;NameNamespace&quot;: &quot;City&quot;,
      &quot;Field&quot;: &quot;City&quot;,
      &quot;Name&quot;: &quot;City&quot;,
      &quot;Tag&quot;: &quot;required&quot;,
      &quot;ActualTag&quot;: &quot;required&quot;,
      &quot;Kind&quot;: 24,
      &quot;Type&quot;: {},
      &quot;Param&quot;: &quot;&quot;,
      &quot;Value&quot;: &quot;&quot;
  },
  &quot;Address.AddressLine&quot;: {
      &quot;FieldNamespace&quot;: &quot;AddressLine&quot;,
      &quot;NameNamespace&quot;: &quot;AddressLine&quot;,
      &quot;Field&quot;: &quot;AddressLine&quot;,
      &quot;Name&quot;: &quot;AddressLine&quot;,
      &quot;Tag&quot;: &quot;required&quot;,
      &quot;ActualTag&quot;: &quot;required&quot;,
      &quot;Kind&quot;: 24,
      &quot;Type&quot;: {},
      &quot;Param&quot;: &quot;&quot;,
      &quot;Value&quot;: &quot;&quot;
  }
  </code></pre>
  <p>What I tried:<br />
  I created function which casts <code>error</code> to <code>ValidationErrors</code> and iterates through all <code>FieldError</code>'s in it</p>
  <pre><code>func ListOfErrors(e error) []map[string]string {
      ve := e.(validator.ValidationErrors)
      InvalidFields := make([]map[string]string, 0)
  
      for _, e := range ve {
          errors := map[string]string{}
          // field := reflect.TypeOf(e.NameNamespace)
          errors[e.Name] = e.Tag
          InvalidFields = append(InvalidFields, errors)
      }
  
      return InvalidFields
  }
  </code></pre>
  <p>The output look's much better</p>
  <pre><code>[
      {
          &quot;City&quot;:&quot;required&quot;
      },
      {
          &quot;AddressLine&quot;:&quot;required&quot;
      }
  ]
  </code></pre>
  <p>But I cannot solve the problem with the name of the fields. I cannot swap <code>Name</code> into <code>name</code> which I noted in structs tag <code>json:&quot;city&quot;</code>. So my question is did I choose correct way to solve the problem if the answer is yes how to get structs JSON tag for field?</p>