Skip to content

Commit fd75be8

Browse files
author
Veinar
committed
Refactored METHOD and JSON validators
1 parent cc55f1d commit fd75be8

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

dracan/validators/json_validator.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,49 @@
33

44
def validate_json(data, schema):
55
"""
6-
Validate the given JSON data against the provided schema.
6+
Validate the given JSON data against the provided schema, including required fields.
77
88
:param data: The JSON data to validate.
9-
:param schema: The schema to validate against.
9+
:param schema: The schema to validate against, which includes 'required' fields.
1010
:return: Tuple (is_valid, error_message).
1111
"""
1212
try:
13+
# Validate the data against the schema, which includes required fields
1314
validate(instance=data, schema=schema)
1415
return True, None
1516
except ValidationError as e:
17+
# Return False and a clear error message if validation fails
1618
return False, str(e)
1719

18-
1920
def create_json_validator(rules_config):
2021
"""
2122
Creates a function that validates the request JSON based on the provided rules_config.
2223
23-
:param rules_config: The configuration that contains validation rules.
24-
:return: A validation function or None if validation is disabled.
24+
:param rules_config: The configuration that contains JSON validation rules, including the JSON schema.
25+
:return: A validation function or None if JSON validation is disabled.
2526
"""
26-
validation_enabled = rules_config.get("validation_enabled", False)
27+
json_validation_enabled = rules_config.get("json_validation_enabled", False)
2728
json_schema = rules_config.get("json_schema", {})
28-
29-
def validate_request():
29+
detailed_errors_enabled = rules_config.get("detailed_errors_enabled", False) # Default to False
30+
31+
def validate_json_request():
3032
"""
31-
Validates the request JSON against the schema if validation is enabled.
33+
Validates the request JSON against the schema if JSON validation is enabled.
3234
:return: Tuple (is_valid, response) - is_valid is True if valid, False otherwise.
3335
"""
34-
if validation_enabled and request.method in ['POST', 'PUT']:
35-
is_valid, error_message = validate_json(request.get_json(), json_schema)
36+
if json_validation_enabled and request.method in ['POST', 'PUT']:
37+
data = request.get_json()
38+
is_valid, error_message = validate_json(data, json_schema)
3639
if not is_valid:
37-
return False, jsonify({'error': f"Invalid JSON: {error_message}"}), 400
40+
# Determine whether to include detailed error information
41+
error_message_to_display = error_message if detailed_errors_enabled else "Invalid JSON format"
42+
# Return False and a 400 Bad Request response with only two values
43+
return False, (jsonify({'error': f"{error_message_to_display}"}), 400)
3844
return True, None
3945

40-
if validation_enabled:
46+
if json_validation_enabled:
4147
print("JSON validation is enabled")
4248
else:
4349
print("JSON validation is disabled")
4450

45-
return validate_request
51+
return validate_json_request

dracan/validators/method_validator.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,32 @@ def create_method_validator(rules_config):
44
"""
55
Creates a function that validates if the request method is allowed based on the provided rules_config.
66
7-
:param rules_config: The configuration that contains allowed methods.
7+
:param rules_config: The configuration that contains allowed methods and whether method validation is enabled.
88
:return: A validation function for HTTP methods.
99
"""
10-
allowed_methods = rules_config.get("allowed_methods", ["GET", "POST", "PUT", "DELETE"])
10+
allowed_methods = rules_config.get("allowed_methods", ["GET", "PUT", "POST", "DELETE"])
11+
method_validation_enabled = rules_config.get("method_validation_enabled", False) # Default to False
1112

12-
def validate_method():
13+
def validate_http_method():
1314
"""
14-
Validates the request method against the allowed methods.
15+
Validates the request method against the allowed methods if method validation is enabled.
1516
:return: Tuple (is_valid, response) - is_valid is True if valid, False otherwise.
1617
"""
18+
if not method_validation_enabled:
19+
# If method validation is disabled, always return True
20+
return True, None
21+
1722
if request.method not in allowed_methods:
18-
return False, jsonify({'error': f"Method {request.method} not allowed"}), 405
23+
# Return False and a validation response with a 405 status code if method is not allowed
24+
return False, (jsonify({'error': f"Method {request.method} not allowed"}), 405)
25+
26+
# Return True and None if the method is allowed
1927
return True, None
2028

21-
return validate_method
29+
if method_validation_enabled:
30+
print("HTTP Method validation is enabled, allowed methods: " + " | ".join(allowed_methods))
31+
else:
32+
print("HTTP Method validation is disabled")
33+
34+
35+
return validate_http_method

proxy_config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"destination": {
33
"host": "127.0.0.1",
44
"port": 8080,
5-
"path": "/health"
5+
"path": "/"
66
}
77
}

rules_config.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"limiting_enabled": true,
33
"rate_limit": "10 per minute",
4-
"allowed_methods": ["GET", "POST"],
4+
"allowed_methods": ["GET", "POST", "DELETE"],
55
"validation_enabled": true,
6+
"detailed_errors_enabled": false,
67
"json_schema": {
78
"type": "object",
89
"properties": {
@@ -11,4 +12,4 @@
1112
},
1213
"required": ["name", "age"]
1314
}
14-
}
15+
}

0 commit comments

Comments
 (0)