Validator component
The
validatorcomponent checks the XML message body against an XML Schema (XSD) and fails the exchange with aSchemaValidationExceptionlisting every error when it does not conform.
Scheme: validator: · Crate: cameleer-xml · Cargo feature: none for a well-formedness check; xml for full XML Schema validation · Consumer: no · Producer: yes ·
Apache Camel: Validator
When to use it
- Reject malformed partner documents (orders, invoices, HL7 v3, UBL) at the edge of a route.
- Check XML produced by an LLM or a transformation before sending it to a strict backend.
- Route invalid documents to a dead letter or error queue with
doTry/onException.
URI format
validator:schemaResource[?options]schemaResource is a file path relative to the working directory, file:path,
classpath:path (read as a relative path) or ref:beanName. xs:include and xs:import
resolve relative to the schema file.
Example
Validate orders and answer with the validation errors instead of failing the caller.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="xs:string"/>
<xs:element name="qty" type="xs:positiveInteger"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>- route:
id: validator-order
from:
uri: direct:validator-order
steps:
- doTry:
steps:
- to: validator:xsd/order.xsd
- setBody:
constant: valid
doCatch:
- exception:
- org.apache.camel.ValidationException
steps:
- setBody:
simple: "invalid: ${exception.message}"A valid order:
<order id="o-1"><customer>Ada</customer><qty>2</qty></order>validAn order with a missing attribute and a negative quantity:
<order><customer>Ada</customer><qty>-1</qty></order>invalid: Validation failed for: xsd/order.xsd
cvc-complex-type.4
cvc-datatype-valid.1.2.1SchemaValidationException is a ValidationException, so the catch above takes both schema
errors and documents that are not well-formed.
Rust DSL
b.from("direct:validator-rust")
.to("validator:xsd/order.xsd")
.set_body(constant("valid"));Options
| option | default | description |
|---|---|---|
failOnNullBody | true | Fail with NoXmlBodyValidationException when the body is null. |
failOnNullHeader | true | Fail with NoXmlHeaderValidationException when headerName is set and the header is missing. |
headerName | (none) | Validate this header instead of the body. |
useSharedSchema | true | Load and compile the schema once. |
contentCache | true | Same effect as useSharedSchema: false reloads the schema per message. |
Accepted and ignored (Java-only settings): schemaFactory, schemaLanguage,
resourceResolver, resourceResolverFactory, errorHandler, useDom,
schemaReaderFactory, lazyStartProducer.
Supported XML Schema
A subset of XML Schema 1.0: global and local elements, ref, minOccurs / maxOccurs,
sequence / choice / all, model groups, attributes, simple types with restrictions
(facets), the built-in data types, include and import. Without cameleer-xml registered,
cameleer-core's validator only checks well-formedness; cameleer_main::new_main() and the
cameleer CLI always register the full validator.
Message headers
| header / property | direction | description |
|---|---|---|
CamelValidationErrors (exchange property) | out | The list of error messages, set when validation fails. |
Run it with Docker
docker run --rm -v "$PWD:/routes:ro" pmsudhi/cameleer run /routes/validate.camel.yamlCommon questions
How do I get all validation errors, not just the first?
The exception message lists every error, and the CamelValidationErrors exchange property
holds them as a list.
How do I validate a header instead of the body?
Set headerName=myHeader.
Does it also catch XML that is not well-formed?
Yes. A parse error fails with SchemaValidationException too, as Xerces reports it through
the same error handler in Camel.
Related
- json-validator for JSON Schema.
- xslt, xmlsecurity-verify.
- Apache Camel Validator component
Found a problem?
If something on this page doesn't match what Cameleer does, or you hit a bug while building
with it, please help fix it, whether you are a person or a coding agent:
open an issue that describes what you saw,
then fork the repository, fix it, add or update a test, run cargo test --workspace, and open
a pull request. CONTRIBUTING.md
explains the ground rules.