Skip to content

chore(server): add Zod request validation to all document endpoints - #88

Merged
Alimedhat000 merged 1 commit into
developfrom
chore/52-document-validation
Aug 26, 2026
Merged

chore(server): add Zod request validation to all document endpoints#88
Alimedhat000 merged 1 commit into
developfrom
chore/52-document-validation

Conversation

@Alimedhat000

Copy link
Copy Markdown
Owner

Closes #52.

Extends the validation pattern introduced for auth + collaborators (#50/#80) to every document route, so malformed input returns clean 400/404s instead of Prisma-driven 500s. Validation audit findings (2026-08-25) below — this PR implements all of them.

Server plan

1. Body schemas (new files in server/src/validations/)

  • createDocument.schema.ts: { title: string(1..200), content?: string(max ~1MB), isPublic?: boolean(default false) } — use .trim() on title
  • updateDocument.schema.ts: partial variant (all optional, same bounds)
  • updateDocSettings.schema.ts: { allowSelfJoin: boolean }
  • Add .max() to existing schemas while here: email ≤254, username ≤50 + charset regex /^[a-zA-Z0-9_.-]+$/, password ≤128, fullName ≤100; align server password floor 6 → 8 to match client (client/src/lib/auth.ts)
  • Share-link query: replace manual permission check with validate({ query }) enum

2. Param schemas

  • Shared z.string().uuid() param objects for id, requestId, userId → wire via validate({ params }) on routes 9–19 of document.router.ts (kills non-UUID → 500 class)

3. Router wiring

Apply validate({ body, params, query }) per route in document.router.ts. Keep controller destructuring as-is where it already matches parsed output.

Client plan (matching constraints so users never hit a 400)

  • maxLength attributes: title inputs 200, email 254, username 50, password 128 (register/login forms, NewDocumentFormBody, rename-modal, collaborators-dropdown email input)
  • Mirror server schema bounds in client zod schemas (client/src/lib/auth.ts) and add title max to new-document/rename flows

Test plan

  • RED first per schema: wrong types / oversized strings / bad UUIDs assert 400 (not 500); happy paths unchanged
  • Extend server/test/document.test.ts; keep suite serial-compatible
  • Full gates: lint → typecheck → build → server tests; client storybook suite

Out of scope

Document routes accepted unvalidated bodies and params: malformed ids
surfaced Prisma errors as 500s, non-boolean flags reached the DB layer,
and title/permission values were never length- or enum-checked. Auth
schemas also lagged client-side bounds (server allowed 6-char passwords
while the client requires 8).

Add body/param/query schemas wired through the existing validate()
middleware for every /document route, replace the manual share-link
permission check with a query enum schema, and harden register/login/
addCollaborator bounds (email <=254, username <=50 + charset,
password 8..128, fullName <=100). Mirror those bounds in the client
(zod schemas + maxLength attributes) so users never hit a 400.

Closes #52. Related: #37 (rate limiting), #83 (enumeration hardening).
@Alimedhat000
Alimedhat000 marked this pull request as ready for review August 25, 2026 20:17
@Alimedhat000
Alimedhat000 merged commit 3b62d69 into develop Aug 26, 2026
2 checks passed
@Alimedhat000 Alimedhat000 linked an issue Aug 26, 2026 that may be closed by this pull request
@Alimedhat000
Alimedhat000 deleted the chore/52-document-validation branch August 28, 2026 01:28
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 Zod request validation to all document endpoints

1 participant