パッケージ jakarta.validation
インターフェース ConstraintValidatorContext
public interface ConstraintValidatorContext特定の制約バリデーターを適用するときに、コンテキストデータと操作を提供します。少なくとも 1 つのConstraintViolationを定義する必要があります(デフォルトのもの、デフォルトのConstraintViolationが無効になっている場合はカスタムのもの)。- 作成者:
- Emmanuel Bernard, Guillaume Smet
ネストされたクラスの要約
ネストされたクラス 修飾子と型 インターフェース 説明 static interfaceConstraintValidatorContext.ConstraintViolationBuilderオプションで違反レポートをサブパスに関連付けることができるConstraintViolationビルダー。
メソッドのサマリー
すべてのメソッド インスタンスメソッド 抽象メソッド 修飾子と型 メソッド 説明 ConstraintValidatorContext.ConstraintViolationBuilderbuildConstraintViolationWithTemplate(StringSE messageTemplate)オプションでサブパスに関連付けることができる違反レポートを作成する制約違反ビルダーを返します。voiddisableDefaultConstraintViolation()デフォルトのConstraintViolationオブジェクト生成を無効にします(制約で宣言されたメッセージテンプレートを使用しています)。ClockProvidergetClockProvider()現在の時刻を取得するためのプロバイダーをClockSE の形式で返します。StringSEgetDefaultConstraintMessageTemplate()<T> Tunwrap(ClassSE<T> type)プロバイダー固有の API へのアクセスを許可する、指定された型のインスタンスを返します。
メソッドの詳細
disableDefaultConstraintViolation
void disableDefaultConstraintViolation()
デフォルトのConstraintViolationオブジェクト生成を無効にします(制約で宣言されたメッセージテンプレートを使用しています)。別の違反メッセージを設定するか、別のプロパティに基づいて
ConstraintViolationを生成できます。
getDefaultConstraintMessageTemplate
StringSE getDefaultConstraintMessageTemplate()
- 戻り値:
- 現在の補間されていないデフォルトのメッセージ
getClockProvider
ClockProvider getClockProvider()
- 戻り値:
- 現在時刻を取得するためのプロバイダー。決して
nullではありません。ブートストラップ中に特定のプロバイダーが構成されていない場合、Clock.systemDefaultZone()によって返される現在のシステム時刻と現在のデフォルトのタイムゾーンを使用したデフォルトの実装が返されます。 - 導入:
- 2.0
buildConstraintViolationWithTemplate
ConstraintValidatorContext.ConstraintViolationBuilder buildConstraintViolationWithTemplate(StringSE messageTemplate)
オプションでサブパスに関連付けることができる違反レポートを作成する制約違反ビルダーを返します。違反メッセージは補間されます。ConstraintViolationを作成するには、Fluent API のいずれかのインターフェースで使用可能なaddConstraintViolation()メソッドのいずれかを呼び出す必要があります。ConstraintViolationBuilderのaddConstraintViolation()またはそれに関連付けられたネストされたインターフェースの後に別のメソッドが呼び出されると、IllegalStateExceptionが発生します。ConstraintValidator.isValid(Object, ConstraintValidatorContext)がfalseを返す場合、ConstraintViolationオブジェクトは、デフォルトのレポートを含む制約違反レポートごとに作成されます(disableDefaultConstraintViolation()が呼び出されていない場合)。このような呼び出しから生成された
ConstraintViolationオブジェクトには、パスがオーバーライドされていない限り、同じコンテキスト情報(ルート Bean、パスなど)が含まれています。別の
ConstraintViolationを作成するには、新しい制約違反ビルダーをConstraintValidatorContextから取得する必要があります。使用例をいくつか示します。//assuming the following domain model public class User { public Map<String,Address> getAddresses() { ... } } public class Address { public String getStreet() { ... } public Country getCountry() { ... } } public class Country { public String getName() { ... } } //From a property-level constraint on User.addresses //Build a constraint violation on the default path - i.e. the "addresses" property context.buildConstraintViolationWithTemplate( "this detail is wrong" ) .addConstraintViolation(); //From a class level constraint on Address //Build a constraint violation on the default path + "street" //i.e. the street property of Address context.buildConstraintViolationWithTemplate( "this detail is wrong" ) .addPropertyNode( "street" ) .addConstraintViolation(); //From a property-level constraint on User.addresses //Build a constraint violation on the default path + the bean stored //under the "home" key in the map context.buildConstraintViolationWithTemplate( "Incorrect home address" ) .addBeanNode() .inContainer( Map.class, 1 ) .inIterable().atKey( "home" ) .addConstraintViolation(); //From a class level constraint on User //Build a constraint violation on the default path + addresses["home"].country.name //i.e. property "country.name" on the object stored under "home" in the map context.buildConstraintViolationWithTemplate( "this detail is wrong" ) .addPropertyNode( "addresses" ) .addPropertyNode( "country" ) .inContainer( Map.class, 1 ) .inIterable().atKey( "home" ) .addPropertyNode( "name" ) .addConstraintViolation(); //From a class level constraint on User //Build a constraint violation on the default path + addresses["home"].<map key> //i.e. a container element constraint violation for the map key context.buildConstraintViolationWithTemplate( "the map key is invalid" ) .addPropertyNode( "addresses" ) .addContainerElementNode( "<map key>", Map.class, 0 ) .inIterable().atKey( "invalid" ) .addConstraintViolation();メソッドのクロスパラメーター制約により、必要に応じて特定のパラメーターに固有のノードを作成できます。いくつかの例を見てみましょう:
//Cross-parameter constraint on method //createUser(String password, String passwordRepeat) //Build a constraint violation on the default path + "passwordRepeat" context.buildConstraintViolationWithTemplate("Passwords do not match") .addParameterNode(1) .addConstraintViolation(); //Cross-parameter constraint on a method //mergeAddresses(Map<String,Address> addresses, // Map<String,Address> otherAddresses) //Build a constraint violation on the default path + "otherAddresses["home"] //i.e. the Address bean hosted in the "home" key of the "otherAddresses" map parameter context.buildConstraintViolationWithTemplate( "Map entry home present in both and does not match") .addParameterNode(1) .addBeanNode() .inContainer( Map.class, 1 ) .inIterable().atKey("home") .addConstraintViolation(); //Cross-parameter constraint on a method //mergeAddresses(Map<String,Address> addresses, // Map<String,Address> otherAddresses) //Build a constraint violation on the default path + "otherAddresses["home"].city //i.e. on the "city" property of the Address bean hosted in //the "home" key of the "otherAddresses" map context.buildConstraintViolationWithTemplate( "Map entry home present in both but city does not match") .addParameterNode(1) .addPropertyNode("city") .inContainer( Map.class, 1 ) .inIterable().atKey("home") .addConstraintViolation();- パラメーター:
messageTemplate- 補間されていない新しい制約メッセージ- 戻り値:
- 制約違反ビルダーを返します
unwrap
<T> T unwrap(ClassSE<T> type)
プロバイダー固有の API へのアクセスを許可する、指定された型のインスタンスを返します。Jakarta Bean Validation プロバイダーの実装が指定されたクラスをサポートしない場合、ValidationExceptionがスローされます。- 型パラメーター:
T- 返されるオブジェクトの型- パラメーター:
type- 返されるオブジェクトのクラス- 戻り値:
- 指定されたクラスのインスタンス
- 例外:
ValidationException- プロバイダーが呼び出しをサポートしていない場合- 導入:
- 1.1