blob: 412c470c74a510756fa0040687440b9c7ca448d6 [file]
= Validator Component
:doctitle: Validator
:shortname: validator
:artifactid: camel-validator
:description: Validate the payload using XML Schema and JAXP Validation.
:since: 1.1
:supportlevel: Stable
:tabs-sync-option:
:component-header: Only producer is supported
:core:
*Since Camel {since}*
*{component-header}*
The Validation component performs XML validation of the message body
using the JAXP Validation API and based on any of the supported XML
schema languages, which defaults to http://www.w3.org/XML/Schema[XML
Schema].
== URI format
----
validator:someLocalOrRemoteResource
----
Where *someLocalOrRemoteResource* is some URL to a local resource on the
classpath or a full URL to a remote resource or resource on the file
system which contains the XSD to validate against. For example:
* `validator:com/mypackage/myschema.xsd`
The Validation component is provided directly in the camel-core.
// component options: START
include::partial$component-configure-options.adoc[]
include::partial$component-endpoint-options.adoc[]
include::partial$component-endpoint-headers.adoc[]
// component options: END
== Example
The following
https://github.com/apache/camel/blob/main/components/camel-spring-xml/src/test/resources/org/apache/camel/component/validator/camelContext.xml[example]
shows how to configure a route from endpoint *direct:start* which then
goes to one of two endpoints, either *mock:valid* or *mock:invalid*
based on whether the XML matches the given schema (which is
supplied on the classpath).
When the XML does not match the schema, the validator throws an `org.apache.camel.ValidationException`,
which the `doCatch` handles:
[source,yaml]
----
- route:
from:
uri: direct:start
steps:
- doTry:
steps:
- to:
uri: validator:org/apache/camel/component/validator/schema.xsd
- to:
uri: mock:valid
doCatch:
- exception:
- org.apache.camel.ValidationException
steps:
- to:
uri: mock:invalid
doFinally:
steps:
- to:
uri: mock:finally
----
To validate a header instead of the message body, set the `headerName` option:
[source,yaml]
----
- route:
from:
uri: direct:startHeaders
steps:
- to:
uri: validator:org/apache/camel/component/validator/schema.xsd
parameters:
headerName: headerToValidate
- to:
uri: mock:valid
----
== Advanced: JMX method `clearCachedSchema`
You can force that the cached schema in the
validator endpoint is cleared and reread with the next process call with
the JMX operation `clearCachedSchema`. You can also use this method to
programmatically clear the cache. This method is available on the
`ValidatorEndpoint` class.