blob: f7fa90801a6366a4f94043e804c197785e61f667 [file] [log] [blame]
= 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.
== Data type format
[source,text]
----
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
[width="100%",cols="25%,75%",options="header",]
|===
| 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 `org.apache.camel.spi.Validator`
|===
== Common Options
All validators have following common options to specify which data type is supported by the validator.
`type` must be specified.
[width="100%",cols="25%,75%",options="header",]
|===
| Name | Description
| type | Data type to validate
|===
== Predicate Validator Options
[width="100%",cols="25%,75%",options="header",]
|===
| Name | Description
| expression | Expression or Predicate to be used for validation
|===
Here is an example to specify a validation predicate:
Java DSL:
[source,java]
----
validator()
.type("csv:CSVOrder")
.withExpression(bodyAs(String.class).contains("{name:XOrder}"));
----
XML DSL:
[source,xml]
----
<predicateValidator Type="csv:CSVOrder">
<simple>${body} contains '{name:XOrder}'</simple>
</predicateValidator>
----
== Endpoint Validator Options
[width="100%",cols="25%,75%",options="header",]
|===
| Name | Description
| ref | Reference to the Endpoint ID
| uri | Endpoint URI
|===
Here is an example to specify endpoint URI in Java DSL:
[source,java]
----
validator()
.type("xml")
.withUri("validator:xsd/schema.xsd");
----
And here is an example to specify endpoint ref in XML DSL:
[source,xml]
----
<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
xref:components::validator-component.adoc[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`
[width="100%",cols="25%,75%",options="header",]
|===
| 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 DSL:
[source,java]
----
validator()
.type("json")
.withJava(com.example.MyCustomValidator.class);
----
XML DSL:
[source,xml]
----
<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 DSL:
[source,java]
----
validator()
.type("xml:ABCOrder")
.withUri("validator:xsd/schema.xsd");
----
And in XML DSL:
[source,xml]
----
<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 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 DSL:
[source,java]
----
from("direct:abc")
.inputTypeWithValidate("xml:ABCOrder")
.log("${body}");
----
XML DSL:
[source,xml]
----
<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 xref:transformer.adoc[Transformer] is a related functionality.