Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Validate JSON Schema Components in Mule

DZone's Guide to

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.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

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.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
mule ,mulesoft ,mule esb ,json

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}