public interface ConstraintValidatorContextConstraintViolation を定義する必要があります(デフォルトのもの、デフォルトの ConstraintViolation が無効になっている場合はカスタムのもの)。| 修飾子と型 | インターフェースと説明 |
|---|---|
static interface | ConstraintValidatorContext.ConstraintViolationBuilder オプションで違反レポートをサブパスに関連付けることができる ConstraintViolation ビルダー。 |
| 修飾子と型 | メソッドと説明 |
|---|---|
ConstraintValidatorContext.ConstraintViolationBuilder | buildConstraintViolationWithTemplate(StringSE messageTemplate) オプションでサブパスに関連付けることができる違反レポートを作成する制約違反ビルダーを返します。 |
void | disableDefaultConstraintViolation() デフォルトの ConstraintViolation オブジェクト生成を無効にします(制約で宣言されたメッセージテンプレートを使用しています)。 |
ClockProvider | getClockProvider() 現在の時刻を取得するためのプロバイダーを ClockSE の形式で返します。 |
StringSE | getDefaultConstraintMessageTemplate() |
<T> T | unwrap(ClassSE<T> type) プロバイダー固有の API へのアクセスを許可する、指定された型のインスタンスを返します。 |
void disableDefaultConstraintViolation()
ConstraintViolation オブジェクト生成を無効にします(制約で宣言されたメッセージテンプレートを使用しています)。 別の違反メッセージを設定するか、別のプロパティに基づいて ConstraintViolation を生成できます。
StringSE getDefaultConstraintMessageTemplate()
ClockProvider getClockProvider()
null ではありません。ブートストラップ中に特定のプロバイダーが構成されていない場合、Clock.systemDefaultZone()SE によって返される現在のシステム時刻と現在のデフォルトのタイムゾーンを使用したデフォルトの実装が返されます。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 - 補間されていない新しい制約メッセージ <T> T unwrap(ClassSE<T> type)
ValidationException がスローされます。T - 返されるオブジェクトの型 type - 返されるオブジェクトのクラス ValidationException - プロバイダーが呼び出しをサポートしていない場合 Copyright © 2019 Eclipse Foundation.
Use is subject to license terms.