Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Data Validation

 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Roel De Nijs
Sheriff
Posts: 10232
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10232
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Super, thanks Roel!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic