feat: add pay invoice input validation middleware - #308
Open
rilwanubala wants to merge 1 commit into
Open
Conversation
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)
|
@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! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
backend/src/extractors.rswith theValidatedBody<T>Axum extractor.Content-Typewith422 Unprocessable Entityand a structured error body.Jsonextractor and surfaces a consistentErrorResponseon deserialisation failures.backend/src/routes/pay.rs,refund.rs, andcancel.rsto useValidatedBody<T>in place of the rawJsonextractor.mod extractors;declaration tobackend/src/main.rs.422in all three route files.Test coverage
Each of
pay.rs,refund.rs, andcancel.rsnow contains:test_*_missing_body_returns_422422test_*_malformed_body_returns_422Content-Type: application/json→422test_*_unreachable_rpc_returns_*500/404/403Request / response examples
Missing body:
{ "error": "Request body must be JSON (Content-Type: application/json)", "code": 422 }Malformed body:
{ "error": "Invalid request body: …", "code": 422 }Checklist