Create utils/validators.py
Browse files- app/utils/validators.py +33 -0
app/utils/validators.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from flask import request, jsonify
|
| 3 |
+
|
| 4 |
+
def validate_email(email):
|
| 5 |
+
"""Validate an email address."""
|
| 6 |
+
regex = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
|
| 7 |
+
return re.match(regex, email) is not None
|
| 8 |
+
|
| 9 |
+
def validate_password(password):
|
| 10 |
+
"""Validate a password (at least 8 characters, one uppercase, one number)."""
|
| 11 |
+
if len(password) < 8:
|
| 12 |
+
return False
|
| 13 |
+
if not re.search(r"[A-Z]", password):
|
| 14 |
+
return False
|
| 15 |
+
if not re.search(r"\d", password):
|
| 16 |
+
return False
|
| 17 |
+
return True
|
| 18 |
+
|
| 19 |
+
def validate_ad_content(content):
|
| 20 |
+
"""Validate ad content (non-empty and within length limits)."""
|
| 21 |
+
if not content or len(content) > 1000:
|
| 22 |
+
return False
|
| 23 |
+
return True
|
| 24 |
+
|
| 25 |
+
def validate_request_json(required_fields):
|
| 26 |
+
"""Validate JSON request payload for required fields."""
|
| 27 |
+
data = request.get_json()
|
| 28 |
+
if not data:
|
| 29 |
+
return jsonify({"error": "Request must be JSON"}), 400
|
| 30 |
+
for field in required_fields:
|
| 31 |
+
if field not in data:
|
| 32 |
+
return jsonify({"error": f"Missing required field: {field}"}), 400
|
| 33 |
+
return None
|