-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
29 lines (25 loc) · 980 Bytes
/
Copy pathhandler.py
File metadata and controls
29 lines (25 loc) · 980 Bytes
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
import json
class InputValidationError(Exception):
pass
def validate_input(data):
if not isinstance(data, dict):
raise InputValidationError("Input must be a dictionary")
if 'name' not in data or not isinstance(data['name'], str):
raise InputValidationError("'name' field must be a string")
if 'age' not in data or not isinstance(data['age'], int):
raise InputValidationError("'age' field must be an integer")
return True
def process_data(data):
try:
validate_input(data)
# Simulate some processing
return json.dumps({"status": "success", "data": data})
except InputValidationError as e:
return json.dumps({"status": "error", "message": str(e)})
if __name__ == '__main__':
sample_input = {'name': 'John Doe', 'age': 30}
result = process_data(sample_input)
print(result)
invalid_input = {'name': 123, 'age': 'thirty'}
result = process_data(invalid_input)
print(result)