def __call__(self, v): """ :param v: is a number enclosed with string :return: Decimal number """ precision, scale, decimal_num = self._get_precision_scale(v) if self.precision is not None and self.scale is not None and\ precision != self.precision and scale != self.scale: raise Invalid( self.msg or "Precision must be equal to %s, and Scale must be equal to %s" % (self.precision, self.scale)) else: if self.precision is not None and precision != self.precision: raise Invalid( self.msg or "Precision must be equal to %s" % self.precision) if self.scale is not None and scale != self.scale: raise Invalid(self.msg or "Scale must be equal to %s" % self.scale) if self.yield_decimal: return decimal_num else: return v
def __call__(self, v): if not isinstance(v, (list, tuple)): raise Invalid(self.msg or 'Value {} is not sequence!'.format(v)) if len(v) != len(self._schemas): raise Invalid(self.msg or 'List lengths differ, value:{} != target:{}'.format(len(v), len(self._schemas))) consumed = set() missing = [] for index, value in enumerate(v): found = False for i, s in enumerate(self._schemas): if i in consumed: continue try: s(value) except Invalid: pass else: found = True consumed.add(i) break if not found: missing.append((index, value)) if len(missing) == 1: el = missing[0] raise Invalid(self.msg or 'Element #{} ({}) is not valid against any validator'.format(el[0], el[1])) elif missing: raise MultipleInvalid([Invalid(self.msg or 'Element #{} ({}) is not valid against any validator'.format( el[0], el[1])) for el in missing]) return v
class Any(CompoundValidator): """ Combines multiple validators together, raising an exception only if they all fail (i.e. validation succeeds if any validator passes). """ def __init__(self, *args, **kw): self.validators = args self.messages = kw.get('messages') def __call__(self, v): exceptions = [] for validator in self.validators: try: validator(v) except Invalid, e: exceptions.append(Invalid(e.message, e.exceptions, validator)) else: return message = '; '.join(e.message for e in exceptions) _messages = { 'please-fix': "Please fix any of: %(errors)s", } if self.messages: _messages.update(messages) raise Invalid(_messages['please-fix'] % {'errors': message}, exceptions, self)
def __call__(self, v): exceptions = [] for validator in self.validators: try: validator(v) except Invalid, e: exceptions.append(Invalid(e.message, e.exceptions, validator))
def __call__(self, v): if v != self.target: raise Invalid( self.msg or 'Values are not equal: value:{} != target:{}'.format( v, self.target)) return v
def __call__(self, v): try: validate.has_length(v, min=self.min, max=self.max, messages=self.messages) except Invalid, e: raise Invalid(e.message, validator=self)
def __call__(self, v): try: validate.is_in_range(v, min=self.min, max=self.max, messages=self.messages) except Invalid, e: raise Invalid(e.message, validator=self)
def __call__(self, v): try: validate.is_url(v, full=self.full, absolute=self.absolute, relative=self.relative, messages=self.messages) except Invalid, e: raise Invalid(e.message, validator=self)
def _get_precision_scale(self, number): """ :param number: :return: tuple(precision, scale, decimal_number) """ try: decimal_num = Decimal(number) except InvalidOperation: raise Invalid(self.msg or 'Value must be a number enclosed with string') return (len(decimal_num.as_tuple().digits), -(decimal_num.as_tuple().exponent), decimal_num)
def __call__(self, v): try: set_v = set(v) except TypeError as e: raise TypeInvalid(self.msg or 'contains unhashable elements: {0}'.format(e)) if len(set_v) != len(v): seen = set() dupes = list(set(x for x in v if x in seen or seen.add(x))) raise Invalid(self.msg or 'contains duplicate items: {0}'.format(dupes)) return v
class All(CompoundValidator): """ Combines multiple validators together, raising an exception unless they all pass """ def __init__(self, *args): self.validators = args def __call__(self, v): exceptions = [] for validator in self.validators: try: validator(v) except Invalid, e: exceptions.append(Invalid(e.message, e.exceptions, validator)) if len(exceptions): message = '; '.join(e.message for e in exceptions) raise Invalid(message, exceptions, self)
def __call__(self, v): try: validate.is_domain_name(v, messages=self.messages) except Invalid, e: raise Invalid(e.message, validator=self)
def __call__(self, v): try: validate.is_plaintext(v, extra=self.extra, messages=self.messages) except Invalid, e: raise Invalid(e.message, validator=self)
def __call__(self, v): try: validate.is_string(v) except Invalid, e: raise Invalid(e.message, validator=self)
def __call__(self, v): try: validate.is_required(v, messages=self.messages) except Invalid, e: raise Invalid(e.message, validator=self)
def __call__(self, v): if v is not None and not isinstance(v, self.kind): raise Invalid(self.msg or "%s must be None or of type %s" % (v, self.kind)) return v
def __call__(self, v): try: validate.is_one_of(v, self.set_of_values, messages=self.messages) except Invalid, e: raise Invalid(e.message, validator=self)
def __call__(self, v): try: validate.is_equal(v, self.compared_to, messages=self.messages) except Invalid, e: raise Invalid(e.message, validator=self)