-
Notifications
You must be signed in to change notification settings - Fork 10
Validation block
zhaber edited this page Aug 2, 2012
·
1 revision
Regardless of the DSL expressivity, you may still have preprocessing logic that cannot be defined as a grules rule. Such logic can be always placed in a validate block which should return a map of clean values processed by this block or throw a ValidationException
if any of the values fail validation. For example:
GET:
id toInt
...
name trim
validate {
def sum = ($options.collect { // options is an HTTP array parameter
it == ‘Yes’ ? 1 : 0
}).sum()
if (sum <= 1) {
throw ValidationException(NOT_ENOUGH_OPTIONS,
‘Please choose more options’, ‘options’)
}
// return two clean parameters
[options: $options, chosenOptionsNum: sum]
}
POST:
...
validate {
...
}
```