Skip to content

feat(form-core): add parseValueWithSchema() stateless method #1421

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

Conversation

LeCarbonator
Copy link
Contributor

@LeCarbonator LeCarbonator commented Apr 16, 2025

As of tanstack form v1.4, there are two ways to use standard schemas as validators:

1. Pass directly

validators: {
  onChange: mySchema
}

2. Create a callback and import standardSchemaValidators

validators: {
  onChange: ({ value }) => {
    return standardSchemaValidators.validate(
      { value, validationSource: 'form' },
      mySchema
    );
  }
}

While more verbose, the second function allows you to combine standard schemas easily with other onChange validation a user may want on the same form / field.
There are some things to note about this:

  • value is redundant, as formApi | fieldApi would have access to it
  • validationSource is redundant, as formApi | fieldApi can infer it
  • mySchema can be anything, so this must be passed
  • standardSchemaValidators must be imported as a separate 'helper' file.

Given some users may want to add more logic than just a schema to their validation (and based on the discussion in this discord post, I propose the following:

FormApi#getValidationIssuesFromSchema(schema: StandardSchemaV1)

FieldApi#getValidationIssuesFromSchema(schema: StandardSchemaV1)

Name is subject to change. Suggest alternatives!

Usage

validators: {
  onChange: ({ value, formApi }) => {
     const issues = formApi.getValidationIssuesFromSchema(mySchema);
     /*
     readonly StandardSchemaV1Issue[] | {
    form: {
        [k: string]: StandardSchemaV1Issue[];
    };
    fields: {
        [k: string]: StandardSchemaV1Issue[];
    };
} | undefined
     */
     if (issues) return issues;
     
     // other custom validation goes here
  }
}

This would allow users to customize their validation more while preserving the parsing that standardSchemaValidators does.

Important

This method should be stateless. Users should have control over whether to return the issues or not.

Copy link

nx-cloud bot commented Apr 16, 2025

View your CI Pipeline Execution ↗ for commit a05f497.

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

☁️ Nx Cloud last updated this comment at 2025-04-16 14:12:55 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@1421

@tanstack/form-core

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

@tanstack/react-form

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

@tanstack/lit-form

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

@tanstack/vue-form

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

@tanstack/solid-form

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

commit: a05f497

Copy link

codecov bot commented Apr 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.52%. Comparing base (24edab3) to head (a05f497).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1421      +/-   ##
==========================================
+ Coverage   88.37%   88.52%   +0.14%     
==========================================
  Files          28       28              
  Lines        1308     1316       +8     
  Branches      343      343              
==========================================
+ Hits         1156     1165       +9     
+ Misses        135      134       -1     
  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.

@LeCarbonator
Copy link
Contributor Author

LeCarbonator commented Apr 16, 2025

Some name ideas:

  • findSchemaIssues(schema: ValidationSchemaV1)
  • checkAgainstSchema(schema: ValidationSchemaV1)
  • evaluateValuesWithSchema(schema: ValidationSchemaV1)

@LeCarbonator
Copy link
Contributor Author

parseValuesWithSchema is probably the closest match for now. It's not a word standard schema uses itself, but it describes the stateless method quite well in my opinion.

@LeCarbonator LeCarbonator force-pushed the standard-schema-validators-helper-function branch from fe34551 to 7036146 Compare April 16, 2025 11:48
@LeCarbonator LeCarbonator changed the title feat(FormApi): add helper method to parse values into Standard Schema issues feat(FormApi): add parseValueWithSchema() stateless method Apr 16, 2025
@LeCarbonator LeCarbonator marked this pull request as ready for review April 16, 2025 11:53
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 the PR, I like how this is turning out!

@LeCarbonator LeCarbonator force-pushed the standard-schema-validators-helper-function branch from 6ebd00c to 41bda55 Compare April 16, 2025 13:34
@LeCarbonator LeCarbonator requested a review from Balastrong April 16, 2025 13:41
@LeCarbonator
Copy link
Contributor Author

That should cover the comments. Also added some type safety to not pass schemas that will set errors on non-existent fields.

@Balastrong Balastrong changed the title feat(FormApi): add parseValueWithSchema() stateless method feat(form-core): add parseValueWithSchema() stateless method Apr 16, 2025
LeCarbonator and others added 2 commits April 16, 2025 15:56
Since we don't pass a field to the method, the term
shouldn't be included in the method name.
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.

That's a perfect PR and a really nice addition, thank you so much!

@Balastrong Balastrong merged commit ba66490 into TanStack:main Apr 16, 2025
6 checks passed
@LeCarbonator LeCarbonator deleted the standard-schema-validators-helper-function branch April 16, 2025 14:53
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