예제 #1
0
 private boolean validateJsonConf() throws IOException, ProcessingException {
   JsonNode schemaNode =
       JsonLoader.fromFile(new File("src/main/resources/ConfigurationSchema.json"));
   JsonSchemaFactory schemaFactory = JsonSchemaFactory.byDefault();
   /*
   Here we consume the original jsonConfigReader in the process of validation and recreate a Reader from JsonNode
   then assign it back to the jsonConfigReader, if the validation succeed.
   Can we improve this logic?
    */
   if (schemaFactory.getSyntaxValidator().schemaIsValid(schemaNode)) {
     JsonSchema jsonSchema = schemaFactory.getJsonSchema(schemaNode);
     JsonNode topologyConfig = JsonLoader.fromReader(jsonConfigReader);
     ProcessingReport report = jsonSchema.validate(topologyConfig);
     if (report.isSuccess()) {
       log.info("Validation succeeded... ");
       log.info(report.toString());
       jsonConfigReader =
           new InputStreamReader(new ByteArrayInputStream(topologyConfig.toString().getBytes()));
       return true;
     } else {
       log.error("Validation failed...");
       log.error(report.toString());
       throw new RuntimeException(
           "Configuration file validation failed, Please recheck configuration file and try again");
     }
   } else {
     log.error("Json configuration schema is not valid ");
     log.error("Terminate the process .....");
     throw new RuntimeException("Json Configuration schema is not valid");
   }
 }
예제 #2
0
  public static void main(final String... args) throws IOException {
    final JsonNode fstabSchema = loadResource("/fstab-inline.json");
    final JsonNode good = loadResource("/fstab-good.json");
    final JsonNode bad = loadResource("/fstab-bad.json");
    final JsonNode bad2 = loadResource("/fstab-bad2.json");

    final JsonSchemaFactory factory =
        JsonSchemaFactory.builder().addressingMode(AddressingMode.INLINE).build();

    final JsonSchema schema = factory.fromSchema(fstabSchema);

    ValidationReport report;

    report = schema.validate(good);
    printReport(report);

    report = schema.validate(bad);
    printReport(report);

    report = schema.validate(bad2);
    printReport(report);
  }