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.
Join the DZone community and get the full member experience.
Join For FreeValidations 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.
<?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.
<?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*$).
<?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.
<?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).
<?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.
<?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.
<?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 > 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.
<?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!
Opinions expressed by DZone contributors are their own.
Comments