title: What is the most lightweight way to check that a JSON object conforms to an expected schema?
body: <p>This is seems to be a common tedious pattern:</p>
  
  <pre><code>function handleResponse(httpResponseBody) {
    const body = JSON.parse(httpResponseBody);
    if (body.someField &amp;&amp;
        body.someField.length &gt;= 5 &amp;&amp;
        body.someField[4].otherField &amp;&amp;
        body.someField[4].otherField.finalField) {
      /* ... do something with finalField ... */
    }
  }
  </code></pre>
  
  <p>A little contrived, but the point is how to avoid boilerplate validation logic like this without resorting to full JSON schema validation for the body if you just are going to surgically access some portion of the object and want to minimally validate that that portion is structured as expected? Something lightweight, conceptually as simple as:</p>
  
  <pre><code>function handleResponse(httpResponseBody) {
    const body = JSON.parse(httpResponseBody);
    if (validate(body, '{ someField[&gt;=5].otherField.finalField }') {
      /* ... safe to access body.someField[4].otherField.finalField directly ... */
    }
  }
  </code></pre>
  
  <p>or possibly:</p>
  
  <pre><code>function handleResponse(httpResponseBody) {
    const body = JSON.parse(httpResponseBody);
    const finalField = access(body, '{ someField[4].otherField.finalField }');
    if (finalField) {
      /* ... */
    }
  }
  </code></pre>