News Ticker

Validation Module: ALL vs ANY scope

Validation Module

Main points:

  • Validation module provides fixed data validation logic
  • There are two scopes: ALL and ANY
  • The ALL scope expects all validations to pass
  • The ANY scope expects at least one validation to pass
  • Example code in GitHub repository

The Validation module

The Mule Validation Module provides a set of 19 fix validators and two scopes. The validators provide a convenient way to test that data conforms to a pattern. For example, the Is Email validator tests if the input string is a valid email address. The two scopes collate validators together and execute them collectively, and depending on whether the scope is ALL or ANY, execution of the flow will continue or an exception will be thrown. See figure 1 for a full list of validators and scopes:

Figure 1: All validators and scopes in the Validation Module

The ALL and ANY scopes

Validators can be collected together in one of two available scopes: ALL or ANY. The manner in which the result of the validator inside the scopes varies.

The ALL scope

The ALL scope executes all validators and requires that all validations pass for the flow to continue to execute. If a validation fails, the remaining validators are executed and a VALIDATION:MULTIPLE exception is thrown. The exception collects together all failed validations.

In figure 2 the three validators are executed and if the data being validated does not pass a validation, subsequent validators are executed and if further validation failures occur they are collected together and a VALIDATION:MULTIPLE exception is thrown with error message summarizing the failed validations.

Figure 2: ANY scope

ALL scope example

Assume that the data being validated is the number 100. Two validations will fail: Is null and Is email. The VALIDATION:MULTIPLE exception will contain the following message:

value was expected to be null
100 is not a valid email address

Example code in GitHub repository.

The ANY scope

The ANY scope executes all validators and requires that a least one validations passes for the flow to continue to execute. If a validation fails, the remaining validators are executed. If all validators fail a VALIDATION:MULTIPLE exception is thrown. The exception collects together all failed validations.

In figure 3 the three validators are executed and if the data being validated does not pass a validation, subsequent validators are executed and if all validation fails they are collected together and a VALIDATION:MULTIPLE exception is thrown with error message summarizing the failed validations.

Figure 3: ANY scope

ANY scope example

Assume that the data being validated is the string cat. All three validators will fail. The VALIDATION:MULTIPLE exception will contain the following message:

value was expected to be null
cat is not a valid INTEGER value
cat is not a valid email address

Example code in GitHub repository.

Conclusion

The validation module provides convenient ways implement defensive programming approach to ensure that data is conformant with requirements. This approach reduces the possibility of subsequent errors occurring as a result of malformed data.

Leave a Reply

%d bloggers like this: