Author: Manish Prabhu
Introduction:
CSV data needs to be validated if we do not have field-level validation enabled in our Mule application. For example, if an application is processing a date field using a transforming message and if the input is having a value other than the date field then it will result in transformation failure. We can make use of try() from the dw::Runtime library to avoid this kind of failure.
About try():
try() uses a user-defined function as a parameter to validate a field and returns an object success: true and result object. If the user-defined function inside try() returns an exception then it will return an object with success: false and error. We can process only good records by making use of try().
Example:
- Input with which we will get success: true

- Input with which we will get success: false

We will make use of try() for the following use case:
Input: CSV payload containing a date field

Expected result: filter good CSV records
productId,productName,pickUpDate
1,p1,2020-01-01
3,p3,2021-02-01
Mule flow:

Transformation using try():

In the above dwl code, validateDate() is a user-defined function. When we use it inside try(), it will return success: true where we have a date data type for the input fields along with yyyy-MM-dd as the date format. As we have applied a filter on the payload, it will give us the records which are having good data.
Postman output:

Application logs:
INFO 2022-07-25 19:29:44,217 [[MuleRuntime].uber.03: [csv-validation].csv-validationFlow.CPU_LITE @69447378] [processor: csv-validationFlow/processors/0; event: 09378bd0-0c22-11ed-a560-ca58c0b0e447] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: input payload:
INFO 2022-07-25 19:29:44,232 [[MuleRuntime].uber.03: [csv-validation].csv-validationFlow.CPU_LITE @69447378] [processor: csv-validationFlow/processors/1; event: 09378bd0-0c22-11ed-a560-ca58c0b0e447] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: productId,productName,pickUpDate
1,p1,2020-01-01
2,p2,abc
3,p3,2021-02-01
INFO 2022-07-25 19:29:44,330 [[MuleRuntime].uber.08: [csv-validation].csv-validationFlow.CPU_INTENSIVE @e05b52e] [processor: csv-validationFlow/processors/4; event: 09378bd0-0c22-11ed-a560-ca58c0b0e447] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: productId,productName,pickUpDate
1,p1,2020-01-01
3,p3,2021-02-01
Reference: https://docs.mulesoft.com/dataweave/2.4/dw-runtime-functions-try