Skip to content

Update Zod from v3 to v4 and refactor validation schemas#433

Merged
SvenVw merged 5 commits into
developmentfrom
FDM432
Jan 28, 2026
Merged

Update Zod from v3 to v4 and refactor validation schemas#433
SvenVw merged 5 commits into
developmentfrom
FDM432

Conversation

@SvenVw
Copy link
Copy Markdown
Collaborator

@SvenVw SvenVw commented Jan 28, 2026

Summary by CodeRabbit

  • Chores

    • Updated Zod validation library from v3 to v4.
  • Refactor

    • Unified validation error handling across forms to the new Zod v4 style (dynamic error callbacks / "error" payloads).
    • Improved date/number preprocessing and default initialization to reduce spurious errors and better handle string input.
    • Preserved existing validation rules and user-facing message texts while changing how validation errors are reported.

✏️ Tip: You can customize this high-level summary in your review settings.

Closes #432

@SvenVw SvenVw self-assigned this Jan 28, 2026
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jan 28, 2026

🦋 Changeset detected

Latest commit: 7161c86

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@svenvw/fdm-app Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@SvenVw SvenVw requested a review from BoraIneviNMI January 28, 2026 12:32
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 28, 2026

Walkthrough

Migrates fdm-app to Zod v4 and updates many Zod schemas to use the v4 error API (replacing required_error/invalid_type_error/message with error callbacks), converts some z.coerce usages to z.preprocess, and changes some .default() to .prefault(); package.json zod dependency bumped.

Changes

Cohort / File(s) Summary
Release Management
​.changeset/many-sails-hang.md
Added changeset documenting a patch bump for the migration.
Dependency Update
fdm-app/package.json
Bumped zod from ^3.25.76 to ^4.3.6.
Fertilizer Schemas
fdm-app/app/components/blocks/fertilizer/formschema.tsx, fdm-app/app/components/blocks/fertilizer-applications/formschema.tsx
Replaced per-constraint required_error/invalid_type_error/message with error callbacks; switched some coercions to preprocess + z.number()/z.date(); moved some defaultprefault; refinement error payloads now use error with paths.
Cultivation / Rotation / Harvest
fdm-app/app/components/blocks/cultivation/schema.ts, fdm-app/app/components/blocks/rotation/schema.ts, fdm-app/app/components/blocks/harvest/schema.tsx
Date fields now use z.preprocess for string→Date and conditional error callbacks; .default(null).prefault(null) for end-date(s); refinements return error entries with paths.
Field & Organic Certification
fdm-app/app/components/blocks/field/schema.tsx, fdm-app/app/components/blocks/organic-certification/schema.ts
Replaced required_error/message with conditional error handlers; date fields use preprocess and error callbacks.
Soil Schemas & Uploads
fdm-app/app/components/blocks/soil/formschema.tsx, fdm-app/app/components/blocks/soil/form-upload.tsx, fdm-app/app/components/blocks/mijnpercelen/form-upload.tsx
Switched numeric/date validators to error callback pattern and preprocess; file refine validators now return error instead of message.
Route / Page Schemas
fdm-app/app/routes/... (farm.*, organization.*, farm.create._index.tsx, signin._index.tsx, welcome.tsx)
Systematically migrated validators to error callbacks, converted some z.coerce usages to preprocess + z.number()/z.date(), and changed some .default().prefault().
Integration Logging
fdm-app/app/integrations/nmi.ts
Zod error logging changed from .format() to z.treeifyError() for validation failures.

Sequence Diagram(s)

(omitted)

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • gerardhros

Poem

🐰 I hopped from v3 into v4 tonight,
Errors now whisper only when input's light,
Prefault seeds and preprocess leaves,
Schemas trimmed like winter eaves,
A rabbit cheers the migration — soft and bright.

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: updating Zod from v3 to v4 and refactoring validation schemas to align with v4 requirements.
Linked Issues check ✅ Passed The PR successfully addresses issue #432 by updating zod from ^3.25.76 to ^4.3.6 and systematically refactoring validation schemas across all affected files to use v4 patterns (error callbacks, preprocess, prefault).
Out of Scope Changes check ✅ Passed All changes are directly related to the Zod v3 to v4 migration. Minor formatting adjustments in some files and import cleanup are incidental to the migration effort.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch FDM432

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot changed the title @coderabbitai Update Zod from v3 to v4 and refactor validation schemas Jan 28, 2026
@coderabbitai coderabbitai Bot added dependencies Pull requests that update a dependency file fdm-app labels Jan 28, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Fix all issues with AI agents
In `@fdm-app/app/components/blocks/harvest/schema.tsx`:
- Around line 25-163: The numeric harvest/yield fields (b_lu_yield,
b_lu_yield_fresh, b_lu_yield_bruto, b_lu_dm, b_lu_tarra, b_lu_uww, b_lu_moist
and b_lu_cp) currently use z.int() which fails on string inputs from FormData;
update each schema block to use z.coerce.number().int() (keeping the existing
z.preprocess((val)=> val===""? undefined: val) and the same chained validators
like .positive(), .max(), .min(), .finite(), .optional()) so strings are coerced
to numbers before integer validation—follow the pattern used by
b_lu_n_harvestable for guidance.

In `@fdm-app/app/routes/welcome.tsx`:
- Around line 49-65: The firstname and surname schema entries in FormSchema use
z.coerce.string(), which coerces undefined to "undefined" and bypasses your
undefined checks; change both to use z.string() instead (preserve the existing
custom error callbacks and .min(1) validators) so the undefined-checking error
functions for firstname and surname run correctly and values like "undefined"
are not accepted.

In `@fdm-app/package.json`:
- Line 90: You added "zod": "^4.3.6" in package.json; validate and adapt code
for Zod v4 compatibility: run the zod-v3-to-v4 codemod across the repo, update
any schema usages that rely on deprecated options (e.g.,
message/required_error), adjust places handling ZodError formatted output,
verify .default() semantics and string validators (e.g., z.ip() → z.ipv4()), and
confirm TypeScript types with `@hookform/resolvers` (ensure you’re on v5.2.2 and
fix any type mismatches in form schemas and error handling like useForm resolver
integrations). After changes, run full typecheck and form-related tests/manual
flows to confirm validation and inference remain correct.

Comment thread fdm-app/app/components/blocks/harvest/schema.tsx Outdated
Comment thread fdm-app/app/routes/welcome.tsx
Comment thread fdm-app/package.json
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.07%. Comparing base (f154ed0) to head (7161c86).
⚠️ Report is 6 commits behind head on development.

Additional details and impacted files
@@             Coverage Diff              @@
##           development     #433   +/-   ##
============================================
  Coverage        88.07%   88.07%           
============================================
  Files               91       91           
  Lines             4621     4621           
  Branches          1492     1492           
============================================
  Hits              4070     4070           
  Misses             551      551           
Flag Coverage Δ
fdm-calculator 88.76% <ø> (ø)
fdm-core 86.77% <ø> (ø)
fdm-data 92.12% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai Bot added the branch:development Issue only affecting development, not the main branch (yet) label Jan 28, 2026
Copy link
Copy Markdown
Collaborator

@BoraIneviNMI BoraIneviNMI left a comment

Choose a reason for hiding this comment

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

It looks good. I see all the necessary required_error and invalid_type_error keys in the Zod 3 code are taken care of. I have one small suggestion.

Comment thread fdm-app/app/components/blocks/harvest/schema.tsx Outdated
@SvenVw SvenVw merged commit 7802fa5 into development Jan 28, 2026
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

branch:development Issue only affecting development, not the main branch (yet) dependencies Pull requests that update a dependency file fdm-app

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate to Zod v4

2 participants