问题
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
EDIT: JSON validator can be downloaded from exchange. search for JSON MODULE
回答1:
i did a small sample to demonstrate how json validator works. you can use the same
- go to https://jsonschema.net/ or any online json schema generator and generate json schema for your json file.
- same this schema into a file with .json extension and place it in src/main/resources folder.
- 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.
here are some of the responses with different json inputs against my flow
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>
来源:https://stackoverflow.com/questions/59318278/validation-of-payload-in-esb