I am trying to validate JSon using the json schema validator. But it returns a generic message. "Json content is not compliant with schema" . I have a HTTP POST REQUEST which sends a payload as follows:
{ "key1" : "value1", "key2" : "value2" ,"key3": "value3" }
if key1 and key2 is missing . I want it to give error messages as follows:
{{
errorCode :1001,
errorMessage : key1 is missing",
},
errorCode :1002,
errorMessage : key2 is missing"
}
}
I tried writing the errors to a file(json file containing all the warnings and messages} looks something like this:
{
"level" : "error",
"domain" : "validation",
"keyword" : "required",
"message" : "object has missing required properties ([\"key1\",\"key2\",\"key3\"])",
"required" : [ "key1", "key2", "key3"],
"missing" : [ "key1", "key2"]
}
This is just a small part of this file. I'll have to loop through the file to reach this information. Is there any other way , I can perform custom validations and return proper error messages to the user.
EDIT 1:
I have created the following RequestObj class:
public class RequestObj {
@Valid
@NotBlank
@NotNull
private String key1;
@Valid
@NotBlank
private String key2;
@Valid
@NotBlank
private String key3;
@Override
public String toString() {
return "RequestObj [key1=" + key1 + ", key2=" + key2 + ", key3=" + key3 + "]";
}
It is not validating key1 as not null.
postman request :
POST /validate HTTP/1.1 Host: localhost:8081 Content-Type: application/json
{ "key2" :"gg", "key3" : "hh" }
when I implement the validator interface. I dont get access to the mule Event. How will I access the json that I need to validate in this case ?