Skip to content

Commit

Permalink
既存アノテーションを組み合わせたバリデーションアノテーションの作成例を例示
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshi-koyama committed Sep 23, 2023
1 parent 61d1eea commit 1c43124
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,22 @@ isValidメソッドの第2引数には、バリデーションのコンテキス
また、@AssertTrueアノテーションを使ったチェックの事例を、バリデーションアノテーションをカスタマイズして実装することもできます。
### 既存のアノテーションを組み合わせたアノテーションの作成
独自のアノテーションを作るだけでなくJakarta Bean Validationで定義されているアノテーションを組み合わせる方法もあります。
たとえば、Visaの申し込みを想定するときに、申込可能な年齢が必須であり18歳以上45歳未満であることをチェックする場合、@Minと@Maxを組み合わせて実装することができます。
```java
@NotNull
@Min(18)
@Max(45)
private Integer age;
```
このようにすればたしかに実装はできますが、年齢のチェックを行うアノテーションを作成したほうが、コードの可読性が高くなります。
また、年齢に関するチェック処理を1つにまとめることで、年齢に関するチェック処理の変更が発生した場合に、変更箇所が1箇所になるため、変更の影響範囲を把握しやすくなります。
## まとめ
Spring Bootでは、Jakarta Bean Validationの実装としてHibernate Validatorが利用されています。
Expand Down

0 comments on commit 1c43124

Please sign in to comment.