feat(interceptors): add ResponseValidationInterceptor for response DT…#31
Open
Chrisbankz0 wants to merge 1 commit into
Open
feat(interceptors): add ResponseValidationInterceptor for response DT…#31Chrisbankz0 wants to merge 1 commit into
Chrisbankz0 wants to merge 1 commit into
Conversation
Contributor
|
@Chrisbankz0 |
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.
#closes
#26
This PR adds runtime response validation for public API handlers to prevent undocumented response shape drift. It introduces a small opt-in system where controller handlers or controllers can be decorated with
@ResponseDto(...)and responses will be validated against the DTO class at runtime.Key changes:
ResponseDtodecorator to mark expected response DTOs.ResponseValidationInterceptorto validate outgoing responses.main.ts.UsernamesControllerhandlers as an example.Files of interest:
Motivation
Controller implementations, DTO definitions, and OpenAPI documentation can drift over time. This PR provides a small safeguard that fails fast when runtime controller responses no longer conform to their declared DTOs, reducing integration bugs between frontend and backend and preventing client runtime errors.
Implementation details
ResponseDtois a metadata decorator (usesSetMetadata) to attach a DTO class to a controller or handler.ResponseValidationInterceptorreads that metadata, transforms outgoing payloads into the DTO class instances usingclass-transformer, and validates withclass-validator. If validation fails, anInternalServerErrorExceptionwithcode: "RESPONSE_VALIDATION_ERROR"is thrown.@ResponseDto(...)are validated.How to test (step-by-step)
cd app/backend pnpm test:unitExpected: tests pass. The spec includes a positive case (valid response) and a negative case (missing required fields causing a validation error).
Call an annotated endpoint (e.g.,
GET /username) and confirm normal responses remain unchanged. If any annotated handler returns an invalid shape, the response will be a 500 with body{ code: "RESPONSE_VALIDATION_ERROR", ... }.To add more coverage, annotate other controllers with
@ResponseDto(YourDto)and add unit/integration tests asserting that invalid shapes fail.Backwards compatibility & migration
@ResponseDtoare validated. There is no breaking change for unannotated routes.@ApiPropertywithoutclass-validatordecorators, convert them to classes and add minimal validation decorators (or extend the interceptor later to read OpenAPI metadata).Security & performance
class-validatorcalls during the response path. For endpoints returning large arrays, you may want to limit array-size validation or move checks to background jobs. Current implementation validates arrays by checking every element synchronously.Checklist for reviewers
main.tsand does not interfere with error handling pipeline.ResponseDtodecorator metadata is read correctly for handler and controller-level annotations.transactions,stellar, etc.).Rollback plan
Revert the commit(s) or remove the global interceptor registration from
main.tsto disable runtime response validation while keeping the decorator and tests for follow-up work.Related issues
If you'd like, I can annotate additional controllers with
@ResponseDto(...)and/or extend the interceptor to validate against OpenAPI schemas for DTOs that are not classes.