title: Validation & Validation Rules for Web Services
body: <p>I have experienced the following scenario in most web service apps I've worked on:</p>
  
  <ol>
  <li>We <strong>create a Domain Model</strong> (in C#). This is the heart of the application. The Domain Model contains business and validation rules, that determine under what conditions an entity is valid or not.</li>
  <li>We <strong>create a "web service" layer</strong> (in C#/WCF). <strong>This layer defines DTO-like objects, that are exposed by the web services.</strong> The DTO-like objects are sliced and assembled from pieces of the domain entities, usually in a coarse grained way.</li>
  <li><strong>On the web client (JavaScript &amp; HTML) the validation rules are duplicated</strong> in a different format, usually some form of JavaScript validation.</li>
  </ol>
  
  <p>How can the validation rules attached to the pieces of domain entities, be exposed to the client, through the web services? The reason for this is that the domain model's validation rules should be defined once, and then be available to consumer clients in some format throughout the rest of the system.</p>
  
  <p><em>The only solution I've come up with so far is to make the validation rules from the domain, available in some form of metadata, defined in XML, JSON, or similar. The big problem with this is that schema between the service layer's DTOs and the domain entities are different, and therefore the domain's validation rules can't be directly sent to the web client - the client works with a different schema, and the domain model.</em></p>
  
  <p>Therefore my question is: <strong>What is the approach requiring the least amount of manual and duplicated code, that maps between the different schemas and layers in the application, but allow everything to interpret the validation rules?</strong> </p>