Last active
February 11, 2016 12:05
-
-
Save TakayoshiMiyamoto/551f7756a30476283db9 to your computer and use it in GitHub Desktop.
Python email, password validators
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import re | |
def validate_str_len_max(length): | |
def decorator(func): | |
def wrapper(*args, **kwargs): | |
if len(args[0]) <= length: | |
return func(*args, **kwargs) | |
return 'Please enter within {len} characters'.format(len=length), False | |
return wrapper | |
return decorator | |
def validate_str_len_min_max(min_len, max_len): | |
def decorator(func): | |
def wrapper(*args, **kwargs): | |
if min_len < len(args[0]) < max_len: | |
return func(*args, **kwargs) | |
return 'The number of characters Please enter {min} to {max} characters'.format( | |
min=min_len, max=max_len), False | |
return wrapper | |
return decorator | |
@validate_str_len_max(75) | |
def email_validate(email): | |
match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', email) | |
if not match: | |
return 'E-mail address is invalid', False | |
return None, True | |
@validate_str_len_min_max(8, 64) | |
def password_validate(password): | |
match = re.search(r'^[\x20-\x7E]+$', password) | |
if not match: | |
return 'Password is invalid', False | |
return None, True | |
class Validators: | |
@staticmethod | |
def validate(keys, values): | |
for key in keys: | |
if key is 'email': | |
msg, succeed = email_validate(values[key]) | |
if succeed is False: | |
return msg, succeed | |
elif key is 'password': | |
msg, succeed = password_validate(values[key]) | |
if succeed is False: | |
return msg, succeed | |
return None, True |
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
import unittest | |
from faker import Factory | |
sys.path.append(os.path.dirname(os.path.abspath(__name__))) | |
from .validators import email_validate, password_validate, Validators | |
fake = Factory.create('ja_JP') | |
class TestValidators(unittest.TestCase): | |
def setUp(self): | |
pass | |
def test_email_validate(self): | |
validate_message, is_succeed = email_validate('[email protected]') | |
self.assertTrue(is_succeed) | |
def test_password_validate(self): | |
validate_message, is_succeed = password_validate('passwordddd') | |
self.assertTrue(is_succeed) | |
def test_validators_class(self): | |
values = {'email': '[email protected]', 'password': fake.password()} | |
message, is_succeed = Validators.validate(keys=['email', 'password'], values=values) | |
self.assertIsNone(message) | |
self.assertTrue(is_succeed) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment