2

I have a HTTP POST REQUEST which sends a payload as follows:

{ "key1" : "value1", "key2" : "value2" ,"key3": "value3" }

I was able to validate all the values but I had to use a component everytime. In this case , I used the validator "Not a blank string" 3 times.

1. Is there any way that I can validate all the three values in a single validator ? 
 2. Should I use Scatter-Gather for validating all the values in parallel(according to best-practices) ?
 3. How can I validate the key set(the key set should only contain "key1", "key2" and "key3"  ?

Update: I have fetched the key set by using the connector setVariable enter image description here

EDIT: JSON validator can be downloaded from exchange. search for JSON MODULE

HMT
  • 2,093
  • 1
  • 19
  • 51

1 Answers1

3

i did a small sample to demonstrate how json validator works. you can use the same

  1. go to https://jsonschema.net/ or any online json schema generator and generate json schema for your json file.
  2. same this schema into a file with .json extension and place it in src/main/resources folder.
  3. implement the schema validate and refer to your schema and your incoming json will get validated against the schema automatically so you dont have to use any of the scatter-gather or each individual key-value pair validations. enter image description here

here are some of the responses with different json inputs against my flow

  1. enter image description here

  2. enter image description here

  3. enter image description here

you can always enhance your schema to suite your needs. you can define data type for each key. lets say key3 should contain only numeric values, key2 should be an array. anything like that all can be achieved by modifying your schema and the validator will validate the incoming json accordingly.

update: here is the xml content of the mule flow

<flow name="stackoverflowFlow1" doc:id="c24d34aa-ce1d-4bbb-b3fd-d73007dad60b" >
        <http:listener doc:name="Listener" doc:id="80ab5db5-1d3e-409f-9990-cebf1fc07dd5" config-ref="HTTP_Listener_config" path="/valid"/>
        <json:validate-schema doc:name="Validate schema" doc:id="0a51cde0-5175-4f11-b972-3d5d708094c6" schema="schema.json"/>
        <logger level="INFO" doc:name="Logger" doc:id="2d95a6f0-8f88-4608-bdb8-7fae8abc0e6d" message="valid json"/>
    </flow>
satish chennupati
  • 2,602
  • 1
  • 18
  • 27
  • thanks for answering my question. In my anypoint studio(version -- 7.3) , I can only see the xml validator. In which version of anypoint studio will I find the json validator ? – HMT Dec 16 '19 at 07:56
  • If possible, can you please share the xml generated behind this flow – HMT Dec 16 '19 at 07:56
  • 1
    i am using studio version 7.4 but i have used this feature with older version too like 6.6. in pallet search for schema validator.. i can share the xml. sure. i will attach it to my answer. – satish chennupati Dec 16 '19 at 13:20
  • I had to download it from Exchange. I found it in the JSON MODULE.Thanks a lot for your help :) – HMT Dec 17 '19 at 05:04
  • Can we provide custom error messages for each key ? for example : "key1 is required" instead of giving a generic message that the json is not compliant with the schema – HMT Dec 19 '19 at 04:43
  • #[error.errorMessage.payload] – HMT Dec 19 '19 at 06:51
  • I tried the above approach , but it doesn't give a user friendly output. – HMT Dec 19 '19 at 06:52