Spaces:
Running
Running
File size: 3,490 Bytes
2dbc96c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
"""Validator Module"""
import re
from bson.objectid import ObjectId
def validate(data, regex):
"""Custom Validator"""
return True if re.match(regex, data) else False
def validate_password(password: str):
"""Password Validator"""
reg = r"\b^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{8,20}$\b"
return validate(password, reg)
def validate_email(email: str):
"""Email Validator"""
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
return validate(email, regex)
def validate_book(**args):
"""Book Validator"""
if not args.get('title') or not args.get('image_url') \
or not args.get('category') or not args.get('user_id'):
return {
'title': 'Title is required',
'image_url': 'Image URL is required',
'category': 'Category is required',
'user_id': 'User ID is required'
}
if args.get('category') not in ['romance', 'peotry', 'politics' 'picture book', 'science', 'fantasy', 'horror', 'thriller']:
return {
'status': 'error',
'message': 'Invalid category'
}
try:
ObjectId(args.get('user_id'))
except:
return {
'user_id': 'User ID must be valid'
}
if not isinstance(args.get('title'), str) or not isinstance(args.get('description'), str) \
or not isinstance(args.get('image_url'), str):
return {
'title': 'Title must be a string',
'description': 'Description must be a string',
'image_url': 'Image URL must be a string'
}
return True
def validate_user(**args):
"""User Validator"""
if not args.get('email') or not args.get('password') or not args.get('name'):
return {
'email': 'Email is required',
'password': 'Password is required',
'name': 'Name is required'
}
if not isinstance(args.get('name'), str) or \
not isinstance(args.get('email'), str) or not isinstance(args.get('password'), str):
return {
'email': 'Email must be a string',
'password': 'Password must be a string',
'name': 'Name must be a string'
}
if not validate_email(args.get('email')):
return {
'email': 'Email is invalid'
}
if not validate_password(args.get('password')):
return {
'password': 'Password is invalid, Should be atleast 8 characters with \
upper and lower case letters, numbers and special characters'
}
print(len(args['name'].split(' ')))
if not 2 <= len(args['name'].split(' ')) <= 30:
return {
'name': 'Name must be between 2 and 30 words'
}
return True
def validate_email_and_password(email, password):
"""Email and Password Validator"""
if not (email and password):
return {
'email': 'Email is required',
'password': 'Password is required'
}
if not validate_email(email):
return {
'email': 'Email is invalid'
}
if not validate_password(password):
return {
'password': 'Password is invalid, Should be atleast 8 characters with \
upper and lower case letters, numbers and special characters'
}
return True
|