Architecture

Validator

qianmoQqianmoQ· 更新于 2026-09-23· 阅读 11 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

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

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

ValidatorDescription
Predicate ValidatorValidate with using Expression or Predicate
Endpoint ValidatorValidate by forwarding to the Endpoint to be used with validation component such as Validation Component or Bean Validation Component.
Custom ValidatorValidate 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.

NameDescription
typeData type to validate

Predicate Validator Options

NameDescription
expressionExpression or Predicate to be used for validation

Here is an example to specify a validation predicate:

  • Java
  • XML
  • YAML
validator()
    .type("csv:CSVOrder")
    .withExpression(simple("${body} contains '{name:XOrder}'"));
<validators>
    <predicateValidator type="csv:CSVOrder">
        <simple>${body} contains '{name:XOrder}'</simple>
    </predicateValidator>
</validators>
- validators:
    predicateValidator:
      type: csv:CSVOrder
      expression:
        simple:
          expression: "${body} contains '{name:XOrder}'"

Endpoint Validator Options

NameDescription
refReference to the Endpoint ID
uriEndpoint URI

Here is an example to specify endpoint URI in Java DSL:

  • Java
  • XML
  • YAML
validator()
    .type("xml")
    .withUri("validator:xsd/schema.xsd");
<validators>
    <endpointValidator uri="validator:xsd/schema.xsd" type="xml"/>
</validators>
- validators:
    endpointValidator:
      type: xml
      uri: validator:xsd/schema.xsd

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

NameDescription
refReference to the custom Validator bean ID
classNameFully qualified class name of the custom Validator class

Here is an example to specify custom Validator class:

  • Java
  • XML
  • YAML
validator()
    .type("json")
    .withJava(com.example.MyCustomValidator.class);
<validators>
    <customValidator className="com.example.MyCustomValidator" type="json"/>
</validators>
- validators:
    customValidator:
      type: json
      className: com.example.MyCustomValidator

Examples

For example to declare the Endpoint Validator which uses validator component to validate xml:ABCOrder, we can do as follows:

  • Java
  • XML
  • YAML
validator()
    .type("xml:ABCOrder")
    .withUri("validator:xsd/schema.xsd");
<validators>
    <endpointValidator uri="validator:xsd/schema.xsd" type="xml:ABCOrder"/>
</validators>
- validators:
    endpointValidator:
      type: xml:ABCOrder
      uri: "validator:xsd/schema.xsd"

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
  • XML
  • YAML
from("direct:abc")
    .inputTypeWithValidate("xml:ABCOrder")
    .log("${body}");
<route>
    <from uri="direct:abc"/>
    <inputType urn="xml:ABCOrder" validate="true"/>
    <log message="${body}"/>
</route>
- route:
    inputType:
      urn: xml:ABCOrder
      validate: "true"
    from:
      uri: direct:abc
      steps:
        - log:
            message: "${body}"

See Also

The Transformer is a related functionality.

评论

登录后参与评论

正在加载评论…