Custom Validation in Mule 4 Using Mule SDK
Published in
3 min readAug 16, 2022
Validators are the operations that validate a Mule message without changing it. Validators can produce the following effects:
- Validator condition is fulfilled: the flow continues and the Mule message remains unchanged
- Validator condition not fulfilled: an error is thrown
Today, we will be creating a custom validation that will indicate whether the currency is valid or not.
Prerequisite
- JDK 1.8 or 11
- Apache Maven (at minimum, 3.3.9)
Steps To Create Custom Validator
- Create Project: Go to your project folder and run the Maven command to generate the project for the mule validator extension:
mvn org.mule.extensions:mule-extensions-archetype-maven-plugin:1.2.0:generate
- Remove Connector Configurations: delete the four java classes below. These files will be used for the custom connectors.
* CurrencyValidatorConfiguration
* CurrencyValidatorConnection
* CurrencyValidatorConnectionProvider
* CurrencyValidatorOperationsTestCase - Currency Dependency: add the dependency below in pom.xml for currency.
- CurrencyValidatorExtension: this is a currency validator extension class. Add the lines below in this class.
- CurrencyValidatorOperations: the class below adds a validation method that is annotated with
@Validator
. The method throws an error type that comes from the generic validation error type. Note that you can also annotate the classCurrencyValidatorOperations
with@Validator
, but if you do, any other operations in it will be flagged as validators.
- CurrencyError: creates the error to throw if the currency validation fails. Notice that it is named according to the validation failure.
- CurrencyErrorsProvider: the validator method needs a
ErrorTypeProvider
that knows all the error types that the validation can throw. This example creates aErrorTypeProvider
that says that the only error the method can throw is of typeCURRENCY-VALIDATOR:INVALID_CURRENCY_CODE
.
- Validator Icon: go to the project root directory and create an icon folder. Place an SVG file image in this folder and the icon file name should be icon.svg.
- Maven Build: go to the project directory and run the terminal. Run
mvn clean install
to build the project.
How to use Custom Validators in a Mule Application
- Create a Mule Application: go to Anypoint Studio and create a Mule application to test the custom validation.
- Add Custom Validator Mule Plugin: go to pom.xml and add the dependency for the custom validator Mule plugin for currency. Make sure you have added the classifier as Mule-plugin. The dependency information will come from the Mule SDK validator.
- HTTP Listener: add an HTTP listener with the default configuration in the test Mule application.
- Currency Validation: drag the currency validation and pass the currency. We are reading the currency from query params.
- Failed Validation Execution: below is the Mule application error logs for invalid currency. Curl command:
curl --location --request GET 'localhost:8081/currency?currency=RUPEE'
.
- Success Execution: if we pass the correct currency, we will get no validation error and response as currency INR. Curl command:
curl --location --request GET 'localhost:8081/currency?currency=INR'
.
Here is the Github repository for Mule custom validation.
Conclusion
Today, we have learned how to develop Mule custom validation using Mule JAVA SDK. In the next tutorial, I will cover the Mule SDK for the rest API.