Feature/api schema validation joi - #105
Merged
jobbykings merged 3 commits intoJun 22, 2026
Merged
Conversation
jobbykings
approved these changes
Jun 22, 2026
6 tasks
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.
Description
Closes #58
Adds Joi-based request validation to all API route files that accept user input. Every POST/PUT/PATCH handler now validates
req.body, and GET handlers validatereq.query/req.params, before the controller is called. Invalid requests are rejected with a standardised 400 envelope:{ success: false, message: 'Validation failed', errors: [{ source, field, message }] }.27 route files were either newly validated or extended:
TypeScript routes (8) — schemas added to
middleware/validation.ts:collaborationRoutes.ts,notificationRoutes.ts,userRoutes.ts,agiTutorRoutes.ts,quizRoutes.ts,holographicRoutes.ts,eventLoggerRoutes.ts,syncRoutes.tsJavaScript routes (19) — inline Joi schemas with
validateRequestSchema:admin.js,auth.js,bookmarks.js,offline.js,gamification.js,search.js,courses.js,aco.js,quantumEncryption.js,fraudDetectionRoutes.js,autonomousAgents.js,rbacRoutes.js,content.js,optimization.js,prediction.js,federatedLearning.js,federatedLearningRoutes.js,swarmLearning.js,recommendations.jsfederatedLearning.js,swarmLearning.js,optimization.jsandprediction.jswere partially updated before this PR; this completes all remaining POST routes in those files.Pre-validated routes (left as-is — already meet the requirement):
smartWallet.ts(already used Joi +validateRequestSchema)transactions.js,tenantAnalytics.js,tenants.js(already had Joi schemas)quantum.js,translation.ts,vrf.ts,timeLockCredentials.ts,crossProtocolBridge.ts,cdnOptimizationRoutes.ts,plagiarismDetectionRoutes.ts,secureCommRoutes.ts(already had express-validator)Also extended
middleware/validation.tswith ~70 new Joi schemas covering collaboration, notifications, user profiles, AGI tutor, quizzes, holographic storage, event logging, sync, admin, announcements, authentication, bookmarks, offline queue, gamification, search, quantum encryption, fraud detection, RBAC, content, courses, ACO, autonomous agents, federated learning, prediction, recommendations, optimisation, and swarm learning domains.Related Issue
Closes #
Type of Change
Packages Affected
contracts/(Soroban / Rust)backend/(Node / Express)frontend/(Next.js)docs/How Has This Been Tested?
src/__tests__/validation.test.js): 9 unit tests that exercisevalidateRequestSchemawith real Joi schemas — valid body passes through, missing fields return 400 with standard envelope, invalid types return descriptive errors, all three sources (body/query/params) are validated, unknown fields are stripped, and multiple errors per source are aggregated..jsfiles passnode -c.tsc --noEmitproduces zero errors across the entire backend.tests/setup.js) depends onmongodb-memory-server, which crashes with SIGSEGV in this environment — this is a pre-existing infrastructure issue, not caused by these changes. The middleware-specific test suite passes cleanly when the setup file is skipped.Checklist
Breaking Changes
None
Additional Notes / Screenshots
Design rationale — Joi was chosen over Zod because it is already a dependency (
"joi": "^17.9.2"inpackage.json), the existingvalidateRequestSchemamiddleware is built for Joi's.validate()API, and bothvalidation.tsandvalidation.jsalready use Joi extensively. Rewriting ~70+ schemas in Zod would have been pure churn with no concrete benefit (the codebase doesn't use Zod's type-inference pattern from schemas).Error format — all validation errors use the standard envelope:
{ "success": false, "message": "Validation failed", "errors": [ { "source": "body", "field": "email", "message": "\"email\" is required" } ] }Remaining work — the following routes use express-validator rather than Joi but already validate input:
translation.ts,vrf.ts,timeLockCredentials.ts,crossProtocolBridge.ts,cdnOptimizationRoutes.ts,plagiarismDetectionRoutes.ts,secureCommRoutes.ts,quantum.js. They meet the "no raw req.body access" requirement and are safe to leave as-is. Migrating them to Joi would be a future cleanup task.