Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raise SyntaxError('%s is not a %s' % (key, kwtype))
def is_valid(self, value):
if not self.is_active:
return None
if not self._is_valid(value):
return self._fail(value)
return None
def _fail(self, value):
return '\'%s\' violates %s.' % (value, self.__class__.__name__)
class Min(Constraint):
fail = '%s is less than %s'
def __init__(self, value_type, kwargs):
self.keywords = {'min': value_type}
super(Min, self).__init__(value_type, kwargs)
def _is_valid(self, value):
return self.min <= value
def _fail(self, value):
return self.fail % (value, self.min)
class Max(Constraint):
fail = '%s is greater than %s'
def _fail(self, value):
return self.fail % (value, self.min)
class LengthMax(Constraint):
keywords = {'max': int}
fail = 'Length of %s is greater than %s'
def _is_valid(self, value):
return self.max >= len(value)
def _fail(self, value):
return self.fail % (value, self.max)
class CharacterExclude(Constraint):
keywords = {'exclude': str}
fail = '\'%s\' contains excluded character \'%s\''
def _is_valid(self, value):
for char in self.exclude:
if char in value:
self._failed_char = char
return False
return True
def _fail(self, value):
return self.fail % (value, self._failed_char)
class IpVersion(Constraint):
keywords = {'version': int}
class Max(Constraint):
fail = '%s is greater than %s'
def __init__(self, value_type, kwargs):
self.keywords = {'max': value_type}
super(Max, self).__init__(value_type, kwargs)
def _is_valid(self, value):
return self.max >= value
def _fail(self, value):
return self.fail % (value, self.max)
class LengthMin(Constraint):
keywords = {'min': int}
fail = 'Length of %s is less than %s'
def _is_valid(self, value):
return self.min <= len(value)
def _fail(self, value):
return self.fail % (value, self.min)
class LengthMax(Constraint):
keywords = {'max': int}
fail = 'Length of %s is greater than %s'
def _is_valid(self, value):
return self.max >= len(value)
class CharacterExclude(Constraint):
keywords = {'exclude': str}
fail = '\'%s\' contains excluded character \'%s\''
def _is_valid(self, value):
for char in self.exclude:
if char in value:
self._failed_char = char
return False
return True
def _fail(self, value):
return self.fail % (value, self._failed_char)
class IpVersion(Constraint):
keywords = {'version': int}
fail = 'IP version of %s is not %s'
def _is_valid(self, value):
try:
import ipaddress
except ImportError:
raise ImportError("You must install the ipaddress backport in Py2")
try:
ip = ipaddress.ip_interface(to_unicode(value))
except ValueError:
return False
return self.version == ip.version
def _fail(self, value):
return self.fail % (value, self.version)
def _fail(self, value):
return self.fail % (value, self.max)
class LengthMin(Constraint):
keywords = {'min': int}
fail = 'Length of %s is less than %s'
def _is_valid(self, value):
return self.min <= len(value)
def _fail(self, value):
return self.fail % (value, self.min)
class LengthMax(Constraint):
keywords = {'max': int}
fail = 'Length of %s is greater than %s'
def _is_valid(self, value):
return self.max >= len(value)
def _fail(self, value):
return self.fail % (value, self.max)
class CharacterExclude(Constraint):
keywords = {'exclude': str}
fail = '\'%s\' contains excluded character \'%s\''
def _is_valid(self, value):
for char in self.exclude:
class Min(Constraint):
fail = '%s is less than %s'
def __init__(self, value_type, kwargs):
self.keywords = {'min': value_type}
super(Min, self).__init__(value_type, kwargs)
def _is_valid(self, value):
return self.min <= value
def _fail(self, value):
return self.fail % (value, self.min)
class Max(Constraint):
fail = '%s is greater than %s'
def __init__(self, value_type, kwargs):
self.keywords = {'max': value_type}
super(Max, self).__init__(value_type, kwargs)
def _is_valid(self, value):
return self.max >= value
def _fail(self, value):
return self.fail % (value, self.max)
class LengthMin(Constraint):
keywords = {'min': int}
fail = 'Length of %s is less than %s'