1

I am using APIkit in Mule with RAML 0.8 and a JSON schema, as follows (example):

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    
    "type": "object",
    
    "properties": {
        "cart": {
            "title": "",
            "description": "",
            "type": "object",
            "properties": {
                "internalNumber": {
                    "type": "integer"
                }
            },
            "required": [
                "internalNumber"
            ]
        }
    },
    "required": [
        "cart"
    ]
}

and in the Mule Flow, I catch the exception and show the following result:

#[exception.cause.message]

When a validation error occurs, I want to get the name of the field in which the validation failed. Instead, this is what I got:

Input

{
    "cart": {
        "internalNumber": "I must be an integer"
    }
}

Output

"instance type (string) does not match any allowed primitive type (allowed: ["integer"])"

Expected output

{
    "field": "cart.internalNumber",
    "error": "instance type (string) does not match any allowed primitive type (allowed: ["integer"])"
}

All I want to know is if there is a way to get the name of the field in which the validation errors occurs.

Regarding the Mule Documentation, I can get the whole JSON string but not the name of the failing field...

I hope someone can give me a better solution.

Thanks!

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
xnacho
  • 51
  • 1
  • 6

2 Answers2

0

Within your JSON Schema, add "required":"true" attribute, to make the fields mandatory.

You can also use JSON schema validator, in your mule flow, by referring to the updated schema.

Any of the case should through you an error with missing field. Use below expression to get expected error message. { "errorMessage": "#[exception].toString().replace("\"","\\\"")" }

  • To get the JSON element, Set a flowVar "err"as #[exception.toString()], use expression flwVars.err.substring(flwVars.err.indexOf('\"'),flwVars.err.indexOf(']')) – Yeshwanth Kumar Jul 31 '16 at 03:40
  • The required keyword takes an array of one or more strings. Each of these strings must be unique. So the correct sintax would be `"required": ["internalNumber"]` at the same level of properties field. Anyway, I already had this and still didn't work. Thanks – xnacho Aug 01 '16 at 12:39
-3

Not sure if you are expecting it as an output or looking for a way to validate your input and schema. I can try to suggest on "All I want to know is if there is a way to get the name of the field in wich the validation errors occurs."; to do this better validate your JSON and input data through online validator before defining definitions. Like using http://www.jsonschemavalidator.net/, it will help you with error and fields. Hope this may help!

SahuG
  • 26
  • 2