Environment: Grails 2.3.8
I have a requirement that the user's password could null but can't be blank. So I define the domain like this:
class User{
...
String password
static constraints = {
...
password nullable:true, blank: false
}
}
I wrote a unit-test for the constraints:
void "password can be null but blank"() {
when: "create a new user with password"
def user = new User(password: password)
then: "validate the user"
user.validate() == result
where:
password | result
"hello" | true
"" | false
null | true
}
The "hello" and null cases are fine, but the blank string("") fails: junit.framework.AssertionFailedError: Condition not satisfied:
user.validate() == result
| | | |
| true | false
| false
app.SecUser : (unsaved)
at app.UserSpec.password can be null but blank(UserSpec.groovy:24)
- Is nullable overriding the blank: false?
- I know that I can use a custom validator to implement the requirement, I'm curious is there any better way?
- Am I doing something wrong?