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)
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]")
def decorated(value): ensure(value, "%s is empty" % safe_repr(value)) return _validator(value)
def valid_names(self): return [safe_repr(obj) for obj in self.valid_types]
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')
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