|
3 | 3 |
|
4 | 4 | def validate_json(data, schema):
|
5 | 5 | """
|
6 |
| - Validate the given JSON data against the provided schema. |
| 6 | + Validate the given JSON data against the provided schema, including required fields. |
7 | 7 |
|
8 | 8 | :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. |
10 | 10 | :return: Tuple (is_valid, error_message).
|
11 | 11 | """
|
12 | 12 | try:
|
| 13 | + # Validate the data against the schema, which includes required fields |
13 | 14 | validate(instance=data, schema=schema)
|
14 | 15 | return True, None
|
15 | 16 | except ValidationError as e:
|
| 17 | + # Return False and a clear error message if validation fails |
16 | 18 | return False, str(e)
|
17 | 19 |
|
18 |
| - |
19 | 20 | def create_json_validator(rules_config):
|
20 | 21 | """
|
21 | 22 | Creates a function that validates the request JSON based on the provided rules_config.
|
22 | 23 |
|
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. |
25 | 26 | """
|
26 |
| - validation_enabled = rules_config.get("validation_enabled", False) |
| 27 | + json_validation_enabled = rules_config.get("json_validation_enabled", False) |
27 | 28 | 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(): |
30 | 32 | """
|
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. |
32 | 34 | :return: Tuple (is_valid, response) - is_valid is True if valid, False otherwise.
|
33 | 35 | """
|
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) |
36 | 39 | 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) |
38 | 44 | return True, None
|
39 | 45 |
|
40 |
| - if validation_enabled: |
| 46 | + if json_validation_enabled: |
41 | 47 | print("JSON validation is enabled")
|
42 | 48 | else:
|
43 | 49 | print("JSON validation is disabled")
|
44 | 50 |
|
45 |
| - return validate_request |
| 51 | + return validate_json_request |
0 commit comments