Validator
Validator performs declarative validation of the message according to the declared Input Type and/or Output Type on a route definition which declares the expected message type.
| Currently, validators is only supported in Java DSL and the classic Spring XML. |
Data type format
scheme:name where scheme is the type of data model like java, xml or json, and name is the individual data type name.
Supported Validators
| Validator | Description |
|---|---|
Predicate Validator | Validate with using Expression or Predicate |
Endpoint Validator | Validate by forwarding to the Endpoint to be used with validation component such as Validation Component or Bean Validation Component. |
Custom Validator | Validate with using custom validator class. Validator must be a subclass of |
Common Options
All validators have following common options to specify which data type is supported by the validator. type must be specified.
| Name | Description |
|---|---|
type | Data type to validate |
Predicate Validator Options
| Name | Description |
|---|---|
expression | Expression or Predicate to be used for validation |
Here is an example to specify a validation predicate:
-
Java
-
Spring XML
validator()
.type("csv:CSVOrder")
.withExpression(bodyAs(String.class).contains("{name:XOrder}")); <predicateValidator Type="csv:CSVOrder">
<simple>${body} contains '{name:XOrder}'</simple>
</predicateValidator> Endpoint Validator Options
| Name | Description |
|---|---|
ref | Reference to the Endpoint ID |
uri | Endpoint URI |
Here is an example to specify endpoint URI in Java DSL:
-
Java
-
Spring XML
validator()
.type("xml")
.withUri("validator:xsd/schema.xsd"); <endpointValidator uri="validator:xsd/schema.xsd" type="xml"/> Note that the Endpoint Validator just forwards the message to the specified endpoint. In above example, camel forwards the message to the validator: endpoint, which actually is a Validation Component. You can also use any other validation component like Bean Validation Component.
Custom Validator Options
The validator must be an implementation of org.apache.camel.spi.Validator
| Name | Description |
|---|---|
ref | Reference to the custom Validator bean ID |
className | Fully qualified class name of the custom Validator class |
Here is an example to specify custom Validator class:
-
Java
-
Spring XML
validator()
.type("json")
.withJava(com.example.MyCustomValidator.class); <customTransformer className="com.example.MyCustomValidator" type="json"/> Examples
For example to declare the Endpoint Validator which uses validator component to validate xml:ABCOrder, we can do as follows:
-
Java
-
Spring XML
validator()
.type("xml:ABCOrder")
.withUri("validator:xsd/schema.xsd"); <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<validators>
<endpointValidator uri="validator:xsd/schema.xsd" type="xml:ABCOrder"/>
</validators>
</camelContext> If you have the following route definition, above validator will be applied when direct:abc endpoint receives the message. Note that inputTypeWithValidate is used instead of inputType in Java DSL, and the validate attribute on the inputType declaration is set to true in XML DSL:
-
Java
-
Spring XML
from("direct:abc")
.inputTypeWithValidate("xml:ABCOrder")
.log("${body}"); <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:abc"/>
<inputType urn="xml:ABCOrder" validate="true"/>
<log message="${body}"/>
</route>
</camelContext> See Also
The Transformer is a related functionality.