Cameleer

Validator component

The validator component checks the XML message body against an XML Schema (XSD) and fails the exchange with a SchemaValidationException listing 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

text
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.

xml
<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>
yaml example: validator-order-xsd
- 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:

xml
<order id="o-1"><customer>Ada</customer><qty>2</qty></order>
text
valid

An order with a missing attribute and a negative quantity:

xml
<order><customer>Ada</customer><qty>-1</qty></order>
text
invalid: Validation failed for: xsd/order.xsd
cvc-complex-type.4
cvc-datatype-valid.1.2.1

SchemaValidationException is a ValidationException, so the catch above takes both schema errors and documents that are not well-formed.

Rust DSL

rust
b.from("direct:validator-rust")
    .to("validator:xsd/order.xsd")
    .set_body(constant("valid"));

Options

optiondefaultdescription
failOnNullBodytrueFail with NoXmlBodyValidationException when the body is null.
failOnNullHeadertrueFail with NoXmlHeaderValidationException when headerName is set and the header is missing.
headerName(none)Validate this header instead of the body.
useSharedSchematrueLoad and compile the schema once.
contentCachetrueSame 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 / propertydirectiondescription
CamelValidationErrors (exchange property)outThe list of error messages, set when validation fails.

Run it with Docker

sh
docker run --rm -v "$PWD:/routes:ro" pmsudhi/cameleer run /routes/validate.camel.yaml

Common 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.

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.

Edit this page on GitHub