Is there way to validate payload with type-checking with zeep? Zeep checks for missing elements but sends payload even when type is incorrect.
Example, I have element:
<element name="order_date" type="dateTime" minOccurs="1" maxOccurs="1"/>
When I make send wrong type, ex '1.0'
for that field, zeep still accepts it. Later on I get zeep.exceptions.Fault
back because SOAP server rejects my requests because of this wrong type as it has to be date in ISO format
I have kind of find a way to do it by pre-creating message and then validating it with schema provided in wsdl
file with lxml
library. This works well but I am not sure if I am using zeep
in correct way, maybe it also supports it out of the box ? I have went through source code and I didn't find too much. I know that zeep
uses type checking to translate SOAP responses back to Python but not sure about pre-validation.
My current way to deal with this:
# There are some missing variables, don't want to CP everything
from lxml import etree as ET
def get_xmlschema():
wsdl_file = ...
wsdl_tree = ET.fromstring(wsdl_file)
return ET.XMLSchema(wsdl_tree.find('.//schema:schema', namespaces=NAMESPACES))
def prevalidate_soap_message(client, operation, *args, **kwargs):
# zeep checks only required elements and doesn't perform type checking
msg = client.create_message(client.service, operation, *args, **kwargs)
payload = msg.find('*/')
get_xmlschema().assertValid(payload)
And this is how I send my payload for context:
client = zeep.Client(wsdl)
operation = client.service[operation_name]
payload = ... # this is python object with structure as in wsdl schema for that operation
result = operation(payload) # it raises ValidationError if elements are missing but not for types