title: MVC web app - where to ensure data validity?
body: <p>I'm using Spring 3 but this question is more widely applicable, I think.</p>
  
  <p>I have a web app with a front controller (a Spring <code>DispatcherServlet</code>).  From there a service is called which in turn uses a DAO or two to accomplish the desired task, such as causing a record to be inserted into the database.</p>
  
  <p>Where do I ensure the data is valid?  Let's say I have an entry that has a maximum length of 50 characters - at what point should I assert the input's length is &lt;= 50?  </p>
  
  <p>If I check validity in the controller then it's possible other code calling the services could be written that doesn't verify data.</p>
  
  <p>I could check the validity in the service layer, but perhaps someone may write a service that doesn't check the validity of the data, thus passing "bad" data to a DAO.</p>
  
  <p>It then may seem obvious to put the validity checks in the DAOs - would that then impact my ability to test the code?</p>
  
  <p>What are the best practices when it comes to checking data validity?</p>