Skip to content

Introduce Zod validation framework - #15

Merged
Ardnived merged 4 commits into
mainfrom
feature/zod-validation
Jan 27, 2026
Merged

Ardnived merged 4 commits into
mainfrom
feature/zod-validation

Conversation

@Ardnived

Copy link
Copy Markdown
Contributor

Summary

  • Adds Zod 4 as the schema validation library for runtime type safety at critical data boundaries
  • Validates route parameters (slugs, IDs, collections) with proper 400 error responses
  • Validates PayloadCMS API configuration (API key, base URL)
  • Adds optional schema prop to FormBuilder for Zod-powered form validation
  • Documents Zod usage patterns in CLAUDE.md

Changes

New Files

  • env.ts - Central Zod schemas for client/server environment variables
  • server/validation.ts - Shared schemas for slugs, IDs, locales, and collection types

Modified Files

  • server/payload-client.ts - Zod-based config validation with PayloadConfigError
  • server/cms-context.ts - Server environment validation
  • pages/[slug]/+data.ts - Slug validation (400 for invalid slugs)
  • pages/meditations/[id]/+data.ts - ID validation (400 for invalid IDs)
  • pages/m/[id]/+data.ts - ID validation for embed route
  • pages/preview/+data.ts - Collection and ID validation
  • components/organisms/FormBuilder/FormBuilder.tsx - Optional schema prop
  • types/vike.d.ts - ImportMetaEnv interface for typed env vars
  • CLAUDE.md - Zod validation documentation

Test plan

  • All 51 tests pass (pnpm test:run)
  • Build succeeds (pnpm build)
  • Visit /about - valid slug loads
  • Visit /ABOUT - returns 400 (invalid slug format)
  • Visit /meditations/123 - valid numeric ID loads
  • Visit /meditations/abc - returns 400 (invalid ID)
  • Visit /preview?collection=pages&id=1 - preview loads
  • Visit /preview?collection=invalid&id=1 - returns 400 (invalid collection)

Closes #14

🤖 Generated with Claude Code

- Add Zod 4 as schema validation library with @hookform/resolvers
- Create env.ts with schemas for client/server environment variables
- Create server/validation.ts with slugSchema, idSchema, collectionSchema
- Add route parameter validation to pages/[slug], meditations/[id], m/[id], preview
- Update payload-client.ts with Zod-based config validation
- Update cms-context.ts with server environment validation
- Add optional schema prop to FormBuilder for Zod form validation
- Update types/vike.d.ts with ImportMetaEnv interface
- Update CLAUDE.md with Zod validation documentation

Closes #14

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jan 27, 2026 •

Copy link
Copy Markdown

Deploying wemeditate-design with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2f9c4fa
Status: ✅  Deploy successful!
Preview URL: https://341c40f8.wm-design.pages.dev
Branch Preview URL: https://feature-zod-validation.wm-design.pages.dev

View logs

Ardnived and others added 3 commits January 27, 2026 14:45
- Remove env.ts (schemas were never used after vite-plugin-validate-env removal)
- Remove unused VALID_LOCALES and localeSchema from validation.ts
- Update CLAUDE.md to reflect simplified structure

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Vike's render() function only accepts specific status codes:
401, 403, 404, 410, 429, 500, 503. The 400 status code is
explicitly not recommended per Vike documentation.

For invalid route parameters (malformed slugs/IDs), 404 is
the appropriate code since the resource doesn't exist with
that invalid identifier.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
- Extract shared apiKeySchema and baseUrlSchema to validation.ts
- Update cms-context.ts and payload-client.ts to use shared schemas
- Add whitespace-only API key test case (restoring lost coverage)
- Use consistent "API key" error message naming

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@Ardnived
Ardnived merged commit 1cc6c43 into main Jan 27, 2026
1 check passed
@Ardnived
Ardnived deleted the feature/zod-validation branch January 27, 2026 08:41
sydevs-bot pushed a commit that referenced this pull request Nov 19, 2026
`types/vike.d.ts` sent readers to `env.ts` for the environment schemas, but
that file was removed in #15 because nothing used it. `.env.example` is
where each variable is documented now.

`lib/live-preview/frame-editor.ts` named `./messages.ts` as the home of the
unsaved-edit stream, and `document.tsx` described its endpoint check as
replacing a filter that "used to live" there. That module is gone; the
stream is `./document.tsx`, and its endpoint check is the one filter, which
is what the comment now says.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Mg9rjZcE8bs5uRJ6dsg6kw
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.

Introduce Zod Validation Framework for Environment Variables, API Responses, Forms, and Routes

1 participant