title: Best practices for model validation using a REST API and a javascript front-end such as Angular
body: <p>I'm transitioning towards more responsive front-end web apps and I have a question about model validation. Here's the set-up: the server has a standard REST API for inserting, updating, retrieving, etc. This could be written in Node or Java Spring, it doesn't matter. The front-end is written with something like Angular (or similar).</p>
  
  <p>What I need is to figure out where to put the validation code. Here's the requirements:</p>
  
  <ul>
  <li><p>All validation code should be written in one place only. Not both client and server. this implies that it should reside on the server, inside the REST API when persisting.</p></li>
  <li><p>The front-end should be capable of understanding validation errors from the server and associating them to the particular field that caused the error. So if the field "username" is mandatory, the client can place an error next to that field saying "Username is mandatory".</p></li>
  <li><p>It should be possible to validate correct variable types. So if we were expecting a number or a date and got a string instead, the error would be something like "'Yo' is not a correct date."</p></li>
  <li><p>The error messages should be localized to the user's language.</p></li>
  </ul>
  
  <p>Can anyone help me out? I need something simple and robust.</p>
  
  <p>Thanks</p>