Validate
The Validate EIP uses an Expression or a Predicate to validate the contents of a message.
This is useful for ensuring that messages are valid before attempting to process them.
When a message is not valid then a PredicateValidationException is thrown.
Options
The Validate eip supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
note | The note for this node. | String | |
description | The description for this node. | String | |
disabled | Whether to disable this EIP from the route during build time. Once an EIP has been disabled then it cannot be enabled later at runtime. | false | Boolean |
expression | Required The predicate expression to validate against the current message. If the predicate returns false, a PredicateValidationException is thrown. | ExpressionDefinition | |
predicateExceptionFactory | Reference to a custom PredicateExceptionFactory for creating the exception when validation fails. | PredicateExceptionFactory |
Message Body
The message body at each side of the Validate EIP: EIP input is the body it needs, EIP output the body after it, and Steps the body its own steps start with.
| Side | Body | Description |
|---|---|---|
EIP output |
| Fails the exchange when the predicate does not match: the body stays as it was. |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers are described in the Camel Catalog.
Using Validate EIP
The route below will read the file contents and validate the message body against a regular expression.
-
Java
-
XML
-
YAML
from("file:inbox")
.validate(bodyAs(String.class).regex("^\\w{10}\\,\\d{2}\\,\\w{24}$"))
.to("bean:myServiceBean.processLine"); <route>
<from uri="file:inbox"/>
<validate>
<simple>${body} regex '^\\w{10}\\,\\d{2}\\,\\w{24}$'</simple>
</validate>
<to uri="bean:myServiceBean?method=processLine"/>
</route> - route:
from:
uri: file:inbox
steps:
- validate:
expression:
simple:
expression: "${body} regex '^\\w{10}\\,\\d{2}\\,\\w{24}$'"
- to:
uri: bean:myServiceBean
parameters:
method: processLine Validate EIP is not limited to the message body. You can also validate the message header.
-
Java
-
XML
-
YAML
from("file:inbox")
.validate(header("bar").isGreaterThan(100))
.to("bean:myServiceBean.processLine"); You can also use validate together with the Simple language.
from("file:inbox")
.validate(simple("${header.bar} > 100"))
.to("bean:myServiceBean.processLine"); <route>
<from uri="file:inbox"/>
<validate>
<simple>${header.bar} > 100</simple>
</validate>
<to uri="bean:myServiceBean?method=processLine"/>
</route> - route:
from:
uri: file:inbox
steps:
- validate:
expression:
simple:
expression: "${header.bar} > 100"
- to:
uri: bean:myServiceBean.processLine