Skip to content

Question: Using a variable value in validation tag #724

Open
@salihzain

Description

@salihzain

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

Question: using variables in the validation tag
Suppose I have the following struct:

type Payload struct {
      Quantity int
      Items []Item
}

How do I proceed to do the following validation: len(Items) = Quantity
I tried the following:

      Items []Item `validate:"len=eqfield=Quantity"

and also tried this

    Item []Item `validate:"len=Quantity"`

And have been looking at the Validate.Var and Validate.VarWithValue functions but wondering if there's any neater approach, especially that I'm using translations and don't want to repeat my code.

Thank you so much in advance, Your work is greatly appreciated.

Code sample, to showcase or reproduce:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions