34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Functions for validating forms.
|
|
"""
|
|
|
|
async def validate_register(request, form):
|
|
"""Validate data from the registration form."""
|
|
username = form.get('username')
|
|
email = form.get('email')
|
|
password = form.get('password')
|
|
password_verify = form.get('password_verify')
|
|
|
|
async with request.app['pool'].acquire() as conn:
|
|
users = await conn.fetch(
|
|
"SELECT username, email FROM user_info "
|
|
"WHERE username = $1 OR email = $2",
|
|
username, email)
|
|
|
|
errors = {'password': [], 'username': [], 'email': []}
|
|
if password != password_verify:
|
|
errors['password'].append("Passwords do not match.")
|
|
if len(password) < 8 or len(password) > 10240:
|
|
errors['password'].append(
|
|
"Password must be between 8 and 10240 characters long.")
|
|
if len(username) < 3 or len(username) > 20:
|
|
errors['username'].append(
|
|
"Username must be between 3 and 20 characters long.")
|
|
if username in [user['username'] for user in users]:
|
|
errors['username'].append("Username already in use.")
|
|
if email in [user['email'] for user in users]:
|
|
errors['email'].append("Email already in use.")
|
|
|
|
return errors
|