title: Handling JSON posts in Yesod
body: <p>An AngularJS client is sending a JSON post to a Yesod server to update a person record. The post can contain the following fields each of which is optional - the client can send any subset of these:</p>
  
  <ul>
  <li>firstName</li>
  <li>lastName</li>
  <li>...</li>
  <li>active</li>
  </ul>
  
  <p>To limit the discussion a bit lets assume the client, at the moment, only wants to toggle activity, so it will only send the active value (it specifically wants to keep the rest intact) and the message will be:</p>
  
  <pre><code>{
    active: 0
  }
  </code></pre>
  
  <p>On the server now, we know the id of the person from the URL (eg. /api/v1.0/person/1) but the client does not send a complete Person entity, so the usual:</p>
  
  <pre><code>person &lt;- requireJsonBody :: Handler Person
  _ &lt;- runDB $ update personId ...
  </code></pre>
  
  <p>will not work here. It would seem a more flexible approach is needed. Maybe something along the lines of:</p>
  
  <pre><code>mapToUpdate :: PersonInfo -&gt; [Update PersonInfo]
  </code></pre>
  
  <p>where PersonInfo is an instance of <code>FromJSON</code> and is defined to match Person but has all the fields of type <code>Maybe a</code>. However, that seems totally contrary to DRY.</p>
  
  <p>So to wrap this up: how would one handle such a use case in Yesod nicely going back and assuming again the client can send any subset of a Person's fields?</p>
  
  <p>You could imagine even more horrifying scenarios. For example one JSON post needing to be mapped to an update of multiple database entities (api entities do not have to map 1:1 to database entities).</p>