DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Custom Attributes in Relational Databases
  • Exploring JSON Schema for Form Validation in Web Components
  • Datafaker Gen: Leveraging BigQuery Sink on Google Cloud Platform
  • Validate XML Request Against XML Schema in Mule 4

Trending

  • Architecting Sub-Microsecond HFT Systems With C++ and Zero-Copy IPC
  • Integrating AI-Driven Decision-Making in Agile Frameworks: A Deep Dive into Real-World Applications and Challenges
  • The Death of "Text-Only" ChatOps: Why Google's A2UI Matters for DevOps and SRE
  • How to Prevent Data Loss in C#
  1. DZone
  2. Coding
  3. Languages
  4. Dynamic JSON Schema Validation - Mule4

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.

By 
Ankur Bhuyan user avatar
Ankur Bhuyan
·
Oct. 11, 21 · Code Snippet
Likes (4)
Comment
Save
Tweet
Share
16.0K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

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
YAML
 
#%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:

Renaming and storing JSON schema

Step 3

  • Define the acceptable countries in the property file in an array format:
YAML
 
#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):
Java
 
%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):
Java
 
%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.

Postman responses


  • Error Scenario 1: If the input request payload is not properly defined, means it is not matching with schema requirement. 

Error scenario


  • Error Scenario 2: If the input request payload has an empty country field.

Error scenario 2


  • Error Scenario 3: If the input request payload has a country that is not valid (or not acceptable by the API).

Error scenario 3


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.
JSON Schema

Opinions expressed by DZone contributors are their own.

Related

  • Custom Attributes in Relational Databases
  • Exploring JSON Schema for Form Validation in Web Components
  • Datafaker Gen: Leveraging BigQuery Sink on Google Cloud Platform
  • Validate XML Request Against XML Schema in Mule 4

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook