Skip to content
zhaber edited this page Jul 27, 2012 · 4 revisions

A validator is a function that examines its input with respect to some requirements and produces a boolean result which indicates whether the input successfully passed the validation. This snippet shows how to implement a validator that tests if the supplied value is even:

boolean even(long value) {
  value % 2 == 0
}

As grules is the internal Groovy DSL, the return keyword is optional at the end of a function body. The return type of a validator should be boolean or Boolean. The other option to indicate a failed validation result is using a ValidationException.

The first validator parameter is called a processed value parameter, as it represents a value of a parameter processed by a current rule.