Skip to content

feat(form-core): add <FieldMeta>.isValid #1422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

LeCarbonator
Copy link
Contributor

This meta property is a shortened alternative to checking
field.state.meta.errors.length === 0. Since FormApi already has this
property, a FieldApi variant wouldn't hurt to be more clear.

This PR is based on Balastrong's suggestion on Discord.

Copy link

nx-cloud bot commented Apr 16, 2025

View your CI Pipeline Execution ↗ for commit f671ab2.

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 1m 44s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 22s View ↗

☁️ Nx Cloud last updated this comment at 2025-04-20 10:36:01 UTC

Copy link

pkg-pr-new bot commented Apr 16, 2025

More templates

@tanstack/angular-form

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

@tanstack/form-core

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

@tanstack/react-form

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

@tanstack/lit-form

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

@tanstack/solid-form

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

@tanstack/svelte-form

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

@tanstack/vue-form

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

commit: f671ab2

@LeCarbonator LeCarbonator marked this pull request as draft April 16, 2025 19:07
@LeCarbonator
Copy link
Contributor Author

For some reason, refactoring this FormApi check fails horribly. Does someone know why?

- const isFieldsValid = !fieldMetaValues.some(
-           (field) =>
-             field?.errorMap &&
-             isNonEmptyArray(Object.values(field.errorMap).filter(Boolean)),
-         )

+ const isFieldsValid = fieldMetaValues.every(
+          (field) => field === undefined || field.isValid,
+        )

@Pascalmh
Copy link
Contributor

Pascalmh commented Apr 16, 2025

Does this work?

const isFieldsValid = fieldMetaValues
  .filter(Boolean)
  .every((field) => field.isValid)

@LeCarbonator
Copy link
Contributor Author

No, but it's clear that the meta needs to be adjusted in some places. I'll tinker with it tomorrow.

@LeCarbonator
Copy link
Contributor Author

It's wild. I added checks wherever field errors or errorMap were tested for and it worked no problem. However, trying to rewrite isFieldsValid to use this boolean fails at 8 locations. I'll have to spend some more time figuring out how store works or something related to that

@LeCarbonator
Copy link
Contributor Author

Does this work?

const isFieldsValid = fieldMetaValues
  .filter(Boolean)
  .every((field) => field.isValid)

It works now! This code wasn't the problem, but my implementation of isValid.

Copy link

codecov bot commented Apr 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.00%. Comparing base (0c79f24) to head (f671ab2).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1422      +/-   ##
==========================================
+ Coverage   88.83%   89.00%   +0.17%     
==========================================
  Files          31       31              
  Lines        1379     1401      +22     
  Branches      347      347              
==========================================
+ Hits         1225     1247      +22     
  Misses        137      137              
  Partials       17       17              

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

this is a shortened counterpart to checking
field.state.meta.errors.length > 0
@LeCarbonator LeCarbonator marked this pull request as ready for review April 18, 2025 10:00
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.

2 participants