Skip to content
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

feat: return an array of errors from a single validator (custom & schema library) #1090

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

Conversation

Balastrong
Copy link
Member

@Balastrong Balastrong commented Dec 29, 2024

This PR addresses the frequent request of being able to return multiple errors from a single validator.

Validator functions (aka what you put inside validators: { onChange: () => ... }) can now return a ValidationResult that is undefined | false | null | ValidationError | ValidationError[] with ValidationError being a string.

Existing validators returning a single string will keep working, additionally you can now also return an array of strings.

As a side effect, validators defined with schema libraries will no longer be cut to the first error by default, but all errors will be shown instead.

Breaking Changes

Being able to return an array of strings from a validator means errorMap.onChange & co will no longer be a string but an array.

Documentation has been updated showing the quickest change to keep the previous behaviour:

- {meta.errorMap.onChange ? (<em>{meta.errorMap.onChange}</em>) : null}
+ {meta.errorMap.onChange?.length ? (<em>{meta.errorMap.onChange[0]}</em>) : null}

In case you were using meta.errors to display all your errors there won't be any breaking change since this value will always be a (flat) aggregate array of errors.

Feedback is welcome!

Copy link

nx-cloud bot commented Dec 29, 2024

View your CI Pipeline Execution ↗ for commit 86c9943.

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

☁️ Nx Cloud last updated this comment at 2024-12-29 17:30:54 UTC

Copy link

pkg-pr-new bot commented Dec 29, 2024

Open in Stackblitz

More templates

@tanstack/angular-form

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

@tanstack/lit-form

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

@tanstack/form-core

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

@tanstack/react-form

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

@tanstack/valibot-form-adapter

npm i https://pkg.pr.new/@tanstack/valibot-form-adapter@1090

@tanstack/solid-form

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

@tanstack/vue-form

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

@tanstack/yup-form-adapter

npm i https://pkg.pr.new/@tanstack/yup-form-adapter@1090

@tanstack/zod-form-adapter

npm i https://pkg.pr.new/@tanstack/zod-form-adapter@1090

commit: 86c9943

Copy link

codecov bot commented Dec 29, 2024

Codecov Report

Attention: Patch coverage is 84.84848% with 10 lines in your changes missing coverage. Please review.

Project coverage is 88.43%. Comparing base (be7f9bf) to head (86c9943).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
packages/form-core/src/FieldApi.ts 88.23% 2 Missing ⚠️
packages/form-core/src/FormApi.ts 94.28% 1 Missing and 1 partial ⚠️
...ages/react-form/src/nextjs/createServerValidate.ts 0.00% 2 Missing ⚠️
...kages/react-form/src/remix/createServerValidate.ts 0.00% 2 Missing ⚠️
...ages/react-form/src/start/createServerValidate.tsx 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1090      +/-   ##
==========================================
+ Coverage   86.60%   88.43%   +1.83%     
==========================================
  Files          29       29              
  Lines        1172     1159      -13     
  Branches      287      281       -6     
==========================================
+ Hits         1015     1025      +10     
+ Misses        144      124      -20     
+ Partials       13       10       -3     

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

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.

1 participant