Author: Pankaj Badi

  1. First we need the JSON data for creating the body in POST Method.
  2. To check that data is valid or not?
  3. You can check that on this link https://jsonlint.com/.
  4. This link can show you the data is valid or not.
  1. You will get the result.
  1. Take the listener in anypoint studio and give the properties and configuration according to your  convenience.
  1. Now convert the JSON validate data into JSON Schema.
  1. For that you will get link 
  2. https://www.liquid-technologies.com/online-json-to-schema-converter
  3. You will get the data in this form.
  1. In src/main/resources you will find the api folder.
  2. In the api folder just click on that folder right hand side you will get new after that file option you will get.

api -> New -> File

  1.  Just create a file and whatever you generate the schema from JSON just paste that JSON Schema into that file.
  1. In Mule Palette you can Search JSON components or you can  search in Search in Exchange JSON and add into the mule palette.
  2. Take Validate schema component from palette in anypoint studio.
  1. In the validation schema properties.In schema option you just call that JSON Schema which you created in the src/main/resources->api->secure.json.
  1. In secure.json file you can write the schema as well as you can you can add required field or mandatory field.
  1.  In this I did a mandatory field.

“Required” : [

“Document_Type”,

 “No”,

“CompanyName”

]

  1. Even you can do Error Handling 
  2. You can take On-Error Propagate in that you can use Transform Message.
  3. In Transform message properties you can write.
  1.  You will get a proper message if you missed any one of the fields that is required.
  2. If you pass all the body in POST Method with all fields you will get proper response.Success response 200 ( success code).
  1.  Success response 200
  1. If I missed any one of the field

“Required” : [

“Document_Type”,

 “No”,

“CompanyName”

]

In POST Method body.

I missed the “Document_Type”

  1. You will get 500 server Error Response.
  2. You will get the proper message which field you are missing.

Conclusion:

 You can use the validate JSON and check from that JSON you can create JSON schema component from Mule palette.

References:

https://docs.mulesoft.com/

Leave a Comment