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] Field validation from form schema #925

Merged
merged 18 commits into from
Sep 18, 2024
Merged

Conversation

crutchcorn
Copy link
Member

@crutchcorn crutchcorn commented Aug 28, 2024

This PR introduces the ability to pass a schema to the validators object on a form and have it validate each specific field:

const form = useForm({
  defaultValues: {
    firstName: '',
    lastName: '',
  },
  onSubmit: async ({ value }) => {
    // Do something with form data
    console.log(value)
  },
  validators: {
    onChange: z.object({
      // Errors here are passed to the <form.Field name="firstName"> component
      firstName: z.string().min(3, 'First name must be at least 3 characters'),
      lastName: z.string().min(3, 'Last name must be at least 3 characters'),
    }),
  },
  // Add a validator to support Zod usage in Form and Field
  validatorAdapter: zodValidator({}),
})

TODO

  • Update Valibot adapter
  • Update Yup adapter
  • Fix issues with createServerValidate
  • Update docs

@crutchcorn crutchcorn requested a review from a team August 28, 2024 09:50
# Conflicts:
#	packages/react-form/src/nextjs/createServerValidate.ts
#	packages/react-form/src/start/createServerValidate.tsx
Copy link

nx-cloud bot commented Aug 28, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 198c08a. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

Copy link

pkg-pr-new bot commented Aug 28, 2024

Open in Stackblitz

More templates

@tanstack/form-core

pnpm add https://pkg.pr.new/@tanstack/form-core@925

@tanstack/angular-form

pnpm add https://pkg.pr.new/@tanstack/angular-form@925

@tanstack/lit-form

pnpm add https://pkg.pr.new/@tanstack/lit-form@925

@tanstack/react-form

pnpm add https://pkg.pr.new/@tanstack/react-form@925

@tanstack/solid-form

pnpm add https://pkg.pr.new/@tanstack/solid-form@925

@tanstack/valibot-form-adapter

pnpm add https://pkg.pr.new/@tanstack/valibot-form-adapter@925

@tanstack/vue-form

pnpm add https://pkg.pr.new/@tanstack/vue-form@925

@tanstack/yup-form-adapter

pnpm add https://pkg.pr.new/@tanstack/yup-form-adapter@925

@tanstack/zod-form-adapter

pnpm add https://pkg.pr.new/@tanstack/zod-form-adapter@925

commit: 198c08a

@crutchcorn crutchcorn mentioned this pull request Aug 28, 2024
10 tasks
Copy link
Member

@Balastrong Balastrong left a comment

Choose a reason for hiding this comment

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

Thanks for jumping so fast on expanding adapters to the new validation API :D

IIRC @jljorgenson18 was also looking for this feature, feedback is welcome!

packages/form-core/src/types.ts Outdated Show resolved Hide resolved
packages/zod-form-adapter/src/validator.ts Outdated Show resolved Hide resolved
packages/form-core/src/FormApi.ts Outdated Show resolved Hide resolved
@jljorgenson18
Copy link

Yep I was going to try to contribute here and was waiting for the field error set from form PR to be merged. I'm happy to give feedback and try it out at least.

@bompi88
Copy link

bompi88 commented Sep 13, 2024

What is the current status of this PR?

@crutchcorn
Copy link
Member Author

@bompi88 I'm... Actively working on it? Like, have made commit less than an hour ago that marked a lot of things off?

We'll get it reviewed soon, but timing just felt weird with "status?" right after I'd worked on it lol

@bompi88
Copy link

bompi88 commented Sep 13, 2024

@crutchcorn I'm sorry I did not see that. I agree that is weird 😂

Copy link
Member

@Balastrong Balastrong left a comment

Choose a reason for hiding this comment

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

All three adapters now should be able to inject errors for:

  • Primitives
  • Nested objects
  • Arrays

A couple extra notes:

  1. I kinda went bruteforce when adapting each validator's path system into our own and used the exact same approach on all of them. Pros, easier to maintain, cons, there might be a smarter (faster?) way for each validator (e.g. zod has a flatten function, could it help? I don't know)
  2. There's quite a lot of repeated code across the validators. Are we ok keeping it like that or refactoring a bit?

Anyway, I don't think these are blockers. We now have the feature working without any breaking change to our API, there's plenty of time to refine the internals later imho.

@bompi88
Copy link

bompi88 commented Sep 18, 2024

🚀

@crutchcorn
Copy link
Member Author

This LGTM @Balastrong - great work! Merging now

@crutchcorn crutchcorn merged commit 2785711 into main Sep 18, 2024
5 checks passed
@crutchcorn crutchcorn deleted the zod-validate-schema branch September 18, 2024 21:16
@bompi88
Copy link

bompi88 commented Sep 19, 2024

@crutchcorn Nice work! I love to test it out 😁 I think the squash commit message was on the wrong format [feat] and not feat: therefore no release? I might be wrong here

Balastrong pushed a commit that referenced this pull request Sep 19, 2024
    * feat: add ability to pass errors to specific fields from Zod

    * chore: code cleanup

    * chore: fix various issues

    * chore: more TSC and lib fixes

    * ci: apply automated fixes

    * feat: add Yup support to schema valdiation

    * chore: rename contexts from code review

    * ci: apply automated fixes

    * feat: support form validaton in Valibot

    * chore: fix tests for createServerValidate

    * docs: add docs for validation logic

    * feat: apply transformError to all fields

    * fix: set transformed errors on nested fields

    * feat: spread zod errors into arrays

    * feat: spread yup errors into fields, nested and arrays

    * feat: spread valibot errors into fields, nested and arrays

    ---------

    Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
    Co-authored-by: Leonardo Montini <lion.48m@gmail.com>
@Balastrong
Copy link
Member

@bompi88 you're right! :D

I fixed it now, 0.33 should be available 🚀

@bompi88
Copy link

bompi88 commented Sep 19, 2024

Thanks @Balastrong and @crutchcorn ! So far this version works really well🎉

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.

4 participants