use of com.github.fge.jsonschema.core.report.ListProcessingReport in project gravitee-management-rest-api by gravitee-io.
the class PolicyServiceImpl method validatePolicyConfiguration.
private String validatePolicyConfiguration(String policyName, String configuration) {
if (policyName != null && configuration != null) {
String schema = getSchema(policyName);
try {
// At least, validate json.
String safePolicyConfiguration = clearNullValues(configuration);
JsonNode jsonConfiguration = JsonLoader.fromString(safePolicyConfiguration);
if (schema != null && !schema.equals("")) {
// Validate json against schema when defined.
JsonNode jsonSchema = JsonLoader.fromString(schema);
ListProcessingReport report = (ListProcessingReport) jsonSchemaFactory.getValidator().validate(jsonSchema, jsonConfiguration, true);
if (!report.isSuccess()) {
boolean hasDefaultValue = false;
String msg = "";
if (report.iterator().hasNext()) {
msg = " : " + report.iterator().next().getMessage();
Pattern pattern = Pattern.compile("\\(\\[\\\"(.*?)\\\"\\]\\)");
Matcher matcher = pattern.matcher(msg);
if (matcher.find()) {
String field = matcher.group(1);
JsonNode properties = jsonSchema.get("properties");
hasDefaultValue = properties != null && properties.get(field) != null && properties.get(field).get("default") != null;
}
}
if (!hasDefaultValue) {
throw new InvalidDataException("Invalid policy configuration" + msg);
}
}
}
return safePolicyConfiguration;
} catch (IOException | ProcessingException e) {
throw new InvalidDataException("Unable to validate policy configuration", e);
}
}
return configuration;
}
Aggregations