fix(form-core): infer form error types from onServerValidate in formOptions - #2386
fix(form-core): infer form error types from onServerValidate in formOptions#2386cpruijsen wants to merge 1 commit into
Conversation
…ptions Specify onServerValidate on FormOptions so shared formOptions flow the server validator return type into form-level errors. Fixes TanStack#1325
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (8)
💤 Files with no reviewable changes (1)
Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review. 📝 WalkthroughWalkthroughThe form API now supports ChangesServer validation error inference
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix Merge Risk: ⚪ Minimal · up to The server-validation error inference change is covered by the shared form-options flow and its example integration, with no actionable merge risk identified. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Changes
Optional
onServerValidateonFormOptions. Put it on sharedformOptions(the same functioncreateServerValidatealready takes).FormApidoes not run it;createServerValidatestill does.form.state.errorsaftermergeFormof Next/Remix/Start server validation was typed asnever[], soformErrors.map((error) => …)did not see the server validator's return type (issue #1325; originally reported asvoid | undefinedon form-core v0.43.2).TOnServerexisted onFormOptions/FormApibut nothing on the options object bound it.onServerValidatelived only oncreateServerValidate, anduseForm'stransformis(data: unknown) => unknown, so spreading sharedformOptionsinto the client form could not infer server errors.Decided: bind
TOnServeronFormOptionsrather than retypingmergeForm/useTransform, the same API ascreateServerValidateand the smallest reversible change. Alternative was threadingServerFormStatethroughtransform; that cannot work whiletransformis(data: unknown) => unknown. Can switch if this should stay adapter-only or v2-only.Checklist
pnpm test:pr.@tanstack/form-coreand@tanstack/react-formvitest --typecheck,form-coreformOptions.spec.ts, and both packages'test:types:ts59.Release Impact
Test plan
formOptions({ onServerValidate: () => 'msg' as const })thennew FormApi(formOpts)/useForm({ ...formOpts }):state.errorsis'msg'[],errorMap.onServeris'msg' | undefined.onServerValidate,state.errorsstaysnever[]when there are no form-level validators.createServerValidate({ ...formOpts })still typechecks whenformOptsalready includesonServerValidate.key={error}needs no cast; field-levelas stringis unchanged.onServerValidate(submit still usescreateServerValidateon the server).Summary by CodeRabbit
New Features
Documentation
Bug Fixes