Is there an idiomatic solution to applying a series of business rules, for example, from an incoming JSON request. The "traditional" Java approach is very if-then
intense, and Scala must offer a far better solution.
I've experimented a bit with pattern matching but haven't really come up with a pattern that works well. (Invariably, I end up with absurdly nested match
statements)...
Here's an absurdly simple example of what I'm trying to do:
if (dateTime.isDefined) {
if (d == None)
// valid, continue
if (d.getMillis > new DateTime().getMillis)
// invalid, fail w/ date format message
else
if (d.getMillis < new DateTime(1970).getMillis)
// invalid, fail w/ date format message
else
// valid, continue
} else
// valid, continue
if (nextItem.isDefined) {
// ...
}
I'm thinking perhaps an approach that uses a series of chained Try()
... but it seems like this pattern must exist out there already.