I'm using json-schema to validate a JSON payload with the help of networknt json-schema-validator. Now I need to set a severity level like "warning" or "error" to each attribute, and based on its value I'm deciding how to print that validation message, for example, log.error
or log.warn
. For that, I need to get that value through Java code when/after it is validated.
I checked a way to parse some additional details to the json-schema
along with other fields(type, message, ..) but couldn't find anything.
Also, I'm wondering about the method in com.networknt.schema.ValidationMessage#getDetails()
, I'm assuming that there is a way to parse extra information through the json-schema
and that information can be retrieved through that method.
If I explain my expectation through a code, it is like this,
"zip_code": {
"type": "string",
"minLength": 1,
"severity": "Error",
"message": {
"type": "Address.zip_code is not a 'String' value",
"minLength": "Address.zip_code is empty"
}
}
"severity" is the additional attribute that holds the information I need
InputStream inputStream = ExampleClass.class.getClassLoader().getResourceAsStream(schema);
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);
JsonSchema jsonSchema = factory.getSchema(inputStream);
Set<ValidationMessage> errors = jsonSchema.validate(event);
errors.forEach(validationMessage -> {
if(validationMessage.getDetails().get("severity") == "Error") {
log.error(validationMessage);
} else {
log.warn(validationMessage);
}
});
Maybe, the above way might not be possible, but anyway I'm expective some guidance here on achieving this. TIA.