パッケージ jakarta.validation

インターフェース ConstraintValidatorContext


public interface ConstraintValidatorContext
特定の制約バリデーターを適用するときに、コンテキストデータと操作を提供します。少なくとも 1 つの ConstraintViolation を定義する必要があります(デフォルトのもの、デフォルトの ConstraintViolation が無効になっている場合はカスタムのもの)。
作成者:
Emmanuel Bernard, Guillaume Smet
  • 方法の詳細

    • disableDefaultConstraintViolation

      void disableDefaultConstraintViolation()
      デフォルトの ConstraintViolation オブジェクト生成を無効にします(制約で宣言されたメッセージテンプレートを使用しています)。

      別の違反メッセージを設定するか、別のプロパティに基づいて ConstraintViolation を生成できます。

    • getDefaultConstraintMessageTemplate

      StringSE getDefaultConstraintMessageTemplate()
      戻り値:
      現在の補間されていないデフォルトのメッセージ
    • getClockProvider

      ClockProvider getClockProvider()
      現在の時刻を取得するためのプロバイダーを ClockSE の形式で返します。Future および Past 制約を検証する場合。
      戻り値:
      現在時刻を取得するためのプロバイダー。決して 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 Validation プロバイダーの実装が指定されたクラスをサポートしない場合、ValidationException がスローされます。
      型パラメーター:
      T - 返されるオブジェクトの型
      パラメーター:
      type - 返されるオブジェクトのクラス
      戻り値:
      指定されたクラスのインスタンス
      例外:
      ValidationException - プロバイダーが呼び出しをサポートしていない場合
      導入:
      1.1