javax-bigdecimal-validation
Javax BigDecimal検証
1. 前書き
チュートリアルlink:/javax-validation[Java Bean Validation Basics]では、基本的な_javax_検証をさまざまなタイプに適用する方法を説明しました。このチュートリアルでは、_BigDecimal_で_javax_検証を使用することに焦点を当てます。 。
2. _BigDecimal_インスタンスの検証
残念ながら、* _ BigDecimal_では、従来の_ @ Min_または_ @ Max_ javax注釈を使用できません。*
幸いなことに、それらを操作するための専用の注釈セットがあります。
-
_ @ DecimalMin_
-
_ @ Digits_
-
_ @ DecimalMax_
_BigDecimal_はlink:/java-bigdecimal-biginteger [精度が高いため、財務計算の最初の選択肢]です。
タイプ_BigDecimal_のフィールドを持つ_Invoice_クラスを見てみましょう。
public class Invoice {
@DecimalMin(value = "0.0", inclusive = false)
@Digits(integer=3, fraction=2)
private BigDecimal price;
private String description;
public Invoice(BigDecimal price, String description) {
this.price = price;
this.description = description;
}
}
2.1. _ @ DecimalMin_
*注釈付き要素は、指定された最小値以上の値の数値である必要があります。
2.2. _ @ DecimalMax_
_ @ DecimalMax_は_ @ DecimalMin_の対応物です。 注釈付き要素は、指定された最大値以下の値である必要があります。 _ @ DecimalMax_には、指定された最大値が包含的か排他的かを指定する_inclusive_属性があります。
また、_ @ Min_および_ @ Max_は_long_値のみを受け入れます。 _ @ DecimalMin_および_ @ DecimalMax_では、_string_形式で値を指定できます。これは、任意の数値型にすることができます。
2.3. _ @ Digits_
多くの場合、_decimal_数の_integral_ partおよび_fraction_部分の桁数を検証する必要があります。
-
_ @ Digit_アノテーションには、_integer_と_fraction_の2つの属性があり、number .の_integral_部分と_fraction_部分で許可される桁数*を指定します。
https://docs.oracle.com/javaee/7/api/javax/validation/constraints/Digits.html [公式ドキュメント]にあるように、_integer_を使用すると、この番号に受け入れられる_integral_桁の最大数*を指定できます。 。 ただし、これは10進数以外の場合にのみ当てはまります。 _decimal_数値の場合、数値の_integral_部分の正確な桁数をチェックします。 これはテストケースで確認できます。
同様に、_fraction_属性を使用すると、この番号に許可される_fractional_桁の最大数を指定できます。*
2.4. テストケース
これらのアノテーションの動作を見てみましょう。
最初に、検証に従って無効な価格で請求書を作成し、検証が失敗することを確認するテストを追加します。
public class InvoiceUnitTest {
private static Validator validator;
@BeforeClass
public static void setupValidatorInstance() {
validator = Validation.buildDefaultValidatorFactory().getValidator();
}
@Test
public void whenPriceIntegerDigitLessThanThreeWithDecimalValue_thenShouldGiveConstraintViolations() {
Invoice invoice = new Invoice(new BigDecimal(10.21), "Book purchased");
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
assertThat(violations.size()).isEqualTo(1);
violations.forEach(action -> assertThat(action.getMessage())
.isEqualTo("numeric value out of bounds (<3 digits>.<2 digits> expected)"));
}
}
整数値である正しい価格で検証をチェックしましょう:
@Test
public void whenPriceIntegerDigitLessThanThreeWithIntegerValue_thenShouldNotGiveConstraintViolations() {
Invoice invoice = new Invoice(new BigDecimal(10), "Book purchased");
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
assertThat(violations.size()).isEqualTo(0);
}
整数部に3桁を超える価格を設定すると、検証エラーが表示されるはずです。
@Test
public void whenPriceIntegerDigitGreaterThanThree_thenShouldGiveConstraintViolations() {
Invoice invoice = new Invoice(new BigDecimal(1021.21), "Book purchased");
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
assertThat(violations.size()).isEqualTo(1);
violations.forEach(action -> assertThat(action.getMessage())
.isEqualTo("numeric value out of bounds (<3 digits>.<2 digits> expected)"));
}
000.00に等しい価格でも制約の検証が必要です。
@Test
public void whenPriceIsZero_thenShouldGiveConstraintViolations() {
Invoice invoice = new Invoice(new BigDecimal(000.00), "Book purchased");
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
assertThat(violations.size()).isEqualTo(1);
violations.forEach(action -> assertThat(action.getMessage())
.isEqualTo("must be greater than 0.0"));
}
最後に、価格が0より大きいケースを見てみましょう。
@Test
public void whenPriceIsGreaterThanZero_thenShouldNotGiveConstraintViolations() {
Invoice invoice = new Invoice(new BigDecimal(100.50), "Book purchased");
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
assertThat(violations.size()).isEqualTo(0);
}
3. 結論
この記事では、BigDecimalで_javax_検証を使用する方法を見ました。
すべてのコードスニペットを見つけることができますhttps://github.com/eugenp/tutorials/tree/master/javaxval[GitHubで]