Esempio n. 1
0
 def _call_or_raise(self, func, value, attr):
     if len(utils.get_func_args(func)) > 1:
         if self.parent.context is None:
             msg = 'No context available for Function field {0!r}'.format(attr)
             raise ValidationError(msg)
         return func(value, self.parent.context)
     else:
         return func(value)
Esempio n. 2
0
 def _serialize(self, value, attr, obj):
     try:
         if len(utils.get_func_args(self.func)) > 1:
             if self.parent.context is None:
                 msg = 'No context available for Function field {0!r}'.format(attr)
                 raise ValidationError(msg)
             return self.func(obj, self.parent.context)
         else:
             return self.func(obj)
     except AttributeError:  # the object is not expected to have the attribute
         pass
Esempio n. 3
0
 def _super_serialize(self, value, attr, obj):
     """ This is essentially copying the functionality from the Marshmallow Method._serialize function,
     but we drop the AttributeError catches. """
     method = utils.callable_or_raise(getattr(self.parent, self.method_name, None))
     if len(utils.get_func_args(method)) > 2:
         if self.parent.context is None:
             msg = 'No context available for Method field {0!r}'.format(attr)
             raise MarshallingError(msg)
         return method(obj, self.parent.context)
     else:
         return method(obj)
Esempio n. 4
0
 def _serialize(self, value, attr, obj):
     try:
         method = utils.callable_or_raise(getattr(self.parent, self.method_name, None))
         if len(utils.get_func_args(method)) > 2:
             if self.parent.context is None:
                 msg = 'No context available for Method field {0!r}'.format(attr)
                 raise MarshallingError(msg)
             return method(obj, self.parent.context)
         else:
             return method(obj)
     except AttributeError:
         pass
Esempio n. 5
0
 def _serialize(self, value, attr, obj):
     try:
         if len(utils.get_func_args(self.func)) > 1:
             if self.parent.context is None:
                 msg = 'No context available for Function field {0!r}'.format(attr)
                 raise ValidationError(msg)
             return self.func(obj, self.parent.context)
         else:
             return self.func(obj)
     except AttributeError:  # the object is not expected to have the attribute
         pass
     return missing_
Esempio n. 6
0
def test_get_func_args():
    def f1(foo, bar):
        pass

    f2 = partial(f1, 'baz')

    class F3(object):
        def __call__(self, foo, bar):
            pass
    f3 = F3()

    for func in [f1, f2, f3]:
        assert utils.get_func_args(func) == ['foo', 'bar']
Esempio n. 7
0
 def _super_serialize(self, value, attr, obj):
     """ This is essentially copying the functionality from the Marshmallow Function._serialize function,
     but we drop the AttributeError catches. """
     try:
         if len(utils.get_func_args(self.func)) > 1:
             if self.parent.context is None:
                 msg = 'No context available for Function field {0!r}'.format(attr)
                 raise MarshallingError(msg)
             return self.func(obj, self.parent.context)
         else:
             return self.func(obj)
     except TypeError as te:  # Function is not callable
         raise MarshallingError(te)
Esempio n. 8
0
def test_get_func_args():
    def f1(self, foo, bar):
        pass

    f2 = partial(f1, 'baz')

    class F3(object):
        def __call__(self, foo, bar):
            pass
    f3 = F3()

    for func in [f1, f2, f3]:
        assert utils.get_func_args(func) == ['self', 'foo', 'bar']
Esempio n. 9
0
def test_get_func_args():
    def f1(foo, bar):
        pass

    f2 = partial(f1, "baz")

    class F3:
        def __call__(self, foo, bar):
            pass

    f3 = F3()

    for func in [f1, f2, f3]:
        assert utils.get_func_args(func) == ["foo", "bar"]
Esempio n. 10
0
 def _validate(self,
               validators,
               output,
               raw_data,
               fields_dict,
               strict=False):
     """Perform schema-level validation. Stores errors if ``strict`` is `False`.
     """
     for validator_func in validators:
         try:
             func_args = utils.get_func_args(validator_func)
             if len(func_args) < 3:
                 res = validator_func(output)
             else:
                 res = validator_func(output, raw_data)
             if res is False:
                 func_name = utils.get_func_name(validator_func)
                 raise ValidationError(
                     'Schema validator {0}({1}) is False'.format(
                         func_name, dict(output)))
         except ValidationError as err:
             # Store or reraise errors
             if err.field_names:
                 field_names = err.field_names
                 field_objs = [fields_dict[each] for each in field_names]
             else:
                 field_names = ['_schema']
                 field_objs = []
             for field_name in field_names:
                 if isinstance(err.messages, (list, tuple)):
                     # self.errors[field_name] may be a dict if schemas are nested
                     if isinstance(self.errors.get(field_name), dict):
                         self.errors[field_name].setdefault(
                             '_schema', []).extend(err.messages)
                     else:
                         self.errors.setdefault(field_name,
                                                []).extend(err.messages)
                 elif isinstance(err.messages, dict):
                     self.errors.setdefault(field_name,
                                            []).append(err.messages)
                 else:
                     self.errors.setdefault(field_name,
                                            []).append(text_type(err))
             if strict:
                 raise ValidationError(self.errors,
                                       fields=field_objs,
                                       field_names=field_names)
     return output
Esempio n. 11
0
 def _validate(self, validators, output, raw_data, fields_dict, strict=False):
     """Perform schema-level validation. Stores errors if ``strict`` is `False`.
     """
     for validator_func in validators:
         try:
             func_args = utils.get_func_args(validator_func)
             if len(func_args) < 3:
                 res = validator_func(output)
             else:
                 res = validator_func(output, raw_data)
             if res is False:
                 func_name = utils.get_func_name(validator_func)
                 raise ValidationError('Schema validator {0}({1}) is False'.format(
                     func_name, dict(output)
                 ))
         except ValidationError as err:
             # Store or reraise errors
             if err.field_names:
                 field_names = err.field_names
                 field_objs = [fields_dict[each] for each in field_names]
             else:
                 field_names = ['_schema']
                 field_objs = []
             for field_name in field_names:
                 if isinstance(err.messages, (list, tuple)):
                     # self.errors[field_name] may be a dict if schemas are nested
                     if isinstance(self.errors.get(field_name), dict):
                         self.errors[field_name].setdefault(
                             '_schema', []
                         ).extend(err.messages)
                     else:
                         self.errors.setdefault(field_name, []).extend(err.messages)
                 elif isinstance(err.messages, dict):
                     self.errors.setdefault(field_name, []).append(err.messages)
                 else:
                     self.errors.setdefault(field_name, []).append(text_type(err))
             if strict:
                 raise ValidationError(
                     self.errors,
                     fields=field_objs,
                     field_names=field_names
                 )
     return output