tags:

views:

156

answers:

6

I have an object which i need to validate. This object has properties which cannot be null, strings which cannot exceed a given size and date strings which i need to check for format correctness.

How would you perform this validation. I don't want to go over the whole object, I'm looking for something more dynamic

EDIT:

This object will be used in webservices input so any setter validation will be useless also i dont know how the system will behave with the added dependency of any annotation based system

Im looking for a custom solution (yes i know reinventing the wheel)

+1  A: 

Without the exact scenario, I can recommend you use the object's setter methods (you did make the fields private, right?) to validate every change to the values. the String setters can call the length-validating code, the Date setters can call the format-validating code, etc.

Hope this helps,

Yuval =8-)

Yuval
This needs to be a POJO no logic inside. thx for the help
Nuno Furtado
POJO doesn't mean no logic inside.
CoverosGene
@NunFur: glad to be of service. perhaps you meant to say 'bean'? =8-)
Yuval
+1  A: 

Check Hibernate Validator.

bwalliser
thx for the link, im trying to avoid annotation, one solution would be to use oVal but i was looking for a custom solution. Im not looking for specific frameworks but solutions, algorithms
Nuno Furtado
why avoiding annotations?
bwalliser
This object will be used in webservices, I'm not sure how it would behave with added dependencies. anyway like i said above i was looking for a custom solution. (yes i know reinventing the wheel) =D
Nuno Furtado
Will the validation be kicked off on the client side of the webservice? If not, (if validation happens server side), you can use the annotations all you want. In that case, hibernate validator is probably your friend
Justin Standard
+1  A: 

Have a look at Apache Commons Validator

kgiannakakis
Commons Validator is a pain.
bwalliser
A: 

Suggest that you do your validation before setting the properties on the object. For example, if you have an int property that you're parsing from a string then you'll need to check for null, empty string, is a number, etc., before even setting it. Otherwise you'll run into type problems anyway. And, if you want to display the erroneous value back to a user then you'll need to keep hold of the original string.

If this is a web application then there are plenty of frameworks that will do this part for you, and then initialise your object with the already-validated and type-safe values. Struts or Spring-MVC come to mind, but I'm not a web developer. They use some sort of xml configuration that describes your object and associated validation, that you can also extend. If this is not a web thing then they might get in the way slightly as they'll want to handle the errors and display error pages directly to your users.

For something a bit lower down try Apache Commons Validator.

Josh
+1  A: 

Basic validation for webservice is handled on the WSDL/XSD level.

bwalliser
+1  A: 

It seems you want the object to be validated on the server side of a web service. If so, the answer I'm suggesting may apply to your situation.

As you've no doubt seen already, there are several ways of going about validating the fields in an object. But you know what needs to be done: check each field to see whether its value is valid. There is no magic here: you just check the fields.

If you're looking to keep your validation code in one place, one thing you might do is create an xml schema containing your validation logic, generate your class from that, and validate the objects you get over the wire using the schema as a reference. This makes it easy to maintain your validation code and your class: you simply update the schema, and re-generate the class. (I wouldn't even keep the class in source control. Just generate it before you compile your code. This guarantees that your class is always in sync with your schema.) I'm sure there are libraries for this in Java (JAXB, maybe?), but I haven't worked with Java for years. It's a very common thing to do in many languages.

Adam