Over a million developers have joined DZone.

A Simple Scala Validation Framework

DZone 's Guide to

A Simple Scala Validation Framework

· Java Zone ·
Free Resource

I was on the lookout for Scala validation frameworks, and I couldn’t readily find any that where not part of/dependent on some web- or other framework. Furthermore, I didn’t want to use a Java validation framework with all the compromises that would entail, so realising it would be relatively trivial to do one in Scala, I knocked one together myself in a few evenings.

Hey presto, here it is! (bundled with a few other utility classes). It should be relatively easy from the README section further down on the page linked to how to use it (the code is built with SBT). However to explain a few things I wanted to achieve by writing my own:

  • Validate any object or object-graph with the same concise API: Well, duh, obviously. :)
  • No external dependencies and deployable/usable in any tier of an application: from frontend to backend.
  • Separate the concerns of validation and domain: Hibernate Validator and the Bean Validator JSR’s use annotations, which works in some respects, but is inappropriate in many scenarios. For instance you may want to validate an address differently depending on what country context it is in.
  • Use the power of functions in Scala: Many if not most Java validation frameworks use annotations, reflections and various other tricks. In Scala this is not necessary, as a lot of it can be circumvented simply by using Scala’s functional programming aspects.
  • Separate the concerns of validation and error message loading: At the moment there is a classpath resource MessageResolver defined that supports localisation, however you can very easily roll your own. You may want to manage messages in different ways (property-files, xml-files, in a database etc).
  • Composability: Use as much or as little of the framework, roll your own implementations and/or hook into the validation framework at the level of abstraction you find useful.

This is something I’ve pretty much put together in a few evenings, so there is probably room for cleaning up and improving the code, though hopefully it is somewhat bug free at less than 200 lines of code with 18 unit tests.. Further, I’m just starting to dive deeper into the functional aspects of Scala, so my “Java damage” may still shine through.

But feedback is most welcome, so go get the source from GitHub!


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}