I want to do something like:
class MyCommand {
String name
String data
static constraints = {
name blank: false, size: 3..64
data (blank: true) || (blank: false, size: 3..64)
}
}
where data is either blank or follows validation such as a size constraint if it is not blank. Is this possible to do without custom validation?
It would be non-trivial to use other constraints within a validator constraint. The delegate of the constraints closure is a ConstrainedPropertyBuilder, which you can read to understand the complexity.
But that doesn't matter because the EmailConstraint uses Apache's EmailValidator, which you can use in your validator. Here's the EmailValidator in action:
You can use EmailValidator in your own validator like this: