[Bean Validation] When java.lang.Boolean is returned by a method starting with "is ~" Cannot be validated with AssertTrue / AssertFalse [Java]

The content of the article is as the title.

The situation that happened

In the correlation check using ʻAssertTrue, when I tried to perform validation such as" Error 1 if false, Error 2 if null` ", the method was not called at the time of validation.

@AssertTrue(message = "Hogehoge")
@NotNull(message = "Fuga Fuga")
private Boolean isValid() {
  /*Correlation check*/
}

Cause

From the official document, it is as follows. To put it plainly, java.lang.Boolean is an object, so giving method names starting with ʻis ~` is a convention violation.

It's a matter of course when you think about it, but it's complicated, so please forgive me ...

What is a getter? The JavaBeans specification specifies that a getter is a method whose

  • name starts with get and has a return type but no parameter
  • name starts with is, has no parameter and is returning boolean

Exhibitor: Bean Validation specification

Coping

You can have it called by changing the method name. However, in this case, I don't think it is necessary to specify Boolean.

There is also the issue of gettingters and names for fields.

@AssertTrue(message = "Hogehoge")
@NotNull(message = "Fuga Fuga")
private Boolean getValid() {
  /*Correlation check*/
}

I couldn't find any other solution.

Impressions

I thought I could run multiple correlation checks on the same field without annotating it, but it didn't work. It's quite painful to try to balance value acquisition and validation, so it's wise to separate the validation object and the value acquisition object, especially when things get complicated.

Recommended Posts

[Bean Validation] When java.lang.Boolean is returned by a method starting with "is ~" Cannot be validated with AssertTrue / AssertFalse [Java]
[Bean Validation] When java.lang.Boolean is returned by a method starting with "is ~" Cannot be validated with AssertTrue / AssertFalse [Java]
Cause of is not visible when calling a method of another class in java
Add Bean Validation with Micronaut (Java)
[Java] Proxy setting method when starting Java
A memorandum when IME cannot be turned on with VS Code (Ubuntu 20.04)
[Java] When checking URL format with Bean Validation, it may be better not to use @URL of Hibernate Validator.