3
AgreementNo int`json:"agreement_no" validate:"required"`

I want get zero value from agreement_no request. still required but get zero value so json request null not allow. but validate in echo not allowed zero value.

How can I get this value without remove required validation or there are any solution?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

2 Answers2

5
AgreementNo int`json:"agreement_no" validate:"required"`

Validator cannot find the difference between default zero-value and 0 parsed from json, so it interpret all zero-values as empty. The same happens for boolean, strings, etc.

The workaround is to use pointer instead of plain type.

AgreementNo *int`json:"agreement_no" validate:"required"`
NobbyNobbs
  • 1,341
  • 1
  • 11
  • 17
0
AgreementNo int`json:"agreement_no" default:"0"`

Replace the validate:"required" with default:"0"