예제 #1
0
파일: element.py 프로젝트: speker/core
    def validate(self, value, render_path=None):
        """Validate that the value is valid"""
        if self.accepts_multiple and isinstance(value, list):

            # Validate bounds
            if len(value) < self.min_occurs:
                raise exceptions.ValidationError(
                    "Expected at least %d items (minOccurs check) %d items found."
                    % (self.min_occurs, len(value)),
                    path=render_path,
                )
            elif self.max_occurs != "unbounded" and len(
                    value) > self.max_occurs:
                raise exceptions.ValidationError(
                    "Expected at most %d items (maxOccurs check) %d items found."
                    % (self.max_occurs, len(value)),
                    path=render_path,
                )

            for val in value:
                self._validate_item(val, render_path)
        else:
            if not self.is_optional and not self.nillable and value in (
                    None, NotSet):
                raise exceptions.ValidationError("Missing element %s" %
                                                 (self.name),
                                                 path=render_path)

            self._validate_item(value, render_path)
예제 #2
0
    def _validate_item(self, value, render_path):
        if value is None:  # can be an lxml element
            return

        # Check if we received a proper value object. If we receive the wrong
        # type then return a nice error message
        if self.restrict:
            expected_types = (etree._Element, dict) + self.restrict.accepted_types
        else:
            expected_types = (etree._Element, dict, AnyObject)

        if value in (None, NotSet):
            if not self.is_optional:
                raise exceptions.ValidationError(
                    "Missing element %s" % (self.name), path=render_path
                )

        elif not isinstance(value, expected_types):
            type_names = ["%s.%s" % (t.__module__, t.__name__) for t in expected_types]
            err_message = "Any element received object of type %r, expected %s" % (
                type(value).__name__,
                " or ".join(type_names),
            )

            raise TypeError(
                "\n".join(
                    (
                        err_message,
                        "See http://docs.python-zeep.org/en/master/datastructures.html"
                        "#any-objects for more information",
                    )
                )
            )
예제 #3
0
파일: attribute.py 프로젝트: speker/core
 def validate(self, value, render_path):
     try:
         self.type.validate(value, required=self.required)
     except exceptions.ValidationError as exc:
         raise exceptions.ValidationError(
             "The attribute %s is not valid: %s" % (self.qname, exc.message),
             path=render_path,
         )
예제 #4
0
파일: element.py 프로젝트: speker/core
    def _validate_item(self, value, render_path):
        if self.nillable and value in (None, NotSet):
            return

        try:
            self.type.validate(value, required=True)
        except exceptions.ValidationError as exc:
            raise exceptions.ValidationError(
                "The element %s is not valid: %s" % (self.qname, exc.message),
                path=render_path,
            )
예제 #5
0
    def validate(self, value, render_path):
        if self.accepts_multiple and isinstance(value, list):

            # Validate bounds
            if len(value) < self.min_occurs:
                raise exceptions.ValidationError(
                    "Expected at least %d items (minOccurs check)" % self.min_occurs
                )
            if self.max_occurs != "unbounded" and len(value) > self.max_occurs:
                raise exceptions.ValidationError(
                    "Expected at most %d items (maxOccurs check)" % self.min_occurs
                )

            for val in value:
                self._validate_item(val, render_path)
        else:
            if not self.is_optional and value in (None, NotSet):
                raise exceptions.ValidationError("Missing element for Any")

            self._validate_item(value, render_path)