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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • File Upload Security and Malware Protection
  • Logging Best Practices Revisited [Video]
  • Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Why You Should Consider Using React Router V6: An Overview of Changes

Trending

  • File Upload Security and Malware Protection
  • Logging Best Practices Revisited [Video]
  • Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Why You Should Consider Using React Router V6: An Overview of Changes

Schema Validation In Mule 4

In this article, see what a schema validation in Mule 4 looks like.

srinivasarao potnuru user avatar by
srinivasarao potnuru
·
Updated Jan. 14, 20 · Tutorial
Like (3)
Save
Tweet
Share
22.59K Views

Join the DZone community and get the full member experience.

Join For Free

Close up of a mule's eye

What Is a Schema Validation Filter?

The Schema Validation component takes XML inputs and approves these against a referenced XSD pattern.

Ordinarily, you can put this component before another message processor, for example, a connector, to decide if the approaching message or occasion ought to be dealt with or not, enabling the message to possibly proceed along the stream when the channel approvals are met and the included XML is viewed as legitimate.

Basic Configuration: 

  • Path to XSD schema location.
  • The Schema Validation Filter utilizes the JAXP libraries to approve a message against a pattern. You should give the way, record name, and expansion of the outline or constructions in the Schema Locations property. 
  • Alternatively, you can set Schema Language to the pattern language to utilize. If not indicated, the property defaults to XML Schema. 
  • For the situation you need to get an exemption or potentially call another stream when the approval comes up short, you could wrap the construction approval channel inside a message channel and afterward use "on Unaccepted" and "throwOnUnaccepted" setup ascribes to determinate the conduct.
You might also be interested in:  Mule 4: JSON Schema Validation

Example of Schema Validation in Mule 4

See how to validate an XML payload using the Schema validation component.

Please follow the below steps:

Step 1:

Create a new Mule Project as XML_Schema_Validation    

Step 2:

Intuitive an HTTP listener connector to get a solicitation for your servers and give the host and port numbers. Here, the host is set to localhost and port to 8081. Test your association and check if the port is being used or not and test it effectively.



Step 3:

Set the path as  /schema.

Step 4:

Drag the schema validator Component

  

Step 5:

Select the schema language and add module configuration also.


NOTE: Example for X12 Schema:


The schema is in the form of (file.xsd) extension

XML
xxxxxxxxxx
1
31
 
1
<?xml version="1.0" encoding="UTF-8" ?>
2
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3
<xs:element name="shiporder">
4
   <xs:complexType>
5
     <xs:sequence>
6
       <xs:element name="orderperson" type="xs:string"></xs:element>
7
       <xs:element name="shipto">
8
         <xs:complexType>
9
           <xs:sequence>
10
             <xs:element name="name" type="xs:string"></xs:element>
11
             <xs:element name="address" type="xs:string"></xs:element>
12
             <xs:element name="city" type="xs:string"></xs:element>
13
             <xs:element name="country" type="xs:string"></xs:element>
14
           </xs:sequence>
15
         </xs:complexType>
16
       </xs:element>
17
       <xs:element name="item" maxOccurs="unbounded">
18
         <xs:complexType>
19
           <xs:sequence>
20
             <xs:element name="title" type="xs:string"></xs:element>
21
             <xs:element name="note" type="xs:string" minOccurs="0"></xs:element>
22
             <xs:element name="quantity" type="xs:positiveInteger"></xs:element>
23
             <xs:element name="price" type="xs:decimal"></xs:element>
24
           </xs:sequence>
25
         </xs:complexType>
26
       </xs:element>
27
     </xs:sequence>
28
     <xs:attribute name="orderid" type="xs:string" use="required"></xs:attribute>
29
   </xs:complexType>
30
</xs:element>
31
</xs:schema>


Step 6:

Drag the logger component and check the payload and whether the validation is successful or not.                                

Step 7:

Run the project

Step 8:

Open the postman and hit your service URL request as shown.  http://localhost:8081/schema

Input payload:

XML
xxxxxxxxxx
1
16
 
1
<?xml version='1.0' encoding='UTF-8'?>
2
<shiporder orderid="????">
3
  <orderperson>????</orderperson>
4
  <shipto>
5
    <name>????</name>
6
    <address>????</address>
7
    <city>????</city>
8
    <country>????</country>
9
  </shipto>
10
  <item>
11
    <title>????</title>
12
    <note>????</note>
13
    <quantity>2</quantity>
14
    <price>2</price>
15
  </item>
16
</shiporder>




Step 9:

Our Output Payload as 

Now, we look about the entire flow of a Mule project i.e XML schema validation. 

NOTE: Entire code of the example:

XML
x
18
 
1
<?xml version="1.0" encoding="UTF-8"?>
2
3
<mule xmlns:xml-module="http://www.mulesoft.org/schema/mule/xml-module" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
4
    xmlns:http="http://www.mulesoft.org/schema/mule/http"
5
    xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
6
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
7
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
8
http://www.mulesoft.org/schema/mule/xml-module http://www.mulesoft.org/schema/mule/xml-module/current/mule-xml-module.xsd">
9
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="dbbb34bf-6117-4011-b60c-aa75d5dcd7c3" >
10
        <http:listener-connection host="0.0.0.0" port="8081" ></http:listener>
11
    </http:listener-config>
12
    <xml-module:config name="XML_Config" doc:name="XML Config" doc:id="51e521e5-6605-4582-9e7b-5dee2c3e4935" ></xml>
13
    <flow name="schemaFlow" doc:id="7af00482-3d87-4845-9e78-d7eaed93ff45" >
14
        <http:listener doc:name="Listener" doc:id="74a745e9-9b9e-4206-898c-f25767bad654" config-ref="HTTP_Listener_config" path="/schema"></http:listener>
15
        <xml-module:validate-schema doc:name="Validate schema" doc:id="de9f9219-5637-428e-b2e1-a3f112473a54" config-ref="XML_Config" schemas="schemas\schema.xsd"></xml>
16
        <logger level="INFO" doc:name="Logger" doc:id="e14bf687-9c23-44af-8805-887bbec127b3" message="#[payload]"></logger>
17
    </flow>
18
</mule>


Conclusion

Mule 4 accompanies several upgrades and improvements. Validate schema component in Mule 3 was different compared to Mule 4, and it has been rearranged for developers. In this way, it is simple to execute and gives some additional highlights.

Further Reading

Validate JSON Schema Components in Mule

How to Use Validate JSON Schema and Message Enricher in Mule

Schema

Opinions expressed by DZone contributors are their own.

Trending

  • File Upload Security and Malware Protection
  • Logging Best Practices Revisited [Video]
  • Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Why You Should Consider Using React Router V6: An Overview of Changes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: