title: How can request params be validated to ensure they include required params and don’t include unsupported params?
body: <p>This is particularly in the context of a <code>REST</code> API built with <code>Ruby</code> and <code>Sinatra</code>.</p>
  
  <p>It's easy enough to manually check to make sure that the required params are not nil. And it's easy to iterate through a flat params hash to see if it's allowed in a whitelist. </p>
  
  <p>However, when the params hash also include hashes it becomes more difficult.</p>
  
  <p>One way of handling this I've thought of is converting the params hash to <code>JSON</code> and using a library to validate it against a <code>JSON</code> schema.</p>
  
  <p>I have come across the <a href="https://github.com/mattt/sinatra-param" rel="nofollow">sinatra-param</a> gem but I haven't had a chance to see if it can validate sub-hashes or check for unsupported params.</p>
  
  <p><strong>Edit:</strong> Another possible way, that might make more sense is passing params directly to the model (I'm  using DataMapper) and using its validation and errors instead of rewriting validations.</p>