Java Code Examples for com.github.fge.jsonschema.main.JsonSchemaFactory#getValidator()

The following examples show how to use com.github.fge.jsonschema.main.JsonSchemaFactory#getValidator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CEFParserTest.java    From metron with Apache License 2.0 5 votes vote down vote up
protected boolean validateJsonData(final String jsonSchema, final String jsonData) throws Exception {
	final JsonNode d = JsonLoader.fromString(jsonData);
	final JsonNode s = JsonLoader.fromString(jsonSchema);

	final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
	JsonValidator v = factory.getValidator();

	ProcessingReport report = v.validate(s, d);

	return report.toString().contains("success");
}
 
Example 2
Source File: LEEFParserTest.java    From metron with Apache License 2.0 5 votes vote down vote up
protected boolean validateJsonData(final String jsonSchema, final String jsonData)
    throws Exception {
  final JsonNode d = JsonLoader.fromString(jsonData);
  final JsonNode s = JsonLoader.fromString(jsonSchema);

  final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
  JsonValidator v = factory.getValidator();

  ProcessingReport report = v.validate(s, d);

  return report.toString().contains("success");
}
 
Example 3
Source File: OnboardingService.java    From cubeai with Apache License 2.0 4 votes vote down vote up
private Boolean parseMetadata(Solution solution, File dataFile) {
    JSONObject metadataJson;

    try {
        String jsonString = FileUtils.readFileToString(dataFile, "UTF-8");
        // solution.setMetadata(jsonString); // 作废metadata字段,直接从文件中获取

        metadataJson = JSONObject.parseObject(jsonString);

        //==========================================================================================================
        // validate schemaVersion
        String schemafile = null;
        String schemaVersion = metadataJson.get("schema").toString();

        if (schemaVersion.contains("3")) {
            schemafile = "/model-schema/model-schema-0.3.0.json";
        } else if (schemaVersion.contains("4")) {
            schemafile = "/model-schema/model-schema-0.4.0.json";
        } else if (schemaVersion.contains("5")) {
            schemafile = "/model-schema/model-schema-0.5.0.json";
        } else {
            log.error("No matching model schema");
            return false;
        }

        JsonNode schema = JsonLoader.fromResource(schemafile);    // 直接以resources为根目录读取
        JsonNode metadataJson1 = JsonLoader.fromFile(dataFile);
        JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        com.github.fge.jsonschema.main.JsonValidator validator = factory.getValidator();
        ProcessingReport report = validator.validate(schema, metadataJson1);
        if (!report.isSuccess()) {
            StringBuilder sb = new StringBuilder();
            for (ProcessingMessage processingMessage : report) {
                if (!processingMessage.getMessage()
                    .equals("the following keywords are unknown and will be ignored: [self]"))
                    sb.append(processingMessage.getMessage() + "\n");
            }
            log.error("Input JSON is not as per schema cause: '" + sb.toString() + "'");
            return false;
        }
        //==========================================================================================================

        if (metadataJson.containsKey("name")) {
            solution.setName(metadataJson.get("name").toString());
        }

        if (metadataJson.containsKey("modelVersion")) {
            solution.setVersion(metadataJson.get("modelVersion").toString());
        } else {
            solution.setVersion("snapshot");
        }
    } catch (Exception e) {
        return false;
    }

    return true;
}
 
Example 4
Source File: AbstractConfigTest.java    From opensoc-streaming with Apache License 2.0 4 votes vote down vote up
/**
 * validateJsonData
 * @param jsonSchema
 * @param jsonData
 * @return
 * @throws Exception
 */
 
protected boolean validateJsonData(final String jsonSchema, final String jsonData)
    throws Exception {
    
    final JsonNode d = JsonLoader.fromString(jsonData);
    final JsonNode s = JsonLoader.fromString(jsonSchema);
    
    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonValidator v = factory.getValidator();
    
    ProcessingReport report = v.validate(s, d);
    System.out.println(report);
    
    return report.toString().contains("success");
}
 
Example 5
Source File: AbstractSchemaTest.java    From opensoc-streaming with Apache License 2.0 4 votes vote down vote up
/**
 * validateJsonData
 * @param jsonSchema
 * @param jsonData
 * @return
 * @throws Exception
 */
 
protected boolean validateJsonData(final String jsonSchema, final String jsonData)
    throws Exception {
    
    final JsonNode d = JsonLoader.fromString(jsonData);
    final JsonNode s = JsonLoader.fromString(jsonSchema);
    
    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonValidator v = factory.getValidator();
    
    ProcessingReport report = v.validate(s, d);
    System.out.println(report);
    
    return report.toString().contains("success");
}
 
Example 6
Source File: AbstractParserConfigTest.java    From metron with Apache License 2.0 3 votes vote down vote up
protected boolean validateJsonData(final String jsonSchema, final String jsonData)
    throws IOException, ProcessingException {

  final JsonNode d = JsonLoader.fromString(jsonData);
  final JsonNode s = JsonLoader.fromString(jsonSchema);

  final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
  JsonValidator v = factory.getValidator();

  ProcessingReport report = v.validate(s, d);

  return report.toString().contains("success");
}