Skip to content

feat(api): implement runtime zod request validation - #64

Merged
codebestia merged 1 commit into
codebestia:mainfrom
0xDeon:feat/zod-validation-middleware-20
May 29, 2026
Merged

feat(api): implement runtime zod request validation#64
codebestia merged 1 commit into
codebestia:mainfrom
0xDeon:feat/zod-validation-middleware-20

Conversation

@0xDeon

@0xDeon 0xDeon commented May 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Replaces unsafe manual if (!field) guards in auth routes with a reusable Zod-powered validation middleware that returns structured 400 responses.

Validation Architecture

A single validate(schema) middleware factory in src/middleware/validate.ts wraps any Zod schema. On failure it returns:

{ "error": "Validation failed", "issues": [{ "field": "walletAddress", "message": "walletAddress is required" }] }

On success it replaces req.body with the parsed (type-safe) data and calls next().

Middleware Design

  • validate(schema) returns a standard Express RequestHandler (3-arg)
  • Uses schema.safeParse() — never throws
  • issues[].field is the dot-joined Zod path (e.g. nested.field)

Routes Migrated

Route Schema
POST /auth/challenge ChallengeSchema — requires walletAddress: string
POST /auth/verify VerifySchema — requires walletAddress, signature, nonce

Schemas live in src/schemas/auth.schemas.ts for maintainability.

Tests Added

src/__tests__/validate.test.ts — 6 test cases:

  • Valid input passes through with correct body
  • Missing required field → 400 structured error
  • Wrong type (string for number) → 400 structured error
  • Empty body → 400 structured error
  • Issues array entries have field + message keys
  • Middleware returns a 3-arg Express handler

All 17 tests pass. Lint clean.

Fixes #20

- Add reusable validate(schema) Express middleware returning structured 400s
- Add ChallengeSchema and VerifySchema for auth routes
- Replace manual if(!field) guards in auth.ts with validate middleware
- Structured error format: { error, issues: [{ field, message }] }
- Add supertest + 6 tests covering valid input, missing fields, wrong types
@drips-wave

drips-wave Bot commented May 29, 2026

Copy link
Copy Markdown

@0xDeon 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

@codebestia codebestia left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
Thanks

@codebestia
codebestia merged commit 525d796 into codebestia:main May 29, 2026
codebestia added a commit that referenced this pull request Jun 26, 2026
feat(api): implement runtime zod request validation
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.

[BACKEND] Add Zod request validation middleware

2 participants