Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from datetime import date, datetime
from .base import Validator
from . import constraints as con
from .. import util
# ABCs for containers were moved to their own module
try:
from collections.abc import Sequence, Mapping
except ImportError:
from collections import Sequence, Mapping
class String(Validator):
"""String validator"""
tag = 'str'
constraints = [con.LengthMin, con.LengthMax, con.CharacterExclude]
def _is_valid(self, value):
return util.isstr(value)
class Number(Validator):
"""Number/float validator"""
value_type = float
tag = 'num'
constraints = [con.Min, con.Max]
def _is_valid(self, value):
return isinstance(value, (int, float))
class Integer(Validator):
class Number(Validator):
"""Number/float validator"""
value_type = float
tag = 'num'
constraints = [con.Min, con.Max]
def _is_valid(self, value):
return isinstance(value, (int, float))
class Integer(Validator):
"""Integer validator"""
value_type = int
tag = 'int'
constraints = [con.Min, con.Max]
def _is_valid(self, value):
return isinstance(value, int)
class Boolean(Validator):
"""Boolean validator"""
tag = 'bool'
def _is_valid(self, value):
return isinstance(value, bool)
class Enum(Validator):
"""Enum validator"""
tag = 'enum'
class Map(Validator):
"""Map and dict validator"""
tag = 'map'
def __init__(self, *args, **kwargs):
super(Map, self).__init__(*args, **kwargs)
self.validators = [val for val in args if isinstance(val, Validator)]
def _is_valid(self, value):
return isinstance(value, Mapping)
class List(Validator):
"""List validator"""
tag = 'list'
constraints = [con.LengthMin, con.LengthMax]
def __init__(self, *args, **kwargs):
super(List, self).__init__(*args, **kwargs)
self.validators = [val for val in args if isinstance(val, Validator)]
def _is_valid(self, value):
return isinstance(value, Sequence) and not util.isstr(value)
class Include(Validator):
"""Include validator"""
tag = 'include'
def __init__(self, *args, **kwargs):
self.include_name = args[0]
self.strict = kwargs.pop('strict', None)
flags |= v if kwargs.pop(k, False) else 0
self.regexes = [re.compile(arg, flags) for arg in args if util.isstr(arg)]
super(Regex, self).__init__(*args, **kwargs)
def _is_valid(self, value):
return util.isstr(value) and any(r.match(value) for r in self.regexes)
def get_name(self):
return self.regex_name or self.tag + " match"
class Ip(Validator):
"""IP address validator"""
tag = 'ip'
constraints = [con.IpVersion]
def _is_valid(self, value):
return self.ip_address(value)
def ip_address(self, value):
try:
import ipaddress
except ImportError:
raise ImportError("You must install the ipaddress backport in Py2")
try:
ipaddress.ip_interface(util.to_unicode(value))
except ValueError:
return False
return True
class Mac(Regex):
class String(Validator):
"""String validator"""
tag = 'str'
constraints = [con.LengthMin, con.LengthMax, con.CharacterExclude]
def _is_valid(self, value):
return util.isstr(value)
class Number(Validator):
"""Number/float validator"""
value_type = float
tag = 'num'
constraints = [con.Min, con.Max]
def _is_valid(self, value):
return isinstance(value, (int, float))
class Integer(Validator):
"""Integer validator"""
value_type = int
tag = 'int'
constraints = [con.Min, con.Max]
def _is_valid(self, value):
return isinstance(value, int)
class Boolean(Validator):