title: Golang Validator with custom structs
body: <p>I am trying to use the Golang Validator (<a href="https://godoc.org/gopkg.in/go-playground/validator.v9" rel="nofollow noreferrer">https://godoc.org/gopkg.in/go-playground/validator.v9</a>) to validate a Request body. I have two entities, Rule and Item. The Item entity relies on the Rule entity.</p>
  <pre><code>type Rule struct {
      RuleNo      int64     `json:&quot;ruleNo&quot; db:&quot;rule_no&quot;`
      Category    string    `json:&quot;category&quot; db:&quot;category&quot; validate:&quot;alphanum&quot;`
      CreatedAt   time.Time `json:&quot;createdAt&quot; db:&quot;created_at&quot;`
      UpdatedAt   time.Time `json:&quot;updatedAt&quot; db:&quot;updated_at&quot;`
  }
  
  type Item struct {
      SeqNo       int64     `json:&quot;-&quot; db:&quot;item_restriction_no&quot;`
      ItemId      string    `json:&quot;itemId&quot; db:&quot;item_id&quot; validate:&quot;alphanum&quot;`
      ItemType    string    `json:&quot;itemType&quot; db:&quot;item_type&quot; validate:&quot;alphanum&quot;`
      Rules       []Rule    `json:&quot;rules&quot; db:&quot;rules&quot;` // how to validate this field?
      CreatedAt   time.Time `json:&quot;createdAt&quot; db:&quot;created_at&quot;`
      UpdatedAt   time.Time `json:&quot;updatedAt&quot; db:&quot;updated_at&quot;`
  }
  </code></pre>
  <p>How can I validate that a Request body has a list of Rules for the &quot;Rules&quot; field for the Item struct? This is my validate function:</p>
  <pre><code>func (item *Item) Validate() error {
      v := validator.New()
      if err := v.Struct(item); err != nil {
          return err
      }
      return nil
  }
  </code></pre>