Skip to content

fix: ensure fieldName is passed to custom validation logic functions#2127

Open
mwg-ofx wants to merge 2 commits intoTanStack:mainfrom
mwg-ofx:dynamic-validation-fieldname
Open

fix: ensure fieldName is passed to custom validation logic functions#2127
mwg-ofx wants to merge 2 commits intoTanStack:mainfrom
mwg-ofx:dynamic-validation-fieldname

Conversation

@mwg-ofx
Copy link
Copy Markdown

@mwg-ofx mwg-ofx commented Apr 16, 2026

🎯 Changes

This was always part of the types for custom validation functions (see #1622), it just wasn't wired up. This update is pretty straightforward - it just wires up the field, and adds a test to prove that custom field-level dynamic validation is now possible. The example in the test mostly follows a basic version of what's outlined in #1861.

Fixes #1861

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • New Features

    • Custom validation logic now receives the relevant field name (including for related/linked fields), so validators can identify which field they apply to.
  • Tests

    • Added tests for custom field-level validation covering dynamic sync and async behaviors (blur- and submit-triggered scenarios).

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 16, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: be0a602f-a824-48ed-bfec-91bde63cf586

📥 Commits

Reviewing files that changed from the base of the PR and between 72c0b37 and 58473fe.

📒 Files selected for processing (1)
  • packages/form-core/tests/DynamicValidation.spec.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/form-core/tests/DynamicValidation.spec.ts

📝 Walkthrough

Walkthrough

Threads fieldName through validation: FieldApi now supplies fieldName when building sync/async validator arrays, utils forward fieldName into the validationLogic event, ValidationLogicValidatorsFn is exported, and tests were added for dynamic field-level validation using event.fieldName.

Changes

Cohort / File(s) Summary
Release Metadata
.changeset/whole-views-wear.md
Added a changeset noting fieldName is now passed into custom validation logic functions.
Field API & Validator Assembly
packages/form-core/src/FieldApi.ts
FieldApi includes fieldName when constructing sync and async validator arrays for the current field and linked fields.
Validation Utilities
packages/form-core/src/utils.ts
getSyncValidatorArray / getAsyncValidatorArray signatures accept optional fieldName; the validationLogic event now receives fieldName for both sync and async flows.
Type Export
packages/form-core/src/ValidationLogic.ts
ValidationLogicValidatorsFn interface changed from file-private to exported for external consumption.
Tests
packages/form-core/tests/DynamicValidation.spec.ts
Added tests covering customised field-level validation logic that uses event.fieldName, for both sync and async dynamic validation scenarios.

Sequence Diagram(s)

sequenceDiagram
  participant FieldApi as FieldApi
  participant Utils as getSync/AsyncValidatorArray
  participant ValidationLogic as validationLogic
  participant Validator as ValidatorFns

  FieldApi->>Utils: build validator array (include fieldName)
  Utils->>ValidationLogic: call validationLogic(event { type, async, fieldName })
  ValidationLogic->>Validator: decide/append validators (may use fieldName)
  Validator-->>ValidationLogic: return validators
  ValidationLogic-->>Utils: provide final validator list
  Utils-->>FieldApi: validators applied / validation results
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 I hopped through lines where names were bare,

I stitched fieldName with gentle care,
From field to logic, snug and bright,
Validators now know whose plight,
Hooray — bugs scurry out of sight.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: ensuring fieldName is passed to custom validation logic functions, which aligns with all modifications in the changeset.
Description check ✅ Passed The PR description follows the template with completed checklist items, clear explanation of changes, and references to related issues; all required sections are present.
Linked Issues check ✅ Passed The PR fully implements the requirements from issue #1861 by wiring up fieldName to custom validation logic, enabling the dynamic validation use case, and includes comprehensive tests.
Out of Scope Changes check ✅ Passed All changes are directly related to passing fieldName through the validation logic pipeline and testing the feature; no unrelated modifications are present.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Copy link
Copy Markdown

@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: 1

🧹 Nitpick comments (1)
packages/form-core/tests/DynamicValidation.spec.ts (1)

238-333: Consider adding one async linked-field case to prevent fieldName regressions.

A small async linked validation scenario asserting props.event.fieldName for the linked field would lock in the async path as well.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/form-core/tests/DynamicValidation.spec.ts` around lines 238 - 333,
Add an async linked-field scenario to the existing test to cover the async path
that depends on props.event.fieldName: update the validationLogic used in the
spec to include a case where a validator is async and references a linked field
(so props.event.fieldName is set for that linked field) and then exercise that
path by triggering the linked-field event (e.g., blur/change/submit on the
linked field) and awaiting the validation; specifically modify the test that
defines validationLogic, FieldApi instances and their validators ('onDynamic')
to add a linked async validator and assertions that await and assert errorMap
for the linked field to ensure the props.event.fieldName branch is exercised.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/form-core/src/FieldApi.ts`:
- Around line 1828-1832: Linked-field async validators are receiving the
triggering field's name (this.name) instead of the linked field's name; update
the linked-field validation loop where getAsyncValidatorArray is called (using
field.options, field.form, validationLogic) to set fieldName to the linked
field's identifier (e.g., linkedField.name or the loop variable representing the
linked field) rather than this.name so event.fieldName in the custom async
validationLogic reflects the linked field.

---

Nitpick comments:
In `@packages/form-core/tests/DynamicValidation.spec.ts`:
- Around line 238-333: Add an async linked-field scenario to the existing test
to cover the async path that depends on props.event.fieldName: update the
validationLogic used in the spec to include a case where a validator is async
and references a linked field (so props.event.fieldName is set for that linked
field) and then exercise that path by triggering the linked-field event (e.g.,
blur/change/submit on the linked field) and awaiting the validation;
specifically modify the test that defines validationLogic, FieldApi instances
and their validators ('onDynamic') to add a linked async validator and
assertions that await and assert errorMap for the linked field to ensure the
props.event.fieldName branch is exercised.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 18dbf798-0f75-4e1b-8636-58dd478bdd92

📥 Commits

Reviewing files that changed from the base of the PR and between 254f157 and ad2cd71.

📒 Files selected for processing (5)
  • .changeset/whole-views-wear.md
  • packages/form-core/src/FieldApi.ts
  • packages/form-core/src/ValidationLogic.ts
  • packages/form-core/src/utils.ts
  • packages/form-core/tests/DynamicValidation.spec.ts

Comment thread packages/form-core/src/FieldApi.ts
This was always part of the types for custom validation functions, it
just wasn't wired up. This update is pretty straightforward - it just
wires up the field, and adds a test to prove that custom field-level
dynamic validation is now possible. The example in the test mostly
follows a basic version of what's outlined in TanStack#1861.

Fixes TanStack#1861
@mwg-ofx mwg-ofx force-pushed the dynamic-validation-fieldname branch from ad2cd71 to 72c0b37 Compare April 16, 2026 03:33
@crutchcorn
Copy link
Copy Markdown
Member

Whoops! My bad for having this bug in the first place! Good catch.

The code generally LGTM at a quick glance, but I want to have another maintainer take a closer look at the tests in particular since I'm deep in Form Group land and just got an email so I thought I'd take a look. I'll have it prioritized given the quality of the PR.

Thanks!

@nx-cloud
Copy link
Copy Markdown

nx-cloud bot commented Apr 16, 2026

View your CI Pipeline Execution ↗ for commit 58473fe

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 44s View ↗

☁️ Nx Cloud last updated this comment at 2026-04-16 15:43:19 UTC

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 16, 2026

More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@2127

@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@2127

@tanstack/form-devtools

npm i https://pkg.pr.new/@tanstack/form-devtools@2127

@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@2127

@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@2127

@tanstack/react-form-devtools

npm i https://pkg.pr.new/@tanstack/react-form-devtools@2127

@tanstack/react-form-nextjs

npm i https://pkg.pr.new/@tanstack/react-form-nextjs@2127

@tanstack/react-form-remix

npm i https://pkg.pr.new/@tanstack/react-form-remix@2127

@tanstack/react-form-start

npm i https://pkg.pr.new/@tanstack/react-form-start@2127

@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@2127

@tanstack/solid-form-devtools

npm i https://pkg.pr.new/@tanstack/solid-form-devtools@2127

@tanstack/svelte-form

npm i https://pkg.pr.new/@tanstack/svelte-form@2127

@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@2127

commit: 58473fe

@sentry
Copy link
Copy Markdown

sentry bot commented Apr 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.25%. Comparing base (6892ed0) to head (58473fe).
⚠️ Report is 167 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2127      +/-   ##
==========================================
- Coverage   90.35%   90.25%   -0.10%     
==========================================
  Files          38       49      +11     
  Lines        1752     2043     +291     
  Branches      444      532      +88     
==========================================
+ Hits         1583     1844     +261     
- Misses        149      179      +30     
  Partials       20       20              

☔ 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.

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.

Validation Logic Fn: event.fieldName seems to always be undefined despite existing in props

2 participants