Feature/zod validation middleware - #139
Open
dubemoyibe-star wants to merge 3 commits into
Open
Conversation
|
@dubemoyibe-star is attempting to deploy a commit to the Jaja's projects Team on Vercel. A member of the Team first needs to authorize it. |
devJaja
self-requested a review
July 20, 2026 23:36
Contributor
|
Resolve the conflicts @dubemoyibe-star |
Contributor
|
Nice Implementation @dubemoyibe-star |
Author
Ok then |
Contributor
|
@dubemoyibe-star |
Contributor
|
@dubemoyibe-star Resolve the conflicts please |
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.
Zod Schema Validation Middleware for API Request Bodies
Overview
API endpoints in
backend/src/api/routes/andbackend/src/api/app.tsperformedad-hoc, inline validation (
if (!taskName || !prompt) ...). This led toinconsistent validation, missing edge cases, and error responses that leaked
internal Zod details (
parse.error.flatten()).This change introduces a reusable
validate()middleware backed by centralizedZod schemas, enforcing a consistent
{ error, details }400 contract acrossevery API endpoint. Input is sanitized (strings trimmed, numbers coerced) and
TypeScript types are derived from the schemas via
z.infer— no manualduplication.
zod(^3.23.8) was already a transitive dependency inpackage.json; no newdependency was required.
Files
New
backend/src/api/schemas/common.schema.tsTaskStatusSchema,PaginationQuerySchema,IdParamSchema,trimmedString()helper. Exports inferred types.backend/src/api/schemas/task.schema.tsCreateTaskSchema,TaskQuerySchema,TaskIdParamSchema(+ inferred input types).backend/src/api/schemas/agent.schema.tsRegisterAgentSchema,AgentListQuerySchema,AgentIdParamSchema(+ inferred input types).backend/src/api/middleware/validate.tsvalidate({ body, query, params })middleware.backend/tests/validation.test.tsModified
backend/src/api/app.tsPOST/GET/DELETE /api/tasksnow usevalidate()+ shared task schemas, replacing inline checks.backend/src/api/routes/agents.ts:idparams, the agent list query, and the register body now usevalidate()+ shared agent schemas.backend/src/api/routes/tasks.tsvalidate()+ shared task schemas (consistent withapp.ts).The
validate()middlewarereq.body,req.query, and/orreq.paramsagainst the suppliedZod schemas.
400and a structured body:{ "error": "Validation failed", "details": [{ "path": "body.prompt", "message": "prompt is required" }] }path+ human-readablemessage.trimmedStringstrips surroundingwhitespace (and rejects whitespace-only),
z.coerce.number()turns querystrings into real numbers. Parsed values are written back onto
reqsodownstream handlers receive the cleaned, coerced data.
Schema highlights
CreateTaskSchema:prompt(trimmed, 1–10000 chars),maxBudgetXLM(
>= 0.1), optionalagentPreferences[]andwalletPublicKey.TaskQuerySchema: pagination (page,pageSizecoerced + clamped 1–100),optional
statusenum,sortenum (defaultcreatedAt:desc), optionalq.RegisterAgentSchema: trimmedagentId, non-emptycapabilities[],pricingXLM >= 0,endpointURL, trimmedstellarPublicKey.AgentListQuerySchema: optionalcapability, coercedminReputation/maxPriceXLM,statusenum.Acceptance criteria status
validate()middleware exists and is reusable.{ error: string, details: FieldError[] }.z.infer) — no manual duplication.app.ts,routes/agents.ts,routes/tasks.ts).Validation
npx tsc -p tsconfig.json --noEmit→ passes.npx jest tests/validation.test.ts→ 6/6 pass (covers 400 shape, trimming,numeric coercion, enum rejection, agent registration).
tests/middleware.test.tsand other non-DB suites continue to pass.Known environment limitation
The SQLite-backed suites (
tests/tasks.test.ts,tests/agents.test.ts,tests/replay.test.ts,tests/e2e/stream.test.ts) currently fail to loadin this sandbox because the native
better-sqlite3addon is not compiled forthe installed Node 24 runtime (no Visual Studio build tools available to rebuild
it). This is pre-existing and unrelated to this change — it occurs at
createApp → createEventStorebefore any validation runs (confirmed viagit stashon the original code). The validation logic is covered directly bytests/validation.test.ts, which exercises the real middleware and schemas overHTTP without needing SQLite.
Examples
Before:
After:
Closes #106