X4HT
- All
- Java
- Technology
- life
About Valang validator
About two months ago I started working on an article on Spring Web Flow - it still isn't finished today. I got in touch with Keith Donald and Erwin Vervaet on the the Spring dev mailing list and got a lot of feedback for the article. We discussed a number of topics on how to improve/extend Web Flow, including validators. Web Flow nicely works with Spring validators to validate (parts of) command objects and uses the outcome of this validation step to steer the flow engine.
Back then validators where the only Java code you had to write next to command objects when working with Web Flow. All the common tasks are taken care of by Web Flow out of the box. It makes sense validation remains a custom job since it's related to a command object that's specific per application. However, it didn't make sense to me to create a class and write the validation rules in Java.
If you've ever written a validator implementation - for Spring or any other framework - you may just like me have felt it's really tedious. Java is not Perl and testing values in Java is not as easy as testing values in Perl. However, the same basic little tasks keep on coming back when writing validation rules: is this equal to that, it this less than that, is this null, is that ..... .
So I imagined there must be a better way. I wanted a simple syntax where you could say:
age >= 18 and length of firstName <= 30
in the Spring config file.
So I started work on a parser. I used javacc before and wrote a first draft of the parser BNF file. I quickly was faced with the same challenges faced by all language designers: left recursion, parentheses, functions, ... . You need to make a lot of choices when writing a parser for a new language and luckily I had 50+ years of language design, wikipedia, my personal experience with the features and limitations of several programming languages and the JXPath BNF file to look at when making these choices. I really learned a lot about language design and javacc and hope to usage this knowledge in other projects where a parser can make our lives a bit easier.
The result is a parser that supports a fairly extensive feature set specifically aimed at doing validation. It supports literals, bean properties, parentheses, logical operators, functions, mathematical expressions and localization amongst other things. It has its own date parser and reuses the predicates from commons-collections. The source code currently lives in the Spring Modules sandbox and is currently scheduled for release with Spring Modules 0.3. There's an article on the Spring Modules wiki that discusses using Valang validator, its features and its syntax.
Valang validator isn't final yet. I already received a lot of invaluable feedback from users that allowed me to fix bugs and extend the functionality. For now the only feature I'm thinking of that's not yet implemented is support for regular expression. I haven't looked into the details yet and it will probably not make it into the source for Spring Modules 0.3.
I'm interested in hearing what you have to say about the current feature set. If there's anything you would like to see changed or added please let me know.
Posted at 03:05PM May 28, 2005 by Steven Devijver in Java | Comments[2]