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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • 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
  • Validate JSON Request Against JSON Schema in Mule 4

Trending

  • Secrets Sprawl and AI: Why Your Non-Human Identities Need Attention Before You Deploy That LLM
  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services
  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
15.5K 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

  • 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
  • Validate JSON Request Against JSON Schema in Mule 4

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!