Browse Source
Added human-readable string representations to RequestValidationError and WebSocketRequestValidationError classes, consistent with the existing ResponseValidationError implementation. This improves developer experience when debugging validation errors by providing clear, formatted error messages. Changes: - Add __str__ method to RequestValidationError - Add __str__ method to WebSocketRequestValidationError - Add comprehensive tests for the new string representations The string format shows the number of errors and lists each error on a separate line, making it easier to read error messages during debugging and logging.pull/14414/head
2 changed files with 82 additions and 1 deletions
@ -0,0 +1,65 @@ |
|||
"""Tests for __str__ methods of validation exceptions.""" |
|||
|
|||
from fastapi.exceptions import ( |
|||
RequestValidationError, |
|||
ResponseValidationError, |
|||
WebSocketRequestValidationError, |
|||
) |
|||
|
|||
|
|||
def test_request_validation_error_str_single_error(): |
|||
"""Test string representation of RequestValidationError with single error.""" |
|||
errors = [{"type": "int_parsing", "loc": ["query", "id"], "msg": "Input should be a valid integer"}] |
|||
exc = RequestValidationError(errors) |
|||
result = str(exc) |
|||
|
|||
assert "1 validation error for request:" in result |
|||
assert "{'type': 'int_parsing'" in result |
|||
|
|||
|
|||
def test_request_validation_error_str_multiple_errors(): |
|||
"""Test string representation of RequestValidationError with multiple errors.""" |
|||
errors = [ |
|||
{"type": "int_parsing", "loc": ["query", "id"], "msg": "Input should be a valid integer"}, |
|||
{"type": "missing", "loc": ["query", "name"], "msg": "Field required"}, |
|||
] |
|||
exc = RequestValidationError(errors) |
|||
result = str(exc) |
|||
|
|||
assert "2 validation errors for request:" in result |
|||
assert "int_parsing" in result |
|||
assert "missing" in result |
|||
|
|||
|
|||
def test_websocket_request_validation_error_str_single_error(): |
|||
"""Test string representation of WebSocketRequestValidationError with single error.""" |
|||
errors = [{"type": "int_parsing", "loc": ["query", "id"], "msg": "Input should be a valid integer"}] |
|||
exc = WebSocketRequestValidationError(errors) |
|||
result = str(exc) |
|||
|
|||
assert "1 validation error for WebSocket request:" in result |
|||
assert "{'type': 'int_parsing'" in result |
|||
|
|||
|
|||
def test_websocket_request_validation_error_str_multiple_errors(): |
|||
"""Test string representation of WebSocketRequestValidationError with multiple errors.""" |
|||
errors = [ |
|||
{"type": "int_parsing", "loc": ["query", "id"], "msg": "Input should be a valid integer"}, |
|||
{"type": "missing", "loc": ["query", "name"], "msg": "Field required"}, |
|||
] |
|||
exc = WebSocketRequestValidationError(errors) |
|||
result = str(exc) |
|||
|
|||
assert "2 validation errors for WebSocket request:" in result |
|||
assert "int_parsing" in result |
|||
assert "missing" in result |
|||
|
|||
|
|||
def test_response_validation_error_str_consistency(): |
|||
"""Test that ResponseValidationError has similar __str__ behavior.""" |
|||
errors = [{"type": "int_parsing", "loc": ["response", "id"], "msg": "Input should be a valid integer"}] |
|||
exc = ResponseValidationError(errors) |
|||
result = str(exc) |
|||
|
|||
assert "1 validation error" in result |
|||
assert "{'type': 'int_parsing'" in result |
|||
Loading…
Reference in new issue