LoginSignup
0
0

More than 1 year has passed since last update.

Goのvalidatorパッケージで、バリデーションをしつつ空白を許容する場合

Last updated at Posted at 2021-05-21

1~2ヶ月くらい前からGoを書き始めてるのですが、
下記でハマったというか、 カスタムバリデーションを作ろうとしてしまったので、備忘録的に記載。
Goのvalidatorで、空白を許容しつつバリデーションをしたい場合、omitemptyタグを利用すれば良い。

type testForm struct {
    code           string validate:"omitempty,len=6"`
}

ハマった理由

requiredのバリデーションが存在するのに、なぜ他のバリデーションに関して空白を許容しないのかと混乱しつつ、omitemptyの存在に気がつけなかった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0