aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Data Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Data Validation" Watch "Data Validation" New topic
Author

Data Validation

Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

How have you guys approach validating the data to make sure that it is not too long for the fields in the data file?

At the moment I am using a cache which contains Room objects. I've no restriction on the length of any values stored in the object e.g. location.

When it comes to writing my cache back to the data file, obviously I can only write back a certain size string for the location. So in theory the entire location string may not be written to the file as there is not enough room for it.

I could place validation on

1) The GUI, to restrict the size of string a user enters
2) The Room object, I could throw an IllegalArgumentException if the string is too long
3) When writing my cache back to the file

I guess (1) would work and prevent a string that is too long ever getting into the system. But I'd prefer to have validation within the system that is not at the GUI level, to cater for the possibility of a different View in the MVC setup.

I could do (3), but it's a bit late to be throwing exceptions when we are writing the cache back to the file.

So that just leaves me with (2) as being the most reasonable one.

Or I could just ignore this problem and document in my choices.txt



What do you guys think? What approaches have you taken?


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5407
    
  13

I don't use Room-objects in my Data class, so this class is completely unaware of the type of objects it is processing and can be used for processing other database files (e.g. customers, hotels,...)

Each String[] parameter is checked against a range of validation (number of fields, length of each field value,...). Whenever such validation fails an IllegalArgumentException is thrown. Of course in my application these exceptions are never thrown, because I make sure only valid String[] are passed to my Data class But another developer will get a clear message when passing an invalid String[] to a method of the Data class. And a 2nd (big) advantage: your code is kept clean and simple, because I don't have to add null pointer, size, field length,... checks to my actual code: I know that every String[] passing the validation method is a valid one


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roel De Nijs wrote:I don't use Room-objects in my Data class, so this class is completely unaware of the type of objects it is processing and can be used for processing other database files (e.g. customers, hotels,...)


Don't you have your cache in your data class? So you used String[] as the value in your Map?

I've gone for the option of hard-coding in the schema information, so not too interested in reusability . I think this way also makes it easier for a Junior programmer to understand.

Roel De Nijs wrote:Each String[] parameter is checked against a range of validation (number of fields, length of each field value,...). Whenever such validation fails an IllegalArgumentException is thrown. Of course in my application these exceptions are never thrown, because I make sure only valid String[] are passed to my Data class But another developer will get a clear message when passing an invalid String[] to a method of the Data class. And a 2nd (big) advantage: your code is kept clean and simple, because I don't have to add null pointer, size, field length,... checks to my actual code: I know that every String[] passing the validation method is a valid one


So in your Data class when you are adding or updating a record, you have checks to ensure that the string values match the schema? And this way you ensure you cache always has records that match the schema, so when writing the cache back to the file you don't need to validate the data?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5407
    
  13

Sean Keane wrote:Don't you have your cache in your data class? So you used String[] as the value in your Map?


Sean Keane wrote:So in your Data class when you are adding or updating a record, you have checks to ensure that the string values match the schema? And this way you ensure you cache always has records that match the schema, so when writing the cache back to the file you don't need to validate the data?
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Super, thanks Roel!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data Validation