feat(api): add JSON schema response validation to REST endpoints#75
Open
mikkyvans0-source wants to merge 3 commits into
Conversation
Attach response schemas to every route in src/api/rest.ts (/status, /price/:a/:b, /price/:a/:b/route, /price/:a/:b/history, /pools) and add shared schema objects in src/api/schemas.ts. In dev/test a validating serializer checks each outgoing payload against its schema and throws on a mismatch, so accidental response-shape changes fail loudly instead of shipping silently. Production keeps Fastify default (fast) serialization for the same schemas, so there is no runtime impact. Adds src/__tests__/schemaValidation.test.ts asserting a known endpoint matches its schema and that extra/wrong-typed fields are rejected. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
@mikkyvans0-source 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! 🚀 |
Attach response schemas to every route in src/api/rest.ts (/status, /price/:a/:b, /price/:a/:b/route, /price/:a/:b/history, /pools) and add shared schema objects in src/api/schemas.ts. In dev/test a validating serializer checks each outgoing payload against its schema and throws on a mismatch, so accidental response-shape changes fail loudly instead of shipping silently. Production keeps Fastify default (fast) serialization for the same schemas, so there is no runtime impact. Adds src/__tests__/schemaValidation.test.ts asserting a known endpoint matches its schema and that extra/wrong-typed fields are rejected.
…ts' of https://github.com/mikkyvans0-source/Lens into Add-JSON-Schema-response-validation-to-all-REST-endpoints
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.
Added JSON Schema response validation to all REST endpoints in src/api/rest.ts.
Closes #57
New file — src/api/schemas.ts:
Shared response schema objects for all five routes: statusResponseSchema, priceResponseSchema, routeResponseSchema, historyResponseSchema, poolsResponseSchema.
installResponseValidation(app) — installs a custom AJV-backed serializer that validates each payload and throws on mismatch. It's a no-op when NODE_ENV === 'production', so production falls back to Fastify's default fast serializer for the same schemas → zero runtime impact.
src/api/rest.ts:
Every route now carries { schema: { response: { 200: ... } } }.
installResponseValidation(app) runs first so routes pick up the validating serializer.
New test — src/tests/schemaValidation.test.ts:
Asserts /price returns 200 and the body matches the published schema.
Asserts an undeclared extra field → 500 (shape drift caught).
Asserts a wrong field type → 500.
Acceptance criteria:
✅ Every REST route has a response schema.
✅ Tests fail when a response shape changes (verified by the two 500-expecting cases).
✅ No production runtime impact (validation skipped when NODE_ENV=production).
Verification: full suite passes (82/82); tsc --noEmit is clean for all changed files (the two webhookDispatcher.ts errors are pre-existing and untouched).