Skip to content

feat(api): add JSON schema response validation to REST endpoints#75

Open
mikkyvans0-source wants to merge 3 commits into
Miracle656:mainfrom
mikkyvans0-source:Add-JSON-Schema-response-validation-to-all-REST-endpoints
Open

feat(api): add JSON schema response validation to REST endpoints#75
mikkyvans0-source wants to merge 3 commits into
Miracle656:mainfrom
mikkyvans0-source:Add-JSON-Schema-response-validation-to-all-REST-endpoints

Conversation

@mikkyvans0-source
Copy link
Copy Markdown

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).

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>
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented Jun 2, 2026

@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! 🚀

Learn more about application limits

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.
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 JSON Schema response validation to all REST endpoints

1 participant