Validation
Much debate was/is going on about validation and its proper place in the application. Many try associate validation to a specific tier or layer of the application. Some say it belongs to the backend while others say it belongs to the frontend. Another common opinion is that it actually belongs to the domain model.
Personally, I strongly support those who say that validation doesn’t belong to a specific tier or layer in the application. It is rather a contextual issue that may be needed anywhere in the application and differ depending on the context. The best way to explain it, is by looking at an example… a real-world use case.
The Use case:
There’s an Account entity in the system. The user of the application should be able to add an Account. The Account has the following properties:
- username
- password
- shippingAddress
- billingAddress
(of course the real Account has many more properties but for this example these properties suffice)
Typically, there will be a form that the user will use to add an Account. A real-world form would probably be a bit more sophisticated then just showing text fields for all the properties. There should probably be another password field for the user, to re-type the password for confirmation. Another common requirement is that the form should be smarter when it comes to the billing and shipping addresses - there should be a checkbox that says “Use billing address as shipping address”. When this checkbox is checked, the user will only need to enter the billing address and the system should make sure that the shipping address is the same.
In a typical architecture, the form will be submitted to a web controller that will process the user input and in turn call an “addUser(…)” method on the backend.
Validation:
there are 3 types of validation that concern this use case:
- The two submitted passwords should be matched. If they don’t match the user should re-type them.
- The submitted fields relate to the user properties as their values. Obviously there are certain constraints on these properties that the submitted values should adhere to. For example, the billing address cannot be null, the username cannot be empty, and the password needs to be between 6 to 8 characters long.
- The username should be unique in the system. That is, if the submitted username is already in use by another account, the user should enter a different username.
I specially chose these 3 validation types, for they map quite nicely with the different tiers or layers in the application. Let’s examine how:
- The first validation is making sure that the user has typed the password s/he intended to. This is a pure frontend tier validation, for it deals with the user experience on the frontend. Theoretically, if for example the user had to choose a password out of a given list of passwords, there wouldn’t be any need for this validation - it’s all about how the user works with the presented form. In any case, whatever form is shown, both the backend functionality and the domain model (the Account entity) stay the same, so this type of validation definitely doesn’t belong to either of these tiers.
- The second validation is making sure that the values of the Account properties adhere to the domain model constraints. As you can guess, this validation strictly belongs to the domain model layer. The domain model is a vertical layer - it is used and accessed by the other horizontal layers. So are the constraints for the model. For example, on the frontend, it makes sense that that form presented to the user will be “aware” of these constraints, and force the user to enter only values that adhere to them. On the backend, it makes sense that these constraints will map correctly to the database if the domain class (Account in this case) is persisted. Since these constraints are practically used everywhere the domain model is used, it only makes sense to couple them together.
- The third and last validation type is to make sure there are no two Accounts with the same username. This is a classic business rule which naturally fits in the backend - where all business rules belong. Putting this validation in a different tier is a clear mistake. For example, if put in the frontend, then when another interface is exposed for the backend (e.g. web services) this rule is absent, and if not implemented in the new interface, will be lost and the application may be left in an inconsistent state… clearly a mistake.
The example above clearly shows that when talking about validation, the first question that needs to be asked is “what type of validation?”. Once this question has been answered, the next step is to start thinking how that validation should be implemented.
next stop… Validation in practice
January 12th, 2006 at 2:27 pm
I completely agree with your distinction of three validation types.
Hope to read about your next stop soon - IMHO the main problem lies in transporting/indicating validation errors of type 2 and 3 to/on the frontend.
January 13th, 2006 at 2:49 am
[…] Dev Thoughts Java, Spring, Development, and the rest « Validation […]
January 13th, 2006 at 6:37 pm
I disagree with your categorization of password validation as ‘pure front-end’ for security reasons. Any web page can be spoofed to remove client-side validation. Therefore all validation must be done on the server. Any validation done on the client is purely decorative and should not be critical to the application.
January 13th, 2006 at 6:41 pm
Actually I agree with you :-). I’m afraid there’s a small misunderstanding here. When I talk about front-end, I refer to the server side presentation tier, not the client of the application (browser).
January 13th, 2006 at 6:46 pm
Furthermore, to be quite honest, I don’t really believe in client side validation. It may look like you’re providing a “better” user experience, but in fact, it’s quite the opposite. Anyway you look at it, some validation must be done in the server side, and when combined with the client side, you can get strange and inconsistent behavior of validation error messages. This is why I like to keep ALL validation in the server side. BTW, Ajax doesn’t solve this problem as well… Actually it makes it worse.
January 29th, 2007 at 3:18 am
Hi,

I found your blog via google by accident and have to admit that youve a really interesting blog
Just saved your feed in my reader, have a nice day
January 30th, 2007 at 9:42 am
I am so much attracted by the validation given…very nice one! Keep going on…
Don Lapre Zach
webmaster@donlaprejones.com
www.donlaprejones.com
February 23rd, 2007 at 5:37 pm
Pretty good summation. I think you should be awarded this months community service medal for saying something useful