The Validate mediator validates XML messages against a specified schema. You can specify an XPath to extract and validate a specific part of the message. Otherwise, the mediator validates the first child of the SOAP body of the current message. 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. |
Code Block |
---|
| <validate [source="xpath"]>
<property name="validation-feature-id" value="true|false"/>*
<schema key="string"/>+
<on-fail>
mediator+
</on-fail>
</validate> |
The mediator configuration can be divided into the following sections. This section is used to specify the key to access the main schema based on which 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 Name | Description |
---|
Source | The XPath expression to extract the XML that needs to be validated. The Validate mediator validates the evaluation of this expression 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 |
---|
| 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. |
| Enable Cache Schema | This check box is enabled by default to ensure that schemas retrieved from the registry for one service/REST API are cached for future use. Tip |
---|
| Be sure to disable this check box if you are using the Validate mediator inside a Template. Since multiple proxy services/REST APIs will be accessing one template, schemas that are cached for one service can interrupt another service that uses the same template. |
| Schema keys defined for Validate Mediator table | The key for the schema location. It can be specified using one of the following methods. - If the key is a static 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 required 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 |
---|
| 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. |
|
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. Parameter Name | Description |
---|
Feature Name | The name of the feature. | Value | Click True to enable the feature, or click False to disable the feature. | Action | Click Delete in the relevant row to delete a feature. |
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 Name | Description |
---|
Location | The 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. | Key | The 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. |
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 creates a SOAP fault to be sent back to the party which sent the message. Code Block |
---|
| <validate cache-schema="true">
<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>
|
In this 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 |
---|
| <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 |
---|
| <validate cache-schema="true">
<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 08 SOAPFaults 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 08 SOAPFaults 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. |