Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The Valiade Mediator is used for schema validation of messages. The portion of the message that needs to be validated is chosen by evaluating the XPath expression. Then this message is validated against the schema specified. Users can specify a sequence to be executed in case of a validation failure. This mediator provides an extension point by providing properties to configure the underline validation engineA selected part of an XML message is validated against a specified schema. This mediator can be extended by adding properties to enable/disable specific features of the validation engine.

Tip

A Fault Mediator should be added as a child to the Validate mediator in order specify the fault sequence to be followed if the validation fails.

Info

The Validate mediator is a content aware mediator.

...

Code Block
XML
XML
<validate [source="xpath"]>
   <property name="validation-feature-id" value="true|false"/>*
   <schema key="string"/>+
   <on-fail>
      mediator+
   </on-fail>
</validate>

 

...

UI Configuration

Image Added

The Validate Mediator validates the result of the evaluation of the source XPath expression, against the schema specified. If the source attribute is not specified, the validation is performed against the first child of the SOAP body of the current message. If the validation fails, the on-fail sequence of mediators is executed. Properties could be used to turn some of the features of the validator on/off.

UI Configuration

Image Removedmediator configuration can be divided into the following sections.

Schema keys defined for Validate Mediator

This section is used to specify the key to access the main schema based on which the validation is carried out, as well as to specify the XML which needs to be validated. The parameters available in this section are as follows.

Parameter NameDescription
Schema keys defined for Validate Mediator table

This parameter is used to specify the key for the schema location. It can be specified using one of the following methods.

  • Enter If the key is a static key if the key is a static value.Enter an expression to calculate the value if value, select Static Key from the list and enter a static key in the data field. This value should be pre-defined and saved as a resource in the Registry. Click either Configuration Registry or Governance Registry as relevant to select the key from the resource tree.
  • If the key is a dynamic value.
  
  
  

 

...

  • , Select Dynamic Key from the list and enter an expression to calculate the value in the data field.

Click Add Key to add a new schema key. Click Delete in the relevant row to delete a schema key.

Info
titleTip

...

To add a new Schema Key, use the "Add New Schema Key" link.

You can click NameSpaces to add namespaces if you are providing an expression. Then the Namespace Editor panel would appear where you can provide any number of namespace prefixes and URLs used in the XPath expression. 

Source

The XPath expression to extract the XML that needs to be validated. The evaluation of this expression is validated against the schema specified in the Schema keys defined for Validate Mediator table. If this is not specified, the validation is performed against the first child of the SOAP body of the current message.

Info
titleTip

You can click

...

NameSpaces to add namespaces if you are providing an expression.

...

Then the Namespace Editor panel would appear where you can provide any number of namespace prefixes and

...

URLs used in the XPath expression.

...

 

Features Defined for Validator Mediator

This section is used to specify which features of the Validate mediator should be enabled and which should be disabled. The parameters available in this section are as follows.

Info

Only the FEATURE_SECURE_PROCESSING feature is currently supported by the validator.

Parameter NameDescription
Feature Name

...

The name of the feature.

...

titleNote
ValueThis parameter is used to specify whether the feature should be enabled or not. Click True to enable and False to disable.
ActionClick Delete in the relevant row to delete a feature.
Resources of the Validate Mediator

A resource in the Validate mediator configuration enables you to import a schema referenced within another schema. In order to access such a schema via a resource, the parent schema should be saved as a resource in the registry. The parameters available in this section are as follows.

Parameter NameDescription
LocationThe location of the schema to be imported. The value entered here should be equal to the value of the schema location attribute within the relevant <xsd:import> element in the parent schema.
KeyThe key to access the parent schema saved in the registry. Click either Configuration Registry or Governance Registry as relevant to select the key from the resource tree.
Info
titleNote

You can also configure the Mediator using XML. Click on "switch to source view" in the "Mediator" window.

...

Example

Example 1 - Basic configuration

In this example, the required schema for validating messages going through the validate mediator is given as a registry key, schema\sample.xsd. No source attribute is specified, and therefore the schema will be used to validate the first child of the SOAP body. The mediation logic to follow if the validation fails is defined within the on-fail element. In this example, the Fault Mediator is used to create a SOAP fault to be sent back to the party which sent the message.

Code Block
XML
XML
<validate>
    <schema key="schema\sample.xsd"/>
         <on-fail>
                <makefault>
                    <code value="tns:Receiver"
                            xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/>
                    <reason value="Invalid Request!!!"/>
                </makefault>
                <property name="RESPONSE" value="true"/>
                <header name="To" expression="get-property('ReplyTo')"/>
         </on-fail>
</validate>

Example 2 - Validate mediator with resources

In the above Validate Mediator configuration, the required schema for validating messages going through the validate mediator is given as a registry key, schema\sample.xsd. In this sample configuration, there is no source attribute. Therefore, in this case, the Validate Mediator takes source as the first child of SOAP Body. Within on-fail, it is possible to specify mediation logic for the failed validation. In the above configuration, it creates a SOAP fault using makefault mediator and sends that back to the requested partythis example, the following schema named 08MockServiceSchema is saved in the Registry. This schema is located in MockDataTypes.xsd. A reference is made within this schema to another schema named 08SOAPFaults which is located in SOAPFaults.xsd.

Code Block
languagexml
<xsd:import namespace= "http://samples.synapse.com/08MockServiceSchema" schemalocation= "MockDataTypes.xsd">
	<xsd:import namespace= "http://samples.synapse.com/08SOAPFaults" schemalocation= "../Common/SOAPFaults.xsd">
</xsd:import>

The Validate mediator can be configured as follows.

Code Block
languagexml
<validate>
	<schema key="MockDataTypes.xsd"/>
	<resource location="../Common/SOAPFaults.xsd" key="conf:custom/schema/SOAPFaults.xsd"/>
	<on-fail>
		<log level="custom">
			<property name="validation failed" value="Validation failed ###"/>
			<property name="error_msg" expression="$ctx:ERROR_MESSAGE"/>
		</log>
	</on-fail>
</validate>

The schema used by the validate mediator is MockDataTypes.xsd. In addition, a resource is used to import the 08SOAPFaults schema which is referred in the 08MockServiceSchema schema. Note that the value ../Common/SOAPFaults.xsd which is specified as the location for the schema to be imported is the same as the location specified for 08SOAPFaults schema in the 08MockServiceSchema configuration.

The on-fail sequence of this Validate mediator includes a Log mediator which is added as a child to the Validate mediator. This log mediator uses two properties to generate the error message Validation failed ### when the validation of a message against the schemas specified fails.

Excerpt
hiddentrue

Description of the Validate Mediator in WSO2 ESB.