title: Validate a JSON and provide a custom validation report
body: <p>I have an application that receives a JSON and upon receiving it should <strong>validate</strong> it. The validation is consisted out of several steps.</p>
  <ol>
  <li>If the received object is a properly formatted JSON</li>
  <li>If all the required fields are present</li>
  <li>If all the fields constraints are satisfied (ie. string sizes)</li>
  <li>If the conditional dependencies are satisfied (if some field has value A, then another field should have value B)</li>
  </ol>
  <p>Based on the described requirements, if any of the constraints is violated, I should generate my own, specifically formatted, <strong>validation report object</strong>.</p>
  <p>I have considered several ways how to do this, but I am not really happy with any, and hoping to get an advice from you guys, how this could be done. What I have considered:</p>
  <h2>Idea #1: Jackson + JSR303 bean validation</h2>
  <pre><code>@CustomConstraintIfAThenB // this should emulate the conditional dependency
  public class Message {
      @JsonProperty(value = &quot;a&quot;, required = true)
      @Size(min = 3, max = 8)
      private String a;
  
      @JsonProperty(value = &quot;b&quot;, required = true)
      @Size(min = 5, max = 9)
      private String b;
  
      // ...
  }
  </code></pre>
  <p>The problem I found with this approach is when generating my report, because first I need to use the Jackson's <code>ObjectMapper</code> to try to create my POJO. If this throws <code>JsonProcessingException</code> I should parse it, and create my report. If doesn't throw, only then I can validate with my bean validator (ie. <code>Hibernate</code>).</p>
  <pre><code>MyReport report = null;
  Message m = null;
  
  // first Jackson validation
  try {
      m = objectMapper.treeToValue(json, Message.class);
  } catch (JsonProcessingException e) {
      report = myExceptionParser.parse(e);
  }
  
  // second bean (field) validation
  Set&lt;ConstraintViolation&lt;Message&gt;&gt; violations = validator.validate(m);
  if (violations.size() &gt; 0) {
      report = myViolationsParser.parse(violations);
  }
  </code></pre>
  <p>Big problem with this is the actual <strong>parsing</strong> through the <code>JsonProcessingException</code> to create my custom report. And additionally parse through the <code>violations</code>. I should write two parsers, one of which (the one that parses the exception) would probably be extremely clumsey.</p>
  <h2>Idea #2: Validation against JSON schema</h2>
  <p>I could write a JSON schema and then validate an incoming JSON against that schema. The up side for this is that I would have a unique, consistent report generated by the JSON Schema Validator I am using, and I should only write one parser for it. For example if I were to use <a href="https://github.com/java-json-tools/json-schema-validator" rel="nofollow noreferrer">fge-validator</a>:</p>
  <pre><code>JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema(mySchema);
  ProcessingReport report = mySchema.validate(json);
  
  // only have one parser to parse the report to my custom report
  MyReport = myReportParser.parse(report);
  </code></pre>
  <p>The problem is with this that <strong>JSON Schema v4</strong> gets very clumsy when dealing with conditional dependencies, as there are no IF-THEN-ELSE keywords. And I couldn't find a library that would support JSON Schema v7. Also, modifying a large JSON Schema can get tricky and very prone to errors. In that context also, class annotations would be better.</p>
  <p>Is there any 3rd way to do the JSON validation and create a custom report out of it, that is moderately elegant and not clumsy as writing two parsers (one of which parses the exception)?</p>
  <p>Please also note that my JSON files can easily have 200+ fields, so writing a custom deserializer field-by-field is probably not an option.</p>
  <p>I would like to get some advises on this, as I can't really find a reasonable way to do this.</p>