Mar 11, 2015

Easily get GORM max size constraints

Oftentimes when designing a UI and working with field limits (such as an HTML input’s “maxlength” attribute), I’ve found myself wondering why it’s not more common to have the constraint come directly from the server. The frequent scenario seems to be that the database has one constraint for the field, and the UI has another — even if they happen to match.

For example:

Shouldn’t the UI limit come from the object’s field constraint? It would certainly make it easier to maintain should it ever change; plus it’s more clear on the UI where the value comes from. Grails has a great ValidationTagLib to report errors after-the-fact, but as far as I’m aware, there’s no easy way to get these constraints beforehand.

I decided to take a stab at creating a taglib that will do just this. It should hopefully be extendable to other fields in the future (such as “nullable” or “blank”), but this should be a good start. First let’s introduce our sample GORM object:

Note that it utilizes both the “maxSize” and the “size” constraints (sometimes both, for testing purposes). Other fields do not have any constraints. Ideally we would want the smallest valid value to be returned, otherwise some flag (null/empty string) when the field does not have such constraints. So let’s take a look at a potential implementation:

Walking through the code we see that an exception is raised when the proper parameters aren’t passed, or when the specified class or field do not exist. If the field does exist, we will take the lesser of “maxSize” and “size”, and return an empty string if no such constraint exists. I chose to use Class.forName() and specify the ClassLoader (rather than the simpler grailsApplication.getClassForName() approach) as it worked better for my tests.

And of course, some tests to illustrate the different scenarios:

Now that we have the taglib done, we can invoke the “limit” method with:

A bit more code than the alternative, but hopefully a lot more maintainable as well. I’ve posted the taglib up on GitHub in case I (or anyone else!) want to extend the functionality in the future. Hope this helps!

Igor Shults

About the Author

Object Partners profile.

One thought on “Easily get GORM max size constraints

  1. Clessio Mendes says:

    Well done. I’m borrowing your approach.

    1. Clessio Mendes says:

      Well done. I’m borrowing your approach. However, have you considered that the field size could be set as a mapping definition, or even could receive a default 255 size when not explicitly defined? In both cases, getting the actual field size from the inner database engine seems a even better approach.

      static mapping = {
      username length: 50
      firstname defaultValue: “john” //sets size as 255 by default
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Google Professional Machine Learning Engineer Exam 2021
Exam Description A Professional Machine Learning Engineer designs, builds, and productionizes ML models to solve business challenges using Google Cloud technologies and knowledge of proven ML models and techniques. The ML Engineer is proficient in all aspects […]
Designing Kubernetes Controllers
There has been some excellent online discussion lately around Kubernetes controllers, highlighted by an excellent Speakerdeck presentation assembled by Tim Hockin. What I’d like to do in this post is explore some of the implications […]
React Server Components
The React Team recently announced new work they are doing on React Server Components, a new way of rendering React components. The goal is to create smaller bundle sizes, speed up render time, and prevent […]
Jolt custom java transform
Jolt is a JSON to JSON transformation library where the transform is defined in JSON. It’s really good at reorganizing the json data and massaging it into the output JSON you need. Sometimes, you just […]