Skip to content

[feat] Field validation from form schema - #925

Merged
crutchcorn merged 18 commits into
mainfrom
zod-validate-schema
Sep 18, 2024
Merged

[feat] Field validation from form schema#925
crutchcorn merged 18 commits into
mainfrom
zod-validate-schema

Conversation

@crutchcorn

@crutchcorn crutchcorn commented Aug 28, 2024

Copy link
Copy Markdown
Member

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
@nx-cloud

nx-cloud Bot commented Aug 28, 2024

Copy link
Copy Markdown

☁️ 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.

@pkg-pr-new

pkg-pr-new Bot commented Aug 28, 2024

Copy link
Copy Markdown

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
11 tasks

@Balastrong Balastrong left a comment

Copy link
Copy Markdown
Member

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!

Comment thread packages/form-core/src/types.ts Outdated
Comment thread packages/zod-form-adapter/src/validator.ts Outdated
Comment thread packages/form-core/src/FormApi.ts Outdated
@jljorgenson18

Copy link
Copy Markdown

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

bompi88 commented Sep 13, 2024

Copy link
Copy Markdown

What is the current status of this PR?

@crutchcorn

Copy link
Copy Markdown
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

bompi88 commented Sep 13, 2024

Copy link
Copy Markdown

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

@Balastrong Balastrong left a comment

Copy link
Copy Markdown
Member

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

bompi88 commented Sep 18, 2024

Copy link
Copy Markdown

🚀

@crutchcorn

Copy link
Copy Markdown
Member Author

This LGTM @Balastrong - great work! Merging now

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

bompi88 commented Sep 19, 2024

Copy link
Copy Markdown

@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
Copy Markdown
Member

@bompi88 you're right! :D

I fixed it now, 0.33 should be available 🚀

@bompi88

bompi88 commented Sep 19, 2024

Copy link
Copy Markdown

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