Skip to content

feat: add pay invoice input validation middleware - #308

Open
rilwanubala wants to merge 1 commit into
WHEELBACK:mainfrom
rilwanubala:feature/add-request-validation-middleware-for-invoices-id-pay
Open

feat: add pay invoice input validation middleware#308
rilwanubala wants to merge 1 commit into
WHEELBACK:mainfrom
rilwanubala:feature/add-request-validation-middleware-for-invoices-id-pay

Conversation

@rilwanubala

Copy link
Copy Markdown

Summary

Centralises request body validation for invoice action routes into a shared ValidatedBody<T> extractor, preventing validation behaviour from drifting as new routes are added.

Changes

  • Added backend/src/extractors.rs with the ValidatedBody<T> Axum extractor.
    • Rejects non-JSON Content-Type with 422 Unprocessable Entity and a structured error body.
    • Wraps Axum's built-in Json extractor and surfaces a consistent ErrorResponse on deserialisation failures.
  • Updated backend/src/routes/pay.rs, refund.rs, and cancel.rs to use ValidatedBody<T> in place of the raw Json extractor.
  • Added mod extractors; declaration to backend/src/main.rs.
  • Added tests for malformed (non-JSON) bodies returning 422 in all three route files.

Test coverage

Each of pay.rs, refund.rs, and cancel.rs now contains:

Test Expectation
test_*_missing_body_returns_422 No body → 422
test_*_malformed_body_returns_422 Non-JSON bytes with Content-Type: application/json422
test_*_unreachable_rpc_returns_* Valid body, unreachable RPC → 500 / 404 / 403

Request / response examples

Missing body:

POST /invoices/1/pay
{ "error": "Request body must be JSON (Content-Type: application/json)", "code": 422 }

Malformed body:

POST /invoices/1/pay
Content-Type: application/json

not-valid-json{{
{ "error": "Invalid request body: …", "code": 422 }

Checklist

Centralises request body validation for /invoices/:id/pay,
/invoices/:id/refund, and /invoices/:id/cancel into a single
reusable ValidatedBody<T> extractor (backend/src/extractors.rs).

The extractor wraps Axum's Json<T> and emits a consistent
422 Unprocessable Entity response with a structured ErrorResponse
body whenever:
  - the Content-Type header is not application/json, or
  - the body is missing, truncated, or syntactically invalid JSON.

This prevents per-route validation logic from drifting as the
API surface grows.

Changes:
- Add backend/src/extractors.rs (ValidatedBody<T>)
- Update pay.rs, refund.rs, cancel.rs to use ValidatedBody<T>
- Add test_*_malformed_body_returns_422 tests to all three routes
- Fix backend/src/routes/mod.rs: export cancel module (was missing)
- Fix backend/src/main.rs: declare mod extractors; import
  cancel_invoice and refund_invoice (were missing, causing compile
  errors)
- Fix SorobanClient::new() call arity in refund.rs and cancel.rs
  tests (2 args → 3 args, matching the actual constructor signature)
@drips-wave

drips-wave Bot commented Jul 28, 2026

Copy link
Copy Markdown

@rilwanubala Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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.

Add request validation middleware for /invoices/:id/pay

1 participant