Problem
Validate incoming data before processing. docs
12 reusable validation functions for DataWeave 2.x — field validation, pattern matching, and bulk payload validation. docs
The table
| Function | Signature | Returns / behaviour | Source |
|---|---|---|---|
isRequired |
fun isRequired(val: Any, fieldName: String): Object = |
returns name as { valid: false, field: "name", error: "name must not be empty" }, email as { valid: false, field: "email", error: "email is required" }, tags as { valid: false, field: "tags", error: "tags must not be empty" }, and status as { valid: true }
|
verified in sandbox |
minLength |
fun minLength(s: String, min: Number, fieldName: String = "field"): Object = |
returns valid as false, field as "field", and error as "field must be at least 5 characters" for a string shorter than the limit without a custom field name |
verified in sandbox |
maxLength |
fun maxLength(s: String, max: Number, fieldName: String = "field"): Object = |
returns valid as false, field as "name", and error as "name must not exceed 10 characters" when the input exceeds the limit |
verified in sandbox |
inRange |
fun inRange(n: Number, min: Number, max: Number, fieldName: String = "field"): Object = |
returns inside as { valid: true }, boundary as { valid: true }, and outside as { valid: false, field: "age", error: "age must be between 10 and 100" }
|
verified in sandbox |
matchesPattern |
fun matchesPattern(s: String, regex: String, fieldName: String = "field"): Object = |
returns validMatch as { valid: true } and invalidMatch as { valid: false, field: "invalidCode", error: "invalidCode does not match required pattern" }
|
verified in sandbox |
isValidDate |
fun isValidDate(s: String, fmt: String, fieldName: String = "field"): Object = |
returns validCheck as { valid: true } and invalidCheck as { valid: false, field: "invalidDate", error: "invalidDate is not a valid date (expected format: yyyy-MM-dd)" }
|
verified in sandbox |
isOneOf |
fun isOneOf(val: Any, allowed: Array, fieldName: String = "field"): Object = |
returns valid as false, field as "status", and error as "status must be one of: ACTIVE, INACTIVE, PENDING" when the value falls outside the set |
verified in sandbox |
isUUID |
fun isUUID(s: String): Boolean = |
returns valid as true, nil as true, and invalid as false for the respective inputs |
verified in sandbox |
isURL |
fun isURL(s: String): Boolean = |
returns validUrl as true and invalidWord as false for the respective inputs |
verified in sandbox |
isPhone |
fun isPhone(s: String): Boolean = |
returns e164 as true and sixteenDigits as false for the respective inputs |
verified in sandbox |
validateAll |
fun validateAll(obj: Object, rules: Object): Object = do { |
returns valid as false and errors as [{ valid: false, field: "email", error: "email is required" }] when rules fail |
verified in sandbox |
hasRequiredFields |
fun hasRequiredFields(obj: Object, fields: Array<String>): Object = do { |
returns valid as false, missing as ["email", "phone"], and error as "Missing required fields: email, phone" when fields are absent |
verified in sandbox |
How to read it
I check the Function column for the exact module export name and verify the Signature column matches my runtime DataWeave version. I read the Returns / behaviour column to confirm whether the call yields a Boolean or an Object containing valid, field, and error keys, noting that minLength defaults the field name to "field". I always place import modules::ValidationUtils at the top of my script before referencing any exported function.
Traps
Missing import causes the runtime to fail with Unable to resolve reference of ValidationUtils::isRequired when the script executes without the module declaration.
What I do now
I load the module at the top of every DataWeave file and pass payload fields directly into the validation functions or wrap them in validateAll for bulk checks. I parse the returned Object to extract the valid flag and route flows based on whether errors contains entries.
Runs shown: DataWeave CLI 2.12.2
104 DataWeave patterns + 8 Exchange modules with 208 MUnit tests: github.com/shakarbisetty/mulesoft-cookbook | 60-second walkthroughs: youtube.com/@SanThaParv


Top comments (0)