java-json-tools / json-schema-validator

A JSON Schema validation implementation in pure Java, which aims for correctness and performance, in that order
http://json-schema-validator.herokuapp.com/
Other
1.63k stars 399 forks source link

How can we get Error Message with the error path appended? #409

Open AnkitJha-OMO30 opened 1 year ago

AnkitJha-OMO30 commented 1 year ago

I am trying to get a custom error message. Below I have mentioned the schema I have used. I am getting the Error Response as mentioned in my message, but the path is not present. I am attaching the error messages with and without the message block.

My Intended format for message has the path of error along with the custom message.

Example Schema:

{
    "$id": "https://saisonomni.com/loanapplication.schema.json",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
        "additionalProperties": false,
        "properties": {
            "details": {
                "type": "object",
                "properties": {
                    "phone": {
                        "type": "string",
                        "pattern": "\\+[0-9]{1,3} [0-9]{10}",
                        "message" : {
                                 "pattern": "Phone Number not in right format. Correct format: +91 9876543210"
                        }
                    }
                }
            }
        }
}

Error with the message block:

{
    "errors": [
        "Phone Number not in right format. Correct format: +91 9876543210"
    ]
}

Error without the message block:

{
    "errors": [
        "$.details.phone: does not match the regex pattern \\+[0-9]{1,3} [0-9]{10}"
    ]
}

Error message that is expected

{
    "errors": [
        "$.details.phone: Phone Number not in right format. Correct format: +91 9876543210"
    ]
}

Is there a way to achieve the above error messaging?