Skip to content

feat: implement validation middleware tests with tests and docs#152

Open
Johnsmichael150 wants to merge 2 commits intoTalenttrust:mainfrom
Johnsmichael150:feature/backend-40-validation-middleware-tests
Open

feat: implement validation middleware tests with tests and docs#152
Johnsmichael150 wants to merge 2 commits intoTalenttrust:mainfrom
Johnsmichael150:feature/backend-40-validation-middleware-tests

Conversation

@Johnsmichael150
Copy link
Copy Markdown

Summary

Adds comprehensive unit tests for all three validation middleware layers and the core validation engine. Also fixes a missing zod runtime dependency.

Changes

File Change
src/middleware/validation.test.ts New — 15 unit tests for validateRequest, validateParams, validateQuery
src/middleware/requestValidation.test.ts New — 22 unit tests for createRequestValidationMiddleware
src/middleware/validate.middleware.test.ts Expanded — 11 tests (was 3) for validateSchema
src/validation/requestSchema.test.ts Expanded — 28 tests (was 7) for validateSegment
docs/backend/validation-middleware-tests.md New — test coverage and security notes
package.json / package-lock.json Added missing zod runtime dependency

Test Coverage

validateRequest / validateParams / validateQuery (validation.ts)

  • Valid input calls next() with no arguments
  • ZodError returns 400 with field-level details
  • Non-Zod errors return 400 with generic message (no stack leakage)
  • Type coercion attack (string-as-number) rejected
  • .strict() schemas reject extra fields

createRequestValidationMiddleware (requestValidation.ts)

  • Valid inputs across body, query, params
  • Partial schemas (body-only, query-only, etc.)
  • Unknown field rejection on all three segments
  • Required field enforcement
  • Type mismatches: string/number/boolean
  • Constraint violations: minLength, maxLength, min, max, enum, pattern
  • Non-object segments (array, null) rejected
  • Aggregated errors from all segments in one response

validateSchema (validate.middleware.ts)

  • Combined body/query/params validation
  • Strict schema extra-field rejection
  • Invalid UUID in params
  • Non-Zod errors forwarded to next(error), not serialized to client

validateSegment (requestSchema.ts)

  • NaN, Infinity, -Infinity rejected as non-finite
  • null treated as missing for required fields
  • Array and null segments rejected before field validation
  • Segment name propagated correctly in all error messages
  • Empty schema rejects any incoming field

Security Notes

  • Unknown fields are always rejected — prevents field injection and prototype pollution
  • Strict type checking prevents query-string type coercion attacks
  • Non-finite numbers rejected to prevent downstream arithmetic errors
  • Non-Zod errors never serialized to client — no stack trace leakage

How to Test

npm test -- --testPathPatterns="src/middleware/validation\.test$|src/middleware/requestValidation\.test$|src/middleware/validate\.middleware\.test$|src/validation/requestSchema\.test$"

Related

- Add unit tests for validateRequest, validateParams, validateQuery (validation.ts)
- Add unit tests for createRequestValidationMiddleware (requestValidation.ts)
- Expand validateSegment tests: NaN/Infinity, null, array, empty schema (requestSchema.ts)
- Expand validateSchema tests: strict schemas, UUID params, error forwarding (validate.middleware.ts)
- Add docs/backend/validation-middleware-tests.md
- Install missing zod dependency
@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Apr 2, 2026

@Johnsmichael150 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

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.

[Mike] 40. Validation middleware tests

2 participants