Exemple #1
0
 def safe_type(self, data, tree):
     """
     Make sure that the incoming data complies with the class type we
     are expecting it to be. In this case, classes that inherit from this
     base class expect data to be of type ``list``.
     """
     if not isinstance(data, list):
         name = self.__class__.__name__
         msg = "did not pass validation against callable: %s" % name
         reason = 'expected a list but got %s' % safe_repr(data)
         raise Invalid(self.schema, tree, reason=reason, pair='value', msg=msg)
Exemple #2
0
 def safe_type(self, data, tree):
     """
     Make sure that the incoming data complies with the class type we
     are expecting it to be. In this case, classes that inherit from this
     base class expect data to be of type ``list``.
     """
     if not isinstance(data, list):
         name = self.__class__.__name__
         msg = "did not pass validation against callable: %s" % name
         reason = 'expected a list but got %s' % safe_repr(data)
         raise Invalid(self.schema,
                       tree,
                       reason=reason,
                       pair='value',
                       msg=msg)
Exemple #3
0
def not_empty(_object):
    """
    Validates the given input (has to be a valid data structure) is empty.
    Input *has* to be one of: `list`, `dict`, or `string`.

    It is specially useful when most of the validators being created are
    dealing with data structures that should not be empty.
    """
    if is_callable(_object):
        _validator = _object

        @wraps(_validator)
        @instance_of()
        def decorated(value):
            ensure(value, "%s is empty" % safe_repr(value))
            return _validator(value)
        return decorated
    try:
        ensure(len(_object), "%s is empty" % safe_repr(_object))
    except TypeError:
        raise AssertionError("not of any valid types: [list, dict, str]")
Exemple #4
0
def not_empty(_object):
    """
    Validates the given input (has to be a valid data structure) is empty.
    Input *has* to be one of: `list`, `dict`, or `string`.

    It is specially useful when most of the validators being created are
    dealing with data structures that should not be empty.
    """
    if is_callable(_object):
        _validator = _object

        @wraps(_validator)
        @instance_of()
        def decorated(value):
            ensure(value, "%s is empty" % safe_repr(value))
            return _validator(value)

        return decorated
    try:
        ensure(len(_object), "%s is empty" % safe_repr(_object))
    except TypeError:
        raise AssertionError("not of any valid types: [list, dict, str]")
Exemple #5
0
 def decorated(value):
     ensure(value, "%s is empty" % safe_repr(value))
     return _validator(value)
Exemple #6
0
 def valid_names(self):
     return [safe_repr(obj) for obj in self.valid_types]
Exemple #7
0
 def data_sanity(self, data, tree=None):
     if not isinstance(data, list):
         name = self.name or 'IterableValidator'
         reason = 'expected a list but got %s' % safe_repr(data)
         msg = 'did not pass validation against callable: %s' % name
         raise Invalid('', tree or [], msg=msg, reason=reason, pair='value')
Exemple #8
0
 def decorated(value):
     ensure(value, "%s is empty" % safe_repr(value))
     return _validator(value)
Exemple #9
0
 def valid_names(self):
     return [safe_repr(obj)
             for obj in self.valid_types]
Exemple #10
0
 def data_sanity(self, data, tree=None):
     if not isinstance(data, list):
         name = self.name or 'IterableValidator'
         reason = 'expected a list but got %s' % safe_repr(data)
         msg = 'did not pass validation against callable: %s' % name
         raise Invalid('', tree or [], msg=msg, reason=reason, pair='value')
Exemple #11
0
 def __init__(self, *schemas):
     for schema in schemas:
         if not is_schema(schema):
             raise TypeError("got a non schema argument: %s" % safe_repr(schema))
     self.schemas = schemas