Over a million developers have joined DZone.

A Simple Scala Validation Framework

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

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!

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.


Published at DZone with permission of Wille Faler, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}