-
Notifications
You must be signed in to change notification settings - Fork 0
Check Reference
All Checks are essentially Closures that return ResultMaps. The reference type must be Check to allow for composition behavior, but Check is also a Single Abstract Method class (SAM) so if you don't directly use any of the facilities bellow then a compatible Closure or Function will be automatically coerced.
This is the most basic building block.
test
is evaluated and if false then makeResult
is
called. Both test
and makeResult
accept the input as their single
argument. makeResult
being a Closure also allows for more dynamic
creation of ResultMap
entries (though GStrings should also provide enough dynamism for most cases).
satsfies({ it.age >= 18 },
{ ResultMap.from(
[age:new Result("${it.age}-year-olds can't vote", 'TOO_YOUNG')]
)})
test
is evaluated and if false then create a ResultMap using the values provided by the mold Map.
This is used to construct all of the other provided Checks and uses the satisfies(Closure, Closure)
signature above.
satisfies({ it.age >= 18 }, key:'age', msg:'Must be 18 or older', code:'TOO_YOUNG');
Note that the mold values are untyped to allow for the use og GStrings in addition to Strings. Ensuring the GStrings are expanded late allows for use of the input
parameter within the method and some of the powerful features afforded to GStrings. Below is an example using GStrings which provides the same functionality as the example for the above signature.
satisfies({ it.age >= 18 }, key:'age', msg:"${input.age}-year-olds can't vote", code:'TOO_YOUNG');