Add comprehensive request validation with Zod schemas #95
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
This PR implements comprehensive request validation using Zod schemas for authentication and employee management endpoints, improving input validation and type safety.
Problem
Several API endpoints had critical validation issues:
as PostBody) without validationSolution
Implement Zod validation schemas for all affected endpoints, leveraging the existing
validateRequestBodyutility.Changes Made
New Validation Schemas
EmployeeIdSchema:
SignupEmployeeSchema:
SignupEmployerSchema:
Endpoint Updates
signup/employee/route.ts
signup/employer/route.ts
approveEmployees/route.ts
removeEmployees/route.ts
Security Benefits
Type Safety Improvements
as PostBody)unknowntypes passed to business logicError Message Quality
Before:
{ "error": {} }After:
{ "success": false, "error": "Validation Error", "message": "employeeId: Employee ID must be a positive number" }Testing
Files Modified
src/lib/validation.ts: Added 3 new schemassrc/app/api/signup/employee/route.tssrc/app/api/signup/employer/route.tssrc/app/api/approveEmployees/route.tssrc/app/api/removeEmployees/route.tsImpact
Before:
After:
Breaking Changes
None - validation simply rejects previously invalid requests that would have caused errors downstream.
Related
This PR complements but is independent from the error handling standardization work, focusing specifically on input validation and type safety.
This improvement significantly enhances API reliability, security, and developer experience through proper input validation and type safety.