Last active
December 20, 2015 04:38
-
-
Save hollanddd/6071839 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Validator(): | |
GEO = ('geo', 'lat/lng', 'location') | |
ZIP = ('zip code', 'postal code', 'zip') | |
PHONE = ('phone', 'phone number', 'number', '#') | |
STATE = ('state abbreviation', 'state code', 'state') | |
ADDRESS = ('address') | |
URL = ('url', 'web address', 'facebook page') | |
STATES = { | |
'AK': 'Alaska', | |
'AL': 'Alabama', | |
'AR': 'Arkansas', | |
'AS': 'American Samoa', | |
'AZ': 'Arizona', | |
'CA': 'California', | |
'CO': 'Colorado', | |
'CT': 'Connecticut', | |
'DC': 'District of Columbia', | |
'DE': 'Delaware', | |
'FL': 'Florida', | |
'GA': 'Georgia', | |
'GU': 'Guam', | |
'HI': 'Hawaii', | |
'IA': 'Iowa', | |
'ID': 'Idaho', | |
'IL': 'Illinois', | |
'IN': 'Indiana', | |
'KS': 'Kansas', | |
'KY': 'Kentucky', | |
'LA': 'Louisiana', | |
'MA': 'Massachusetts', | |
'MD': 'Maryland', | |
'ME': 'Maine', | |
'MI': 'Michigan', | |
'MN': 'Minnesota', | |
'MO': 'Missouri', | |
'MP': 'Northern Mariana Islands', | |
'MS': 'Mississippi', | |
'MT': 'Montana', | |
'NA': 'National', | |
'NC': 'North Carolina', | |
'ND': 'North Dakota', | |
'NE': 'Nebraska', | |
'NH': 'New Hampshire', | |
'NJ': 'New Jersey', | |
'NM': 'New Mexico', | |
'NV': 'Nevada', | |
'NY': 'New York', | |
'OH': 'Ohio', | |
'OK': 'Oklahoma', | |
'OR': 'Oregon', | |
'PA': 'Pennsylvania', | |
'PR': 'Puerto Rico', | |
'RI': 'Rhode Island', | |
'SC': 'South Carolina', | |
'SD': 'South Dakota', | |
'TN': 'Tennessee', | |
'TX': 'Texas', | |
'UT': 'Utah', | |
'VA': 'Virginia', | |
'VI': 'Virgin Islands', | |
'VT': 'Vermont', | |
'WA': 'Washington', | |
'WI': 'Wisconsin', | |
'WV': 'West Virginia', | |
'WY': 'Wyoming' | |
} | |
def check(self, type_of, obj): | |
if type(type_of) is not str: raise TypeError | |
type_of = type_of.lower() | |
if type_of in self.PHONE: | |
return self._validate_phone(obj) | |
if type_of in self.ZIP: | |
return self._validate_zip(obj) | |
if type_of in self.STATE: | |
return self._validate_state(obj) | |
if type_of in self.ADDRESS: | |
return self._validate_address(obj) | |
if type_of in self.URL: | |
return self._validate_url(obj) | |
if type_of in self.GEO: | |
return self._validate_geo(obj) | |
def _validate_phone(self, obj): | |
pattern = re.compile(r''' | |
# don't match beginning of string, number can start anywhere | |
(\d{3}) # area code is 3 digits (e.g. '800') | |
\D* # optional separator is any number of non-digits | |
(\d{3}) # trunk is 3 digits (e.g. '555') | |
\D* # optional separator | |
(\d{4}) # rest of number is 4 digits (e.g. '1212') | |
\D* # optional separator | |
(\d*) # extension is optional and can be any number of digits | |
$ # end of string | |
''', re.VERBOSE) | |
valid = re.match(pattern, obj) | |
return True if valid else False | |
def _validate_zip(self, obj): | |
try: | |
obj = str(obj) | |
except: | |
raise TypeError, 'zip must be a string' | |
pattern = re.compile(r'.*(\d{5}(\-\d{4})?)$', re.VERBOSE) | |
valid = re.match(pattern, obj) | |
return True if valid else False | |
def _validate_state(self, obj): | |
if (obj.upper() in self.STATES or obj in self.STATES.values()): | |
return True | |
return False | |
def _validate_url(self, obj): | |
return 'Validator::_validate_address()' | |
def _validate_address(self, obj): | |
return 'Validator::_validate_address()' | |
def _valiate_geo(self, obj): | |
return 'Validator::_validate_geo()' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment