Event-CRUD-Flask-python3-API/middlewares.py

37 lines
1.4 KiB
Python
Raw Normal View History

from functools import wraps
from flask import request, jsonify
from datetime import datetime
def validate_event_post_request(f):
@wraps(f)
def decorated_function(*args, **kwargs):
data = request.get_json()
if not data:
return jsonify({"message": "No input data provided"}), 400
# Check required fields
required_fields = ['title', 'duedate', 'location', 'description']
if not all(field in data for field in required_fields):
return jsonify({"message": "Please check your data, you missing some props; visit our docs https://git.dayanhub.com/kfir"}), 400
# Validate 'title'
if not isinstance(data['title'], str) or not data['title'].strip():
return jsonify({"message": "Invalid title"}), 400
# Validate 'description'
if not isinstance(data['description'], str):
return jsonify({"message": "Invalid description"}), 400
# Validate 'time' (ensure it's a valid datetime string)
try:
datetime.strptime(data['duedate'], '%Y-%m-%dT%H:%M:%S')
except ValueError:
return jsonify({"message": "Invalid time format. Use YYYY-MM-DDTHH:MM:SS"}), 400
# Validate 'location'
if not isinstance(data['location'], str) or not data['location'].strip():
return jsonify({"message": "Invalid location"}), 400
return f(*args, **kwargs)
return decorated_function