Introduction: Validation Module and its operations.
In Mule 4, the Validation module is used to ensure data integrity by checking if data meets certain criteria before further processing.
Supported operations:
Is Allowed Ip: Validates that an IP address is present in the IP list.
Is Blank String: Validates that value is a blank String.
Is Elapsed: Validates that a given time has elapsed since the specified initial time.
Is Email: Validates that the email address is valid.
Is Empty Collection: Validates that value is an empty collection.
Is False: Validates that the given value is false.
Is IP: Validates that an IP address represented as a String is valid.
Is Not Blank String: Validates that value is not a blank String.
Is Not Denied Ip: Validates that an IP address is not present in the IP list.
Is Not Elapsed: Validates that a given time has not elapsed since the specified initial time.
Is Not Empty Collection: Validates that value is not an empty collection.
Is Not Null: Validates that the given value is not null. Keep in mind that the definition of null can vary depending on the mimeType value.
For example, for an application/java mimeType, null means a blank pointer. However, if the mimeType is application/json then the
String null is also a null value.
Is Null: Validates that the given value is null. Keep in mind that the definition of null can vary depending on the mimeType value.
Is Number: Receives a numeric value as a String and validates that it can be parsed per the rules of a numberType
Is Time: Validates that a time in String format is valid for the given pattern and locale. If no pattern is provided, then the locale’s default is
used.
Is True: Validates that the given value is true.
Is URL: Validates that url is valid.
Matches Regex: Validates that the value matches to the regex regular expression.
Validate Size: Validates that value has a size between certain inclusive boundaries. This validator is capable of handling instances of
String, collection, map, and arrays.
Any: Perform a list of nested validation operations and informs only one. VALIDATION: MULTIPLE error which summarizes all the found errors (if any).
All: Perform a list of nested validation operations and informs only one. VALIDATION: MULTIPLE error which summarizes all the found errors (if any).
Walkthrough/Steps
1. Configure the Is IP Operation
The Is IP operation checks that a given IP address is valid. It supports both IPV4 and IPV6 addresses. In the case of IPV6, the operation
supports both full and collapsed addresses, but not addresses containing ports. The following example shows how to configure the operation in Anypoint Studio:
In the Mule Palette view, select Validation > Is IP.
Drag Is IP onto the Studio canvas.
In the Is IP configuration screen, provide the following parameters:
o Ip: payload.ip
o Message: The value provided is not a valid IP address.(optional)
2. Configure the Is Elapsed Operation
The Is elapsed operation validates that a given time has elapsed since the specified initial time.
The following example shows how to configure the operation in Anypoint Studio:
In the Mule Palette view, select Validation > Is elapsed.
Drag Is elapsed onto the Studio canvas.
In the Is elapsed configuration screen, provide the following parameters:
o Time: 2
o Time unit: SECONDS
o Since: payload (initial time set in the payload)
3. Configure the Is Email Operation
The Is email operation checks that a given email is valid. The value defaults to the email payload.
The following example shows how to configure the operation in Anypoint Studio:
In the Mule Palette view, select Validation > Is email.
Drag Is email onto the Studio canvas.
In the Is email configuration screen, provide the following parameters:
o Email: payload.emailId
o Message: The value is not a valid email.(optional)
4. Configure the Validate Size Operation
The Validate size operation validates that the input size is between the given minimum and maximum boundaries. This is valid for inputs of
type string, Collection, Map, and Array. In the case of a string, the size refers to the length in characters.
The following example shows how to configure the operation in Anypoint Studio:
In the Mule Palette view, select Validation > Validate size.
Drag Validate size onto the Studio canvas.
In the Validate size configuration screen, provide the following parameters:
o Value: payload.Name
o Min: 8(the minimum size of Name)
o Max: 30(the maximum size of Name)
5. Configure the Is Time Operation
The Is time operation verifies that a time is valid for the given pattern and locale. If no pattern is provided, then the locale default is used.
The following example shows how to configure the operation in Anypoint Studio:
In the Mule Palette view, select Validation > Is time.
Drag Is time onto the Studio canvas.
In the Is time configuration screen, provide the following parameters:
o Time: payload.time
o Pattern: HH:mm::ss
6. Configure the Is Number Operation
The Is number operation verifies that a string can be parsed as a number of a specified numeric type, using the given locale (which defaults
to the system locale) and pattern (which defaults to the locale pattern). This validation can include a range in which the number is validated
using the Min value and Max Value parameters.
The following example shows how to configure the operation in Anypoint Studio:
In the Mule Palette view, select Validation > Is number.
Drag Is number onto the Studio canvas.
In the Is number configuration screen, provide the following parameters:
o Value: payload.age
o Min value: set minimum value for age
o Max value: set maximum value for age
o Number type: INTEGER
7. Configure the Is Empty Collection Operation
The Is empty collection operation validates that the provided value is an empty collection.
The following example shows how to configure the operation in Anypoint Studio:
In the Mule Palette view, select Validation > Is empty collection.
Drag Is empty collection onto the Studio canvas.
In the Is empty collection configuration screen, set the Value field to payload.collection
8. Configure the Is Allowed IP Operation
The Is allowed ip operation validates that a given IP is in the allowlisted IP list.
The following example shows how to configure the operation in Anypoint Studio:
In the Mule Palette view, select Validation > Is allowed ip.
Drag Is allowed ip onto the Studio canvas.
In the Is allowed ip configuration screen, provide the following parameters:
o Ip address: payload.address
o Allow list: Edit inline.
o Ips: Edit inline
Click the plus sign (+) to open the IP value window.
Set Values of Valid IP.
9. Configure the All Scope
The All scope evaluates a list of nested validation operations and can return only one VALIDATION:MULTIPLE error which summarizes all
of the found errors. Sometimes you want to evaluate several conditions, and some might fail simultaneously. In these cases, generate a
single error that contains all of the descriptions. About the All scope validator:
o All validations are executed even if all of them fail.
o If any of the validations fail, a single exception is thrown. The exception contains a multiline message in which each line corresponds to a
failing validation.
o Validators are executed sequentially using the flow’s thread, but the order doesn’t matter because validators don’t cause any side effects.
o Unlike the rest of the validation operations, the All scope validator does not enable you to customize the exception type or the message
through validation:exception or exception factory elements. However, you can customize the message of the inner validation operations.
ANY Scope:
Perform a list of nested validation operations and informs only one VALIDATION:MULTIPLE error which summarizes all of the found errors
(if all failed).
10. Configure the Matches Regex Operation
The Matches regex operation validates that a given expression matches a Java regular expression.
The following example shows how to configure the operation in Anypoint Studio:
In the Mule Palette view, select Validation > Matches regex.
Drag Matches regex onto the Studio canvas.
In the Expression field of the Matches regex configuration screen, select Expression, and in the empty field add #[payload.userId]
Specify the pattern to be satisfied by the value in expression field, in the Regex field.