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

  • Building a Vector Index in Azure AI Search: HNSW, Profiles, and RAG Retrieval
  • Engineering Closed-Loop Graph-RAG Systems, Part 4: Evaluating a Graph-RAG System
  • A Practical Blueprint for Deploying Agentic Solutions
  • Why Stable RAG Answers Can Still Hide Unstable Evidence
  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
14.0K 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

  • 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