Dynamic JSON Schema Validation - Mule4
Learn how dynamic JSON schema validation can help to re-use the old JSON Schema to validate the incoming payload in the Mulesoft project without RAML schema failure.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
In some cases, the RAML schema definition can't be possible. Like if the project wants to re-use the old JSON Schema to validate the incoming payload in the Mulesoft project (to migrate from other ESB). Also, if the single API endpoint is going to deploy for many countries and for each country we have a separate JSON Schema.
In such requirements we can use Dynamic JSON Schema validation, which means based on the country (present in input payload), it will load the country-specific JSON Schema and perform validation of incoming payload. Additionally, if the project wants to add/remove/update the schema frequently, we don't have to change the validation code. Just adding/replacing the JSON schema in a specific format in the proper directory and append the newly added country in the property file (if new addition) will be enough.
POC
Step 1
- Define the RAML based on your requirement
#%RAML 1.0
title: dynamic-schema-validation
description: This API is to give example how we can use country based Json Schema for validation
version: v0
/userRegister:
description: This endpoint to store the Data of user in Database
post:
responses:
200:
body:
application/json:
Step 2
- Rename the JSON schema and store it in a dedicated directory as like this:
Step 3
- Define the acceptable countries in the property file in an array format:
#HTTP API Properties
http:
private:
port: "8091"
host: "0.0.0.0"
path: "/api/*"
#reconenct frequency
reconnect:
frequency: "10000"
attempts: "2"
#Country list that will support by the API
supported_countries : ['IN','US']
Step 4
- Write DataWeave logic to validate the contains check of the incoming country field with expected countries (array):
%dw 2.0
output application/java
---
if ((p('supported_countries') contains(payload.country))) true else false
Step 5
- Write the DataWeave logic to prepare the schema name based on the input payload (country):
%dw 2.0
import * from dw::core::Strings
output application/java
---
("/schemas/" ++ payload.country ++ "_" ++ substringAfterLast(attributes.relativePath, '/') ++ "_request.json")
Step 6
- Follow the complete code and do the HAPPY and ERROR scenarios test. Find the postman script for the below tests here.
Postman Responses
- Happy Scenario: If we pass a valid payload, means the country in the payload is acceptable and the other fields are properly defined as per schema requirement.
- Error Scenario 1: If the input request payload is not properly defined, means it is not matching with schema requirement.
- Error Scenario 2: If the input request payload has an empty country field.
- Error Scenario 3: If the input request payload has a country that is not valid (or not acceptable by the API).
Conclusion
- This POC is to give an idea about the usage of Dynamic Schema Validation and I am sure this can be enhanced based on the requirement.
Opinions expressed by DZone contributors are their own.
Comments