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

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.


<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


<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
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"
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"/>

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.


Keep learning.

json, mule, mule esb, mulesoft

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}