title: Adding validation information to REST responses
body: <p>I'm trying to build a (mostly) restful service, but I'm struggling with one part of the design. We expose various resources, which on the server side look like:</p>
  
  <pre><code>public class Thing1 : Resource {
    public string ABC {get;set;}
    public string DEF {get;set;}
  }
  </code></pre>
  
  <p>Where <code>Resource</code> is a base class:</p>
  
  <pre><code>public class Resource {
    public List&lt;Link&gt; Links {get;set;}
  }
  </code></pre>
  
  <p>Where <code>Link</code>s, in turn, just bind <code>rel</code>s and <code>uri</code>s. In this way, each <code>Resource</code> has links to other resources, etc, and the consumer can navigate through the various resources that the service offers.</p>
  
  <p>Some (but not all) resources are editable, and so the consumer will retrieve the resource, make changes to it, and then <code>PUT</code> those changes back to the service. Of course, at that point, the service will perform validation as required (and deal with any concurrency issues).</p>
  
  <p>But, as always, it would be nice if the consuming application could perform some validation up-front before even attempting the <code>PUT</code> request, to cut down on unnecessary round trips (in much the same way that we might use javascript validation even though the server has to repeat it).</p>
  
  <p>So, I'd like to include some validation information in our responses, so that the consuming application knows that (for instance), <code>ABC</code> cannot be longer than 6 characters. It should be noted that, at present, the consumer <em>can</em> use the same resource classes (they're in a separate assembly, together with appropriate <code>MediaTypeFormatter</code> classes) - adding attributes (e.g. <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.aspx" rel="nofollow noreferrer"><code>System.ComponentModel.DataAnnotations.RequiredAttribute</code></a>) feels wrong because then the consuming application ends up with the validation as it was at the time they took the shared assembly, rather than as it may be <em>now</em> in the service.</p>
  
  <p>There's also some validation that is more policy based where the actual validation properties cannot be computed until runtime.</p>
  
  <p><strong>tl;dr;</strong></p>
  
  <p>What's a good design for including "helpful" validation information in REST responses, alongside the actual resource, so that consuming applications can build good user experiences?</p>