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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Understanding the Fan-Out/Fan-In API Integration Pattern
  • Mule 4: Dynamically Call Multiple Subflows With One Flow-Ref
  • Validate XML Request Against XML Schema in Mule 4
  • What D'Hack Is DPoP?

Trending

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • A Modern Stack for Building Scalable Systems
  • Performance Optimization Techniques for Snowflake on AWS
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Validation Module in Mule

Validation Module in Mule

Learn how to use the Validation module in Mule to verify that the content of a message in your flow matches a given set of criteria.

By 
Ankit Lawaniya user avatar
Ankit Lawaniya
·
Aug. 21, 17 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
38.6K Views

Join the DZone community and get the full member experience.

Join For Free

The Validation module provides an easy out-of-the-box way to verify that the content of a message in your flow matches a given set of criteria.

The Difference Between Mule's Filter Component and Validation Module

The main advantage of the Validation Module over using filters is traceability, as filters all raise identical exceptions, making it hard for us to know where the exception was caused. So if we have two filters in the same flow, we cannot know which one failed and we cannot customize the exception type. Validators, on the other hand, raise an exception with a meaningful message attached, so we can customize the exception type.

Note: This feature is supported by the Mule run-time as of Mule ESB 3.7.0 in both Community and Enterprise editions.

The validations module was designed following these principles:

  • If the message doesn’t meet the specified criteria, the validation fails and a ValidationException is thrown.

  • By default, this exception has a meaningful message attached. You can optionally customize this message and change the type of exception thrown, as long as the new exception type has a constructor that overrides Exception(String).

  • In case you want to throw an Exception type that lacks this constructor, or in which its creation is not trivial, or in which you want it to contain additional information.

How to Use Validator

There are two ways of using a validator: through a Message Processor, or through a MEL function.

Types of Built-In Validator

Below is not the complete list of Validators; please refer to the MuleSoft article on Validation components for the complete list.

Email Address Validator

<validation:is-email email=”#[json:email]” />

Regular Expression Validator

<validation:matches-regex value=”#[json:Name]” regex=”/^[A-z]+$/” message=”Name can not contain Integer value” />

Not Empty check Validator

<validation:is-not-empty value=”#[json:employeeId]” message=”Employee Id is mandatory to supply” />

Size Validator

<validation:validate-size value=”#[json:age]” min=”2″ max=”3″ message=”Please provide correct age” />

Not Null Validator

<validation:not-null expression=”#[value]” value=”#[payload]” />

Number Validator

<validation:is-number value=”#[json:Id]” numberType=”INTEGER” message=”ID can not be String or any other data Type” />

How to Validate Many Conditions at Once

There are scenarios in which you may want to evaluate several conditions, out of which more than one could fail simultaneously. In these cases, it’s ideal to generate a single error that contains all of the descriptions.

The solution is the All Validator.

Let’s walk through how to use All Validator in a Mule application. In this example, we are receiving the JSON Request through an HTTP call from the REST client, which will be validated against the ALL Validator Component.

Mule Flow:

Image title

Code:

<?xml version=”1.0″ encoding=”UTF-8″?>
<mule xmlns:json=”http://www.mulesoft.org/schema/mule/json&#8221;
xmlns:http=”http://www.mulesoft.org/schema/mule/http&#8221; xmlns:validation=”http://www.mulesoft.org/schema/mule/validation&#8221;
xmlns=”http://www.mulesoft.org/schema/mule/core&#8221; xmlns:doc=”http://www.mulesoft.org/schema/mule/documentation&#8221;
xmlns:spring=”http://www.springframework.org/schema/beans&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
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.mulesoft.org/schema/mule/validation http://www.mulesoft.org/schema/mule/validation/current/mule-validation.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”&gt;
<http:listener-config name=”HTTP_Listener_Configuration”
host=”localhost” port=”8081″ basePath=”/validation” doc:name=”HTTP Listener Configuration” />
<validation:config name=”Validation_Configuration”
doc:name=”Validation Configuration” />
<flow name=”ValidationComponent”>
<http:listener config-ref=”HTTP_Listener_Configuration”
path=”/validation” doc:name=”HTTP” />
<byte-array-to-string-transformer
doc:name=”Byte Array to String” />
<validation:all config-ref=”Validation_Configuration”
doc:name=”Validation”>
<validation:validations>
<validation:is-number value=”#[json:id]”
numberType=”INTEGER” message=”ID can not be String data Type” />
<validation:is-not-empty value=”#[json:account]”
message=”Account Number Required” />
<validation:matches-regex value=”#[json:name]”
regex=”^[a-zA-Z]+$” message=”Name can not contain Integer value and other special charectors” />
<validation:validate-size value=”#[json:age]”
min=”2″ max=”3″ message=”Please provide correct age” />
<validation:is-email email=”#[json:email]” />
</validation:validations>
</validation:all>
<logger level=”INFO” message=”Validation Successful” doc:name=”Logger”></logger>
</flow>
</mule>


Success Request:

{
“id”: 123,
“account”: 1234,
“name”:”Ankit”,
“age”: 24 ,
“email” :”ankit.lawaniya@live.com”
}

Image title

Failed Request:

{ 
 "id": "dummyId",
 "account": 12,
 "name":"Ankit1", 
 "age": 2 ,
 "email" :"ankit.lawaniyalive.com"
 }

Image title

Below is the exception thrown for the validation failure:

Root Exception stack trace:
org.mule.extension.validation.api.MultipleValidationException: ID can not be String data Type
Name can not contain Integer value and other special charectors
Please provide correct age
ankit.lawaniyalive.com is not a valid email address
at org.mule.extension.validation.internal.ValidationStrategies.all(ValidationStrategies.java:71)

Hope this helps.

Thanks. Keep learning.

Filter (software) Requests Flow (web browser) application MULE Advantage (cryptography) JSON IT Web Protocols

Opinions expressed by DZone contributors are their own.

Related

  • Understanding the Fan-Out/Fan-In API Integration Pattern
  • Mule 4: Dynamically Call Multiple Subflows With One Flow-Ref
  • Validate XML Request Against XML Schema in Mule 4
  • What D'Hack Is DPoP?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!