Skip to content

Enhance DIGIPIN API with full input validation, improved error handling, and middleware upgrades#3

Open
Rajdip585 wants to merge 3 commits intoINDIAPOST-gov:mainfrom
Rajdip585:main
Open

Enhance DIGIPIN API with full input validation, improved error handling, and middleware upgrades#3
Rajdip585 wants to merge 3 commits intoINDIAPOST-gov:mainfrom
Rajdip585:main

Conversation

@Rajdip585
Copy link
Copy Markdown

Overview

This pull request introduces a set of improvements to the DIGIPIN API, focusing on input validation, error handling, and safer request processing.
The goal is to make the API more robust, predictable, and user-friendly while maintaining backward compatibility.


Key Improvements

1. Added Comprehensive Input Validation

  • Validates latitude, longitude, and digipin fields for:
    • Missing values
    • Incorrect types
    • Invalid number formats
    • Empty strings
  • Introduced helper functions:
    • isValidNumber()
    • isValidString()

2. Improved Error Handling

  • Added structured error responses with explicit error codes.
  • Better handling of malformed JSON payloads.
  • Clear error messaging for invalid inputs in both POST and GET endpoints.

3. Middleware Enhancements

  • Added a validateJsonContentType middleware to enforce application/json for POST requests.
  • Improved JSON parsing error handler (invalid/malformed JSON now returns a clear 400 response).

4. Route-Level Improvements

  • Rewrote DIGIPIN encode/decode routes for clarity, safety, and maintainability.
  • Consistent parsing of query parameters and body data.
  • Clean separation of validation, parsing, and execution logic.

Files Updated

  • src/app.js
  • src/routes/digipin.routes.js
  • server.js

Why These Changes Matter

  • Prevents server crashes or unpredictable behavior caused by invalid inputs.
  • Guarantees consistent API responses across clients (mobile, web).
  • Makes the API more production-ready with clearer contracts and stricter validation.
  • Improves developer experience and debugging reliability.

Compatibility

✔ No breaking changes to the API structure
✔ Existing clients continue to work
✔ New validations only reject invalid data (expected behavior)


Request

Please review the changes and merge if they align with the project's direction.
I am open to adjustments or suggestions to align with your preferred coding style or architecture.


Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant