Esempio n. 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)
Esempio n. 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
Esempio n. 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)
Esempio n. 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)
Esempio n. 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)
Esempio n. 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)
Esempio n. 7
0
    def test_is_delayed(self):
        @delay
        def my_schema():
            return 'a', 'b'

        assert utils.expand_schema(my_schema) == ('a', 'b')
Esempio n. 8
0
 def test_not_delayed(self):
     schema = ('a', 'b')
     assert utils.expand_schema(schema) == schema
Esempio n. 9
0
 def __call__(self, data, tree):
     schema = expand_schema(self.schema)
     validator = RecursiveValidator(data, schema, tree)
     validator.validate()
Esempio n. 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()
Esempio n. 11
0
    def test_is_delayed(self):
        @delay
        def my_schema():
            return "a", "b"

        assert utils.expand_schema(my_schema) == ("a", "b")
Esempio n. 12
0
 def test_not_delayed(self):
     schema = ("a", "b")
     assert utils.expand_schema(schema) == schema
Esempio n. 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()
Esempio n. 14
0
 def test_is_delayed(self):
     @delay
     def my_schema():
         return 'a', 'b'
     assert utils.expand_schema(my_schema) == ('a', 'b')
Esempio n. 15
0
 def test_not_delayed(self):
     schema = ('a', 'b')
     assert utils.expand_schema(schema) == schema