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] Allow returning anything in a validator, not just a string #1104

Merged
merged 96 commits into from
Feb 21, 2025

Conversation

crutchcorn
Copy link
Member

@crutchcorn crutchcorn commented Jan 9, 2025

This PR allows you to return non-string values from validators:

<form.Field name="password" validators={{onChange: () => ({hasUppercase: false, hasLowercase: true}) }} />

In addition, it also enforces type safety on the returned value on both errorMap and the errors array:

const form = new FormApi({
  defaultValues: {
    name: 'test',
  },
} as const)

const field = new FieldApi({
  form,
  name: 'name',
  validators: {
    onChange: () => {
      return 123 as const
    },
  },
})

assertType<123 | undefined>(field.state.meta.errorMap.onChange)

assertType<Array<123 | undefined>>(field.state.meta.errors)

Breaking Changes

  • Removes all validatorAdapters (packages, code, and props alike)
    • Instead, you can use our Standard Schema support for Valibot, Zod, and ArkType
    • As a result, however, Yup is no longer supported until this community PR is merged
  • When using Standard Schema validators:
    • form.errors is now Record<string, StandardSchemaIssue[]>
    • field.errors is now StandardSchemaIssue[] (flattened)
  • Vue 3.4 is the new minimum version
  • Vue JSX usage no longer works (fixes SFC usage)
  • TanStack Start adapter API changed (fixes Update TanStack Start adapter #1023)

TODO

  • Fix Start adapter
  • Think about how to handle schema validators' return types
  • Fix standard schema types
  • Fix adapters
    • vue-form
    • solid-form
    • react-form
    • lit-form
    • angular-form
  • Docs highlights of non-string returns
  • Docs mention of error array handling
  • Docs update for TanStack Start adapter
  • Docs updates for validations and errors

Future work

  • Add standard schema validation for SSR adapters
  • Add examples for standard schema for other libraries

Copy link

nx-cloud bot commented Jan 9, 2025

View your CI Pipeline Execution ↗ for commit b6de203.

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

☁️ Nx Cloud last updated this comment at 2025-02-21 03:23:36 UTC

@crutchcorn crutchcorn changed the title [FIX] Allow returning anything in a validator, not just a string [WIP] [FIX] Allow returning anything in a validator, not just a string Jan 9, 2025
@crutchcorn crutchcorn marked this pull request as draft January 9, 2025 08:56
Copy link

pkg-pr-new bot commented Jan 9, 2025

Open in Stackblitz

More templates

@tanstack/angular-form

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

@tanstack/form-core

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

@tanstack/react-form

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

@tanstack/lit-form

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

@tanstack/solid-form

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

@tanstack/vue-form

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

commit: b6de203

@crutchcorn crutchcorn changed the title [WIP] [FIX] Allow returning anything in a validator, not just a string [WIP] [FEAT] Allow returning anything in a validator, not just a string Jan 10, 2025
crutchcorn and others added 6 commits February 20, 2025 17:44
# Conflicts:
#	docs/reference/functions/mergeform.md
#	examples/angular/valibot/package.json
#	examples/angular/yup/package.json
#	examples/angular/zod/package.json
#	examples/react/query-integration/package.json
#	examples/react/standard-schema/package.json
#	examples/react/tanstack-start/package.json
#	examples/react/ui-libraries/package.json
#	examples/react/valibot/package.json
#	examples/react/yup/package.json
#	examples/react/zod/package.json
#	examples/solid/array/package.json
#	examples/solid/simple/package.json
#	examples/solid/valibot/package.json
#	examples/solid/yup/package.json
#	examples/solid/zod/package.json
#	examples/vue/array/package.json
#	examples/vue/simple/package.json
#	examples/vue/valibot/package.json
#	examples/vue/yup/package.json
#	examples/vue/zod/package.json
#	packages/valibot-form-adapter/package.json
#	packages/yup-form-adapter/package.json
#	packages/zod-form-adapter/package.json
#	pnpm-lock.yaml
@crutchcorn crutchcorn changed the title [WIP] [FEAT] Allow returning anything in a validator, not just a string [FEAT] Allow returning anything in a validator, not just a string Feb 21, 2025
@crutchcorn crutchcorn marked this pull request as ready for review February 21, 2025 02:50
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.

Let's do it! 🚀

@crutchcorn crutchcorn merged commit d8ed149 into main Feb 21, 2025
7 of 8 checks passed
@crutchcorn crutchcorn deleted the return-anything-not-just-string branch February 21, 2025 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update TanStack Start adapter
3 participants