Schema Validation In Mule 4
In this article, see what a schema validation in Mule 4 looks like.
Join the DZone community and get the full member experience.
Join For FreeWhat 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
xxxxxxxxxx
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"></xs:element>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="address" type="xs:string"></xs:element>
<xs:element name="city" type="xs:string"></xs:element>
<xs:element name="country" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"></xs:element>
<xs:element name="note" type="xs:string" minOccurs="0"></xs:element>
<xs:element name="quantity" type="xs:positiveInteger"></xs:element>
<xs:element name="price" type="xs:decimal"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"></xs:attribute>
</xs:complexType>
</xs:element>
</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:
xxxxxxxxxx
<shiporder orderid="????">
<orderperson>????</orderperson>
<shipto>
<name>????</name>
<address>????</address>
<city>????</city>
<country>????</country>
</shipto>
<item>
<title>????</title>
<note>????</note>
<quantity>2</quantity>
<price>2</price>
</item>
</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:
<mule xmlns:xml-module="http://www.mulesoft.org/schema/mule/xml-module" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
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: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
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/xml-module http://www.mulesoft.org/schema/mule/xml-module/current/mule-xml-module.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="dbbb34bf-6117-4011-b60c-aa75d5dcd7c3" >
<http:listener-connection host="0.0.0.0" port="8081" ></http:listener>
</http:listener-config>
<xml-module:config name="XML_Config" doc:name="XML Config" doc:id="51e521e5-6605-4582-9e7b-5dee2c3e4935" ></xml>
<flow name="schemaFlow" doc:id="7af00482-3d87-4845-9e78-d7eaed93ff45" >
<http:listener doc:name="Listener" doc:id="74a745e9-9b9e-4206-898c-f25767bad654" config-ref="HTTP_Listener_config" path="/schema"></http:listener>
<xml-module:validate-schema doc:name="Validate schema" doc:id="de9f9219-5637-428e-b2e1-a3f112473a54" config-ref="XML_Config" schemas="schemas\schema.xsd"></xml>
<logger level="INFO" doc:name="Logger" doc:id="e14bf687-9c23-44af-8805-887bbec127b3" message="#[payload]"></logger>
</flow>
</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
Opinions expressed by DZone contributors are their own.
Comments