More On Groovy Validation

I put my Groovy validators on github. So far it is all in a branch called “working”. There is not much in master.

I think the only way I can get it to work with immutable objects is if you declare the class and the fields final, and add the @Equals, @HashCode and @ToString annotations to the class. So you have to do a by hand most of the things that @Immutable just does for you. Getting my annotations to work with the @Immutable annotation might be difficult. Every time I try AST transformations, I always wind up saying to myself: That’s an hour of my life I am not getting back.

There are a couple of other projects that can be used for validation. Grails has constraints. The Grails constraints use the Spring Validator interface. (“Spring Validator” sounds like yet another Spring project, but it’s not.) It would be nice to validate POGOs without having to drag in Spring to do it. Looking at the Spring site (and a couple of examples like this one), it looks like you have to write a separate validator for every class you make. I want something simple and light.

There is also a project called on Sourceforge called OVal. That does a LOT of stuff, far beyond this project. It has 22 dependencies, 3 of them for logging alone. This allows you to do a LOT of stuff: you can have one field’s constraints depend on another property, you can have one property’s constraints dependent upon the state of another object, you can put the constrains in XML or annotations, it can also be used for Design By Contract. It does a lot of stuff. I wanted to make something similar to the Grails constraints for POGOs. It looks like OVal does a lot more than what I am looking for.

There is also Hibernate Validator. It implements some JSR, but when I read the documentation, it said I had to add two or three other JSRs. Honestly, I could not get it to work. You can use this to validate properties/fields. It can also validate method and constructor parameters. I just want something simple for POGOs.

Maybe I can refactor it to be a static constraints block like Grails has.


Image from “Sacramentarium [Sacramentaire de Charles le Chauve (?), frg.]”, a 9th century manuscript housed at the Bibliothèque nationale de France. Source / BnF; image assumed allowed under Fair Use.