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

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

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • Scalability 101: How to Build, Measure, and Improve It
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Start Coding With Google Cloud Workstations
  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  1. DZone
  2. Coding
  3. Languages
  4. Validate JSON Schema Components in Mule

Validate JSON Schema Components in Mule

Learn how to use the JSON Schema validator to validate payloads against a schema in a Mule application, with code included.

By 
Ankit Lawaniya user avatar
Ankit Lawaniya
·
Aug. 06, 17 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
13.8K Views

Join the DZone community and get the full member experience.

Join For Free

The JSON Schema validator evaluates JSON payloads at runtime and verifies that they match a referenced JSON schema. We can match against schemas that exist in a local file or in an external URI.

If the payload is not compliant with JSON schema, then we will get this Exception: org.mule.module.json.validation.JsonSchemaValidationException: Json content is not compliant with schema.

Use Case

This feature is especially useful when we are consuming a service that expects JSON inputs and that must match a specific schema. Only the JSON payload which is successfully validated against the schema will pass through otherwise JSON schema validation exception will be raised.

Syntax:

<json:validate-schema schemaLocation="EmployeeSchema.json" doc:name="Validate Json Schema" />


Let’s walk through how to use the JSON Schema validator in a Mule application. Using the JSON Schema Validator is very simple: we only need to provide a reference to the schema file you want to validate against on schemaLocation. The Schema Validator accepts both local and external resources. In this example, we have "EmployeeSchema.json" located in the src/main/resources folder. We are expecting the JSON payload as part of the request which will be validated against the schema.

Mule Flow:

Image title

Code:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.8.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd 
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">

<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" basePath="/json" doc:name="HTTP Listener Configuration" />
    <flow name="myFirstSchemaValidator">  
        <http:listener config-ref="HTTP_Listener_Configuration" doc:name="HTTP" path="/json"/>
        <json:validate-schema schemaLocation="EmployeeSchema.json" doc:name="Validate Json Schema" />
       <logger message="Schema Validated successfully" level="INFO"/>
    </flow>
</mule>

Now, to execute this flow, we will have to open the REST client and pass the payload as part of the POST request body. Below are the success and failed requests for the flow.

Success Request:

{
    "firstName": "Ankit",
    "lastName": "Lawaniya",
    "age": 28
}

Image title


Failed Request:

{
    "firstName": "Ankit",
    "age": 28
}

Image title

Hope this helps.

Thanks.

Keep learning.

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!