Exemple #1
0
def SoappySanityCheck(soappy_service, obj, ns, obj_type, max_occurs='1'):
    """Validates any given object against its WSDL definition.

  This method considers None and the empty string to be a valid representation
  of any type.

  Args:
    soappy_service: SOAPpy.WSDL.Proxy An object encapsulating a SOAP service.
    obj: Object to be validated. Depending on the WSDL-defined type this object
         represents, the data type will vary. It should always be either a
         dictionary, list, or string no matter what WSDL-defined type it is.
    ns: string The namespace the given type belongs to.
    obj_type: A string specifying the type name defined in the WSDL.
    max_occurs: string The maxOccurs attribute for this object.

  Raises:
    ValidationError: The given type has no definition in the WSDL or the given
                     object is not a valid representation of the type defined in
                     the WSDL.
  """
    if obj in (None, ''):
        return
    elif Utils.IsBaseSoapType(obj_type):
        try:
            ValidateTypes(((obj, (str, unicode)), ))
        except ValidationError:
            raise ValidationError(
                'Objects of type \'%s\' should be a string but '
                'value \'%s\' is a \'%s\' instead.' %
                (obj_type, obj, type(obj)))
    else:
        try:
            soap_type = soappy_service.wsdl.types[ns].types[obj_type].tag
            if soap_type == 'simpleType':
                _SoappySanityCheckSimpleType(obj, obj_type)
            elif soap_type == 'complexType':
                if (SoappyUtils.IsAnArrayType(obj_type, ns, soappy_service)
                        or not max_occurs.isdigit() or int(max_occurs) > 1):
                    _SoappySanityCheckArray(soappy_service, obj, ns, obj_type)
                else:
                    _SoappySanityCheckComplexType(soappy_service, obj, ns,
                                                  obj_type)
            else:
                raise ValidationError(
                    'Unrecognized type definition tag in WSDL: \'%s\'' %
                    soap_type)
        except KeyError:
            raise ValidationError(
                'This type is not defined in the WSDL: \'%s\'' % obj_type)
Exemple #2
0
def _SoappySanityCheckArray(soappy_service, obj, ns, type_name):
    """Validates a list representing an array type against its WSDL definition.

  Args:
    soappy_service: SOAPpy.WSDL.Proxy The SOAPpy service object containing the
                    descriptions of all WSDL-defined types.
    obj: Object to be validated. Depending on the WSDL-defined type this object
         represents, the data type will vary. It should always be either a
         dictionary, list, or string no matter what WSDL-defined type it is.
    ns: string The namespace the given type belongs to.
    type_name: A string specifying the type name defined in the WSDL.

  Raises:
    ValidationError if the given object is not a valid representation of the
    given list type.
  """
    try:
        ValidateTypes(((obj, list), ))
    except ValidationError:
        raise ValidationError('Type \'%s\' should be a list but value '
                              '\'%s\' is a \'%s\' instead.' %
                              (type_name, obj, type(obj)))
    if Utils.IsBaseSoapType(type_name):
        for item in obj:
            if item is None: continue
            try:
                ValidateTypes(((item, (str, unicode)), ))
            except ValidationError:
                raise ValidationError(
                    'The items in array \'%s\' must all be strings. '
                    'Value \'%s\' is of type \'%s\'.' %
                    (type_name, item, type(item)))
    else:
        for item in obj:
            if item is None: continue
            SoappySanityCheck(
                soappy_service, item, ns,
                SoappyUtils.GetArrayItemTypeName(type_name, ns,
                                                 soappy_service))