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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Solving Parallel Writing Issues in MuleSoft With Distributed Locking
  • Optimizing MuleSoft Performance With HikariCP: A Complete Guide
  • Generic and Dynamic API: MuleSoft

Trending

  • When Airflow Tasks Get Stuck in Queued: A Real-World Debugging Story
  • How to Introduce a New API Quickly Using Micronaut
  • Useful System Table Queries in Relational Databases
  • Is Big Data Dying?
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. What Are Anypoint Validations With Mulesoft?

What Are Anypoint Validations With Mulesoft?

Anypoint validations alert you if a message doesn't meet specific criteria. There's even a way to build your own custom validator to match your requirements.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Mar. 05, 17 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
18.5K Views

Join the DZone community and get the full member experience.

Join For Free

Validations are an out-of-the-box feature available with Mulesoft to verify the content of the message in the Mule flow (i.e., validating the date format, email address, URL, IP, etc.). The advantage of validations over the filters is traceability. Filter raises the identical exceptions, which makes it difficult to understand where the exception was raised, whereas validations provide exceptions with some meaningful message. It's easier to understand.

  • If a message doesn't meet specific criteria, the validation fails and ValidationException will be raised.

  • The exception raised has a meaningful message attached. You can optionally customize the message as well as the exception raised.

  • There is a facility to build your own custom validator if out-of-the-box validations don't match your requirements.

The validator can be used as a Message Processor as well as MEL (Mule Expression Language).

<flow name="melValidate">
  <choice>
    <when expression="#[validator.validateEmail(payload.email)]">
      <flow-ref name="sendEmail" />
    </when>
    <otherwise>
      <logger message="Email address is not in correct format." />
    </otherwise>
  </choice>
</flow>

Validate Email Address

It will check if the email address is valid.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns:validation="http://www.mulesoft.org/schema/mule/validation"
	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"
	xmlns:xsi="http://wwwa.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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://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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
	<flow name="validationflowFlow">
		<http:listener config-ref="HTTP_Listener_Configuration" path="/validate" allowedMethods="POST" doc:name="HTTP"/>
		<validation:is-email message="Email Address is not in correct format" email="#[payload]" doc:name="Validation"/>
		<logger level="INFO" doc:name="Logger" message="Email Address is valid"/>
	</flow>
</mule>

Validate URL

This checks if the URL is valid. 

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns:validation="http://www.mulesoft.org/schema/mule/validation"
	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"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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://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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
	<flow name="validationflowFlow">
		<http:listener config-ref="HTTP_Listener_Configuration" path="/validate" allowedMethods="POST" doc:name="HTTP"/>
		<validation:is-url message="URL is not in correct format"  doc:name="Validation" url="#[payload]"/>
		<logger level="INFO" doc:name="Logger" message="URL is valid"/>
	</flow>
</mule>

Validate Regular Expression

It checks the content of a message on a defined pattern (i.e., ^[1-9]\d*$).

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns:validation="http://www.mulesoft.org/schema/mule/validation"
	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"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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://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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
	<flow name="valixdationflowFlow">
		<http:listener config-ref="HTTP_Listener_Configuration" path="/validate" allowedMethods="POST" doc:name="HTTP"/>
		<validation:matches-regex message="Message doesn't match pattern. It is expecting number."  doc:name="Validation" regex="^[1-9]\d*$" value="#[payload]"/>
		<logger level="INFO" doc:name="Logger" message="Message is valid"/>
	</flow>
</mule>

Validate Size

This is used validate the input message size between min and max boundaries. It's valid for inputs like Map, Array, String, and Collections.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns:validation="http://www.mulesoft.org/schema/mule/validation"
	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"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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://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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
	<flow name="validationflowFlow">
		<http:listener config-ref="HTTP_Listener_Configuration" path="/validate" allowedMethods="POST" doc:name="HTTP"/>
		<validation:validate-size message="Message size should be in defined range"  doc:name="Validation"  value="#[payload]" max="#[maxLenght]" min="#[minLenght]"/>
		<logger level="INFO" doc:name="Logger" message="Valid Message"/>
	</flow>
</mule>

Validate IP

It is used to check the IP is valid or not (i.e., 10.10.131.122).

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns:validation="http://www.mulesoft.org/schema/mule/validation"
	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"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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://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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
	<flow name="validationflowFlow">
		<http:listener config-ref="HTTP_Listener_Configuration" path="/validate" allowedMethods="POST" doc:name="HTTP"/>
		<validation:is-ip message="IP address is not correct"  doc:name="Validation" ip="#[payload]"/>
		<logger level="INFO" doc:name="Logger" message="Valid Message"/>
	</flow>
</mule>

Validate String, Collection, and Map Are Not Empty

In the case of String, "empty" means that the length of the input data is greater than zero. In the of Map and Collection, "not empty" refers to how many items it contains.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns:validation="http://www.mulesoft.org/schema/mule/validation"
	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"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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://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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
	<flow name="validationflowFlow">
		<http:listener config-ref="HTTP_Listener_Configuration" path="/validate" allowedMethods="POST" doc:name="HTTP"/>
		<validation:is-not-empty message="Input message is empty"  doc:name="Validation" value="#[payload]"/>
		<logger level="INFO" doc:name="Logger" message="Valid Message"/>
	</flow>
</mule>

Is True and Is False Fallback Validators

You have seen many validations above. Sometimes, there might be a scenario that cannot be met from the above validations. You can use two fallback validators to evaluate the expression is true or is false.

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns:validation="http://www.mulesoft.org/schema/mule/validation"
	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"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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://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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
	<flow name="validationflowFlow">
		<http:listener config-ref="HTTP_Listener_Configuration" path="/validate" allowedMethods="POST" doc:name="HTTP"/>
		<validation:is-true message="Payload Size must be grater than 10"  doc:name="Validation" expression="#[payload &gt; 10]"/>
		<logger level="INFO" doc:name="Logger" message="Valid Message"/>
	</flow>
</mule>

Validating Multiple Conditions at Once

There may be a scenario in which you need to validate the several conditions. If more than one condition fails, it is ideal to generate a single error with all the description.

  • Validators are executed sequentially in flow's thread.

  • All validations are executed, even all validations fails.

  • Unlike the rest of the validators, the all validator does not allow you to directly customize the exception type or the message through validation

  • If any of the validators fails, only a single error will be thrown.

Image title

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:validation="http://www.mulesoft.org/schema/mule/validation" 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" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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://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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="validationflowFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/validate" allowedMethods="POST" doc:name="HTTP"/>
        <validation:all   doc:name="Validation" >
            <validation:validations>
                <validation:is-email email="#[payload]" message="Invalid Email Address"/>
                <validation:is-not-empty value="#[payload]" message="Payload is Empty"/>
                <validation:is-url url="#[payload]" message="Invalid Url"/>
            </validation:validations>
        </validation:all>
        <logger level="INFO" doc:name="Logger" message="Valid Message"/>
    </flow>
</mule>

Now, you know how to implement validations in Mule flow!

MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Solving Parallel Writing Issues in MuleSoft With Distributed Locking
  • Optimizing MuleSoft Performance With HikariCP: A Complete Guide
  • Generic and Dynamic API: MuleSoft

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!