0

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.

Prasad
  • 83
  • 1
  • 8

1 Answers1

0

Disclaimer: I contribute to this project.

While this library does not currently support severities, you can fake it with a few changes. First update your schema to preface each message with your desired severity.

"zip_code": {
  "type": "string",
  "minLength": 1,
  "message": {
    "type": "ERROR: Address.zip_code is not a 'String' value",
    "minLength": "ERROR: Address.zip_code is empty"
  }
}

Second, update your code to test for the prefix.

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().startsWith("ERROR: ")) {
       log.error(validationMessage.getDetails().substring("ERROR: ".length()));
    } else {
       log.warn(validationMessage.getDetails());
    }
});
Faron
  • 1,354
  • 10
  • 23
  • Thank you for the reply, this is what I am currently using as a workaround. By the way, just curious why the setMessage method is not public in ValidationMessage class. – Prasad May 12 '23 at 04:06
  • What do you think if reflection is used to do that? – Prasad May 17 '23 at 03:55