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
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • How AI Will Change Agile Project Management
  • TDD vs. BDD: Choosing The Suitable Framework
  • An Overview of Cloud Cryptography
  • Decoding eBPF Observability: How eBPF Transforms Observability as We Know It

Trending

  • How AI Will Change Agile Project Management
  • TDD vs. BDD: Choosing The Suitable Framework
  • An Overview of Cloud Cryptography
  • Decoding eBPF Observability: How eBPF Transforms Observability as We Know It
  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.

Jitendra Bafna user avatar by
Jitendra Bafna
CORE ·
Mar. 05, 17 · Tutorial
Like (5)
Save
Tweet
Share
17.87K 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.

Trending

  • How AI Will Change Agile Project Management
  • TDD vs. BDD: Choosing The Suitable Framework
  • An Overview of Cloud Cryptography
  • Decoding eBPF Observability: How eBPF Transforms Observability as We Know It

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: