Example #1
0
    def __call__(self, data, tree):
        """
        Go through each item in data against the first schema in the arguments.
        If that fails try each subsequent schema until it passes. Even if
        schemas are failing to apply to that given item, consume all the
        available ones until at least one passes.

        If nothing passes, the last error is raised.

        :param data: Incoming data from the validator engine in normalized dict
        form.
        :param tree: The traversing tree up to this point, always passed in.
        :raises Invalid: If none of the schemas can validate the data.
        """
        first_schema = expand_schema(self.schemas[0])
        index = len(data) - 1
        validator = IterableValidator(data,
                                      first_schema,
                                      tree,
                                      index=index,
                                      name='MultiIterable')

        for item_index in range(len(data)):
            try:
                validator.leaf(item_index)
            except (SchemaError, Invalid):
                self.itemized_validation(validator, item_index)
Example #2
0
    def itemized_validation(self, validator, item_index):
        error = None

        for schema in self.schemas:
            try:
                validator.schema = expand_schema(schema)
                validator.tree = []
                return validator.leaf(item_index)
            except Invalid as e:
                error = e

        if error is not None:
            raise error
Example #3
0
    def __call__(self, data, tree):
        schema = expand_schema(self.schema)
        index = len(data) - 1
        validator = RecursiveValidator(data, schema, [], index=index)
        for item_index in range(len(data)):
            try:
                return validator.leaf(item_index)
            except Invalid:
                if tree:
                    tree.pop
                pass

        msg = "did not contain any valid objects against callable: %s" % self.__class__.__name__
        raise Invalid(schema, tree, pair='value', msg=msg)
Example #4
0
    def __call__(self, data, tree):
        schema = expand_schema(self.schema)
        self.safe_type(data, tree)
        index = len(data) - 1
        validator = IterableValidator(data, schema, [], index=index, name='AnyItem')
        for item_index in range(len(data)):
            try:
                return validator.leaf(item_index)
            except Invalid:
                pass

        tree.append('list[]')
        if is_callable(schema):
            msg = "did not contain any valid items against callable: %s" % schema.__name__
        else:
            msg = "did not contain any valid items matching %s" % repr(schema)
        raise Invalid(schema, tree, pair='value', msg=msg)
Example #5
0
    def __call__(self, data, tree):
        schema = expand_schema(self.schema)
        self.safe_type(data, tree)
        index = len(data) - 1
        validator = IterableValidator(data,
                                      schema, [],
                                      index=index,
                                      name='AnyItem')
        for item_index in range(len(data)):
            try:
                return validator.leaf(item_index)
            except Invalid:
                pass

        tree.append('list[]')
        if is_callable(schema):
            msg = "did not contain any valid items against callable: %s" % schema.__name__
        else:
            msg = "did not contain any valid items matching %s" % repr(schema)
        raise Invalid(schema, tree, pair='value', msg=msg)
Example #6
0
    def __call__(self, data, tree):
        """
        Go through each item in data against the first schema in the arguments.
        If that fails try each subsequent schema until it passes. Even if
        schemas are failing to apply to that given item, consume all the
        available ones until at least one passes.

        If nothing passes, the last error is raised.

        :param data: Incoming data from the validator engine in normalized dict
        form.
        :param tree: The traversing tree up to this point, always passed in.
        :raises Invalid: If none of the schemas can validate the data.
        """
        first_schema = expand_schema(self.schemas[0])
        index = len(data) - 1
        validator = RecursiveValidator(data, first_schema, [], index=index)
        for item_index in range(len(data)):
            try:
                validator.leaf(item_index)
            except Invalid:
                self.itemized_validation(validator, item_index)
Example #7
0
    def test_is_delayed(self):
        @delay
        def my_schema():
            return 'a', 'b'

        assert utils.expand_schema(my_schema) == ('a', 'b')
Example #8
0
 def test_not_delayed(self):
     schema = ('a', 'b')
     assert utils.expand_schema(schema) == schema
Example #9
0
 def __call__(self, data, tree):
     schema = expand_schema(self.schema)
     validator = RecursiveValidator(data, schema, tree)
     validator.validate()
Example #10
0
 def __call__(self, data, tree):
     schema = expand_schema(self.schema)
     self.safe_type(data, tree)
     validator = IterableValidator(data, schema, tree, name='AllItems')
     validator.validate()
Example #11
0
    def test_is_delayed(self):
        @delay
        def my_schema():
            return "a", "b"

        assert utils.expand_schema(my_schema) == ("a", "b")
Example #12
0
 def test_not_delayed(self):
     schema = ("a", "b")
     assert utils.expand_schema(schema) == schema
Example #13
0
 def __call__(self, data, tree):
     schema = expand_schema(self.schema)
     self.safe_type(data, tree)
     validator = IterableValidator(data, schema, tree, name='AllItems')
     validator.validate()
Example #14
0
 def test_is_delayed(self):
     @delay
     def my_schema():
         return 'a', 'b'
     assert utils.expand_schema(my_schema) == ('a', 'b')
Example #15
0
 def test_not_delayed(self):
     schema = ('a', 'b')
     assert utils.expand_schema(schema) == schema