이 콘텐츠는 선택한 언어로 제공되지 않습니다.

11.4. Supported Validators


11.4.1. Java

11.4.1.1. Use a Java Validator

Procedure 11.2. Create a Java Validator

  1. Implement the org.switchyard.validate.Validator interface.
  2. Add a <validate.java> definition to your switchyard.xml file.
  3. Alternatively, annotate one or more methods to your Java class with @Validator:
            
    @Named("MyValidatorBean")
    public class MyValidator {
        @Validator(name = "{urn:switchyard-quickstart-demo:orders:1.0}submitOrder")
        public ValidationResult validate(Element from) {
           // handle validation here
        }
    }        
            
    
    
    Copy to Clipboard Toggle word wrap
    When using the @Validator annotation, the SwitchYard Maven plug-in automatically generates the <validate.java> definitions for you and add them to the switchyard.xml file packaged in your application.
    The Java class above produces this <validate.java> definition:
        
    <validate.java bean="MyValidatorBean"
                   name="{urn:switchyard-quickstart-demo:orders:1.0}submitOrder"/>    
    
    
    Copy to Clipboard Toggle word wrap

11.4.1.2. Java Validator Reference

The optional name element of the @Validator annotation can be used to specify the qualified type name used during validator registration. If not supplied, the full class name of the method parameter is used as the type.
The CDI bean name specified by @Named annotation is used to resolve validator class. If you fail to specify it, then the validator's class name is used instead:
<validates>
   <validate.java class="org.switchyard.quickstarts.demos.orders.MyValidator"
                   name="{urn:switchyard-quickstart-demo:orders:1.0}submitOrder"/>
</transforms>
Copy to Clipboard Toggle word wrap
Note
Each of the <validate.java> definitions have either a bean attribute or a class attribute. These two attributes are mutually exclusive.

11.4.1.3. ValidationResult

ValidationResult is a simple interface which represents the result of validation. It has two methods, isValid() and getDetail().

11.4.1.4. Java Validation Failure

If Java validation fails, the message exchange process stops immediately and a HandlerException is thrown along with a validation failure message which is returned by ValidationResult.getDetail(). Make sure that you return a ValidationResult with failure detail message when you want to indicate a validation failure in your Java Validator, instead of throwing a RuntimeException.

11.4.1.5. ValidationResult Properties

isValid() returns whether the validation succeeded or not. _getDetail() returns an error message if validation fails.
package org.switchyard.validate;
public interface ValidationResult {
    boolean isValid();
    String getDetail();
}
Copy to Clipboard Toggle word wrap
There are three convenience methods available for org.switchyard.validate.BaseValidator. These are validResult(), invalidResult() and invalidResult(String) Use them to generate ValidationResult objects.

11.4.2. XML

11.4.2.1. Use an XML Validator

The XML validator allows you to perform a validation against its schema definition. It support DTD, XML_SCHEMA, and RELAX_NG schema types. You can configure an XML validator by specifying the schema Type, the name QName, and the path to the schema file. Here is an example:
<validate.xml schemaType="XML_SCHEMA" name="{urn:switchyard-quickstart:validate-xml:0.1.0}order" failOnWarning="true" namespaceAware="true">
   <schemaFiles>
      <entry file="/xsd/orders.xsd"/>
   </schemaFiles>
   <schemaCatalogs>
      <entry file="/xsd/catalog.xml"/>
   </schemaCatalogs>
</validate.xml>
Copy to Clipboard Toggle word wrap
If you specify the failOnWarning attribute as true, the validation fails if any warning is detected during validation. If the XML content to be validated has namespace prefix, then you need to specify namespaceAware as true.

11.4.2.2. XML Catalog

You can use XML catalog to decouple the schema file location from schema definition itself. This schema is orders.xsd which has a import element. It refers to logical name orders.base by the schemaLocation attribute:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="urn:switchyard-quickstart:validate-xml:0.1.0"
        xmlns:base="urn:switchyard-quickstart:validate-xml-base:0.1.0"
        xmlns:orders="urn:switchyard-quickstart:validate-xml:0.1.0">
        <import namespace="urn:switchyard-quickstart:validate-xml-base:0.1.0" schemaLocation="orders.base"/>
...
Copy to Clipboard Toggle word wrap
Here is the catalog.xml file that resolves actual schema location from logical name orders.base:
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
    <system systemId="orders.base" uri="orders-base.xsd"/>
</catalog>
Copy to Clipboard Toggle word wrap

11.4.2.3. XML Validation Failure

If XML validation fails, the message exchange process stops immediately and a HandlerException is thrown along with a validation failure message. XMLValidator collects a set of validation failures through the SAX ErrorHandler, and uses the getMessage() of each received SAXParseException as a failure detail with extracting root cause, if exists.
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat