Update validation_and_logging.md

This commit is contained in:
Ankit Mahato
2025-11-05 04:14:00 +05:30
parent e906ff2f60
commit f2cc620082

View File

@@ -1,33 +1,40 @@
# API Dash Validation Logging Implementation # API Dash Validation Logging Implementation
## Overview ## Overview
This implementation addresses GitHub issues #906 and #587 by migrating validation warnings and errors from transient UI notifications to the persistent in-app logging console. This implementation addresses GitHub issues #906 and #587 by migrating validation warnings and errors from transient UI notifications to the persistent in-app logging console.
## Changes Made ## Changes Made
### 1. Enhanced `collection_providers.dart` ### 1. Enhanced `collection_providers.dart`
**File:** `lib/providers/collection_providers.dart` **File:** `lib/providers/collection_providers.dart`
**Added validation logic in the `sendRequest()` method:** **Added validation logic in the `sendRequest()` method:**
#### Validation Checks Implemented: #### Validation Checks Implemented:
1. **Empty URL Validation** 1. **Empty URL Validation**
- **Level:** Error - **Level:** Error
- **Message:** "Request URL is empty. Please provide a valid URL." - **Message:** "Request URL is empty. Please provide a valid URL."
- **Tags:** `['request-validation', 'empty-url']` - **Tags:** `['request-validation', 'empty-url']`
2. **GET Request with Body Warning** 2. **GET Request with Body Warning**
- **Level:** Warning - **Level:** Warning
- **Message:** "GET request contains a body. This may not be supported by all servers." - **Message:** "GET request contains a body. This may not be supported by all servers."
- **Tags:** `['request-validation', 'get-with-body']` - **Tags:** `['request-validation', 'get-with-body']`
- **Trigger:** When HTTP method is GET and request body is not empty - **Trigger:** When HTTP method is GET and request body is not empty
3. **JSON Validation** 3. **JSON Validation**
- **Valid JSON (Debug):** - **Valid JSON (Debug):**
- **Level:** Debug - **Level:** Debug
- **Message:** "Request body contains valid JSON." - **Message:** "Request body contains valid JSON."
- **Tags:** `['request-validation', 'valid-json']` - **Tags:** `['request-validation', 'valid-json']`
- **Invalid JSON (Error):** - **Invalid JSON (Error):**
- **Level:** Error - **Level:** Error
- **Message:** "Invalid JSON in request body: [error details]" - **Message:** "Invalid JSON in request body: [error details]"
@@ -40,19 +47,25 @@ This implementation addresses GitHub issues #906 and #587 by migrating validatio
- **Tags:** `['request-validation', 'completed']` - **Tags:** `['request-validation', 'completed']`
### 2. Added Import ### 2. Added Import
**Added:** `import 'package:better_networking/better_networking.dart';` **Added:** `import 'package:better_networking/better_networking.dart';`
- Required for accessing `HTTPVerb`, `ContentType`, and `kJsonDecoder` constants - Required for accessing `HTTPVerb`, `ContentType`, and `kJsonDecoder` constants
## Implementation Details ## Implementation Details
### Code Location ### Code Location
The validation logic is inserted in the `sendRequest()` method right after the HTTP request model is prepared but before the actual network request is sent. This ensures: The validation logic is inserted in the `sendRequest()` method right after the HTTP request model is prepared but before the actual network request is sent. This ensures:
- All request parameters are finalized - All request parameters are finalized
- Validation happens for every request - Validation happens for every request
- Logs appear in the terminal before network activity - Logs appear in the terminal before network activity
### Terminal Integration ### Terminal Integration
Uses the existing terminal logging system: Uses the existing terminal logging system:
```dart ```dart
final terminal = ref.read(terminalStateProvider.notifier); final terminal = ref.read(terminalStateProvider.notifier);
terminal.logSystem( terminal.logSystem(
@@ -66,34 +79,43 @@ terminal.logSystem(
## Testing Scenarios ## Testing Scenarios
### Test Case 1: GET Request with Body ### Test Case 1: GET Request with Body
**Setup:** **Setup:**
- Method: GET - Method: GET
- URL: https://api.example.com/data - URL: https://api.example.com/data
- Body: `{"key": "value"}` - Body: `{"key": "value"}`
**Expected Result:** **Expected Result:**
- ⚠️ Warning log appears in terminal console - ⚠️ Warning log appears in terminal console
- Message: "GET request contains a body. This may not be supported by all servers." - Message: "GET request contains a body. This may not be supported by all servers."
### Test Case 2: POST Request with Invalid JSON ### Test Case 2: POST Request with Invalid JSON
**Setup:** **Setup:**
- Method: POST - Method: POST
- URL: https://api.example.com/data - URL: https://api.example.com/data
- Content-Type: application/json - Content-Type: application/json
- Body: `{"key": "value", "invalid": }` (missing value) - Body: `{"key": "value", "invalid": }` (missing value)
**Expected Result:** **Expected Result:**
- ❌ Error log appears in terminal console - ❌ Error log appears in terminal console
- Message: "Invalid JSON in request body: [FormatException details]" - Message: "Invalid JSON in request body: [FormatException details]"
### Test Case 3: Valid POST Request ### Test Case 3: Valid POST Request
**Setup:** **Setup:**
- Method: POST - Method: POST
- URL: https://api.example.com/data - URL: https://api.example.com/data
- Content-Type: application/json - Content-Type: application/json
- Body: `{"key": "value", "number": 123}` - Body: `{"key": "value", "number": 123}`
**Expected Result:** **Expected Result:**
- ✅ Debug log: "Request body contains valid JSON." - ✅ Debug log: "Request body contains valid JSON."
- Info log: "Request validation completed for POST https://api.example.com/data" - Info log: "Request validation completed for POST https://api.example.com/data"
@@ -108,12 +130,14 @@ terminal.logSystem(
## Migration Status ## Migration Status
**Completed:** **Completed:**
- GET request with body validation - GET request with body validation
- JSON validation for request bodies - JSON validation for request bodies
- Empty URL validation - Empty URL validation
- Integration with existing terminal logging system - Integration with existing terminal logging system
**Verified:** **Verified:**
- No compilation errors - No compilation errors
- Proper import statements - Proper import statements
- Consistent logging format - Consistent logging format
@@ -122,29 +146,9 @@ terminal.logSystem(
## Future Enhancements ## Future Enhancements
Potential additional validations that could be added: Potential additional validations that could be added:
- URL format validation - URL format validation
- Header validation - Header validation
- Authentication parameter validation - Authentication parameter validation
- Request size limits - Request size limits
- Content-Type mismatch warnings - Content-Type mismatch warnings
## Files Modified
1. `lib/providers/collection_providers.dart` - Added validation logic
2. `test_validation_logging.dart` - Demo/test file (can be removed)
3. `VALIDATION_LOGGING_IMPLEMENTATION.md` - This documentation
## Commit Message
```
fix(logging): redirect request validation warnings & errors to in-app console (#906, #587)
- Add GET-with-body validation warning to terminal console
- Add JSON validation error logging for invalid request bodies
- Add empty URL validation error logging
- Add request validation completion info logging
- Replace transient UI notifications with persistent terminal logs
- Maintain consistent logging format with categories and tags
Resolves #906: Migrate to in-app logging console
Resolves #587: Add a Global status bar in API Dash
```