Exemplo n.º 1
0
def test_get_func_name():
    def foo(a, b):
        return a + b

    assert utils.get_func_name(foo) == 'foo'
    # Works for partials, too
    foo_partial = partial(foo, 1)

    assert utils.get_func_name(foo_partial) == 'foo'
Exemplo n.º 2
0
def test_get_func_name():
    def foo(a, b):
        return a + b

    assert utils.get_func_name(foo) == 'foo'
    # Works for partials, too
    foo_partial = partial(foo, 1)

    assert utils.get_func_name(foo_partial) == 'foo'
Exemplo n.º 3
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
Exemplo n.º 4
0
 def _validate(self, value):
     """Perform validation on ``value``. Raise a :exc:`ValidationError` if validation
     does not succeed.
     """
     errors = []
     for validator in self.validators:
         func_name = utils.get_func_name(validator)
         msg = u'Validator {0}({1}) is False'.format(
             func_name, value
         )
         try:
             if validator(value) is False:
                 raise ValidationError(getattr(self, 'error', None) or msg)
         except ValidationError as err:
             errors.extend(err.messages)
     if errors:
         raise ValidationError(errors)
Exemplo n.º 5
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
Exemplo n.º 6
0
 def _validate(self, validators, output, fields_dict, strict=False):
     """Perform schema-level validation. Stores errors if ``strict`` is `False`.
     """
     for validator_func in validators:
         try:
             if validator_func(output) is False:
                 func_name = utils.get_func_name(validator_func)
                 raise ValidationError(u'Schema validator {0}({1}) is False'.format(
                     func_name, dict(output)
                 ))
         except ValidationError as err:
             # Store or reraise errors
             if err.field:
                 field_name = err.field
                 field_obj = fields_dict[field_name]
             else:
                 field_name = '_schema'
                 field_obj = None
             if strict:
                 raise UnmarshallingError(err, field=field_obj, field_name=field_name)
             self.errors.setdefault(field_name, []).append(text_type(err))
     return output