[Tutor] Code/Style Review of Data Validation Code
Sean Abrahams
sa at sfsu.edu
Tue Feb 24 18:41:02 EST 2004
The following is some code I wrote to validate certain types of data
that is passed into a python script via a web form. However, the way it
is implemented now you can pass in any set of rules and any set of data
and get back some results on wether the data passed the tests.
The style of the resulting dict helps with redisplaying the results
back onto the web page.
I'm interested in improving the code overall and perhaps working
exceptions in, instead of using a specific dict format. Anyone have any
arguments for creating a class for this type of stuff? etc?
Thanks,
---Sean
------
def validateData(rules, data):
"""
Validates {data} according to {rules}.
Datatypes
---------
alpha - can only contain alpha letters
alphanum - can only contain alpha letters and/or numbers
num - can only contain numbers
email - must conform to email regexp recipe
phone - must conform to phone regexp recipe
Usage
-----
data = {
'firstname' : 'John',
'lastname' : 'St. Claire',
'email' : 'jsn.com',
'phone' : '415-333-3333'
}
rules = {
'firstname' : {
'dataType' : 'alpha',
'minlen' : '2',
'maxlen' : '64',
'isRequired' : '1'
},
'lastname' : {
'dataType' : 'alpha',
'minlen' : '2',
'maxlen' : '64',
'isRequired' : '1'
},
'email' : {
'dataType' : 'email',
'minlen' : '5',
'maxlen' : '30',
'isRequired' : '0'
},
'phone' : {
'dataType' : 'phone',
'minlen' : '7' ,
'maxlen' : '32',
'isRequired' : '0'
},
}
@returns dict of the format:
results = {
'name' : 'John'
'name_error' : '', # Empty because no error
'lastname' : 'St. Claire',
'lastname_error' : '',
'email' : 'jsn.com',
'email_error' : '* Invalid email address. Please enter a valid
email address. (Ex: yourname at sfsu.edu)',
'phone' : '415-333-3333',
'phone_error' : ''
}
@param data: dict of data
@type data: dict
@param rules: the rules that will be applied towards the data
@type rules: dict of dicts
@returns: original data with _error keys added
@rtype: dict
"""
results = {}
for key in rules.keys():
try:
if rules[key]['isRequired'] == '0' and data[key] == "":
pass
else:
errorCheck = validate(data[key], rules[key]['dataType'],
rules[key]['minlen'], rules[key]['maxlen'])
results[key] = errorCheck[0]
try:
results[key+"_error"] = errorCheck[1]
except IndexError:
results[key+"_error"] = ""
except KeyError:
results[key] = ""
results[key+"_error"] = "* Error"
return results
def validate(input, dataType, minlen, maxlen):
"""
Validate input, applying dataType, minlen, and
maxlen constraints, and return results.
@param input: the input of the form field
@type input: string
@param dataType: the dataType the input must match to
@type dataType: string
@param minlen: the minimum length input must be
@type minlen: string
@param maxlen: the maximum length input can be
@type maxlen: string
@returns: results in the format of:
results = ['Jon'] if the data is valid
results = ['invalidemail.com', "* Error Message"] if data is
invalid
@rtype: list
"""
results = []
if dataType == "alpha":
if re.compile("^\D+$").search(input):
results = [input]
else:
results = [input, "* Only letters are allowed."]
if dataType == "alphanum":
if re.compile("^([1-zA-Z0-1@#.,'/\-\s]{1,255})$").search(input):
results = [input]
else:
results = [input, "* Only alphanumerics are allowed."]
if dataType == "num":
if re.compile("^\d+$").search(input):
results = [input]
else:
results = [input, "* Only numbers are allowed."]
if dataType == "email":
if re.compile("^.+ at .+\..{2,3}$").search(input):
results = [input]
else:
results = [input, """* Invalid email address. Please enter
a valid email address. (Ex: yourname at domain.com)"""]
if dataType == "phone":
if
re.compile("^1?\s*-?\s*(\d{3}|\(\s*\d{3}\s*\))\s*-?\s*\d{3}\s*-?
\s*\d{4}$").search(input):
results = [input]
else:
results = [input, """* Invalid phone number. Please enter
the complete number. (Ex: 415-333-3333)"""]
if len(str(input)) < int(minlen):
results = [input, """* Must contain at least """ + \
str(minlen) + """ characters."""]
if len(str(input)) > int(maxlen):
results = [input, """* Must contain less than """ + \
str(maxlen) + """ characters."""]
return results
More information about the Tutor
mailing list