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

Add ParseOptions argument to standardSchemaV1 #4567

Merged
merged 5 commits into from
Mar 9, 2025

Conversation

rehos
Copy link
Contributor

@rehos rehos commented Mar 9, 2025

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

Add optional ParseOptions argument to standardSchemaV1. This will make form validation in form libraries like @tanstack/form more usable (i.e. display all errors to the user instead of just the first)

@rehos rehos requested a review from gcanti as a code owner March 9, 2025 13:23
Copy link

changeset-bot bot commented Mar 9, 2025

🦋 Changeset detected

Latest commit: 6295bb7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 36 packages
Name Type
effect Patch
@effect/cli Patch
@effect/cluster-browser Patch
@effect/cluster-node Patch
@effect/cluster-workflow Patch
@effect/cluster Patch
@effect/experimental Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/platform Patch
@effect/printer-ansi Patch
@effect/printer Patch
@effect/rpc-http Patch
@effect/rpc Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-drizzle Patch
@effect/sql-kysely Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/sql Patch
@effect/typeclass Patch
@effect/vitest Patch
@effect/ai Patch
@effect/ai-anthropic Patch
@effect/ai-openai Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@gcanti gcanti left a comment

Choose a reason for hiding this comment

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

I wonder if we should make { errors: "all" } the default. AFAIK, the spec doesn't specify how many issues should be raised by default.

@gcanti gcanti added the schema label Mar 9, 2025
@rehos
Copy link
Contributor Author

rehos commented Mar 9, 2025

I would prefer the default with { errors: "all" }. Should we do something like:

export const standardSchemaV1 = <A, I>(
  schema: Schema<A, I, never>,
  overrideOptions?: AST.ParseOptions // Changed
): StandardSchemaV1<I, A> => {
  const decodeUnknown = ParseResult.decodeUnknown(schema, { errors: "all" }) // Changed
  return {
    "~standard": {
      version: 1,
      vendor: "effect",
      validate(value) {
        const scheduler = new scheduler_.SyncScheduler()
        const fiber = Effect.runFork(
          Effect.matchEffect(decodeUnknown(value, overrideOptions), { // Changed
            onFailure: makeStandardFailureFromParseIssue,
            onSuccess: (value) => Effect.succeed({ value })
          }),
          { scheduler }
        )
        scheduler.flush()
        const exit = fiber.unsafePoll()
        if (exit) {
          return makeStandardResult(exit)
        }
        return new Promise((resolve) => {
          fiber.addObserver((exit) => {
            resolve(makeStandardResult(exit))
          })
        })
      }
    }
  }
}

@gcanti
Copy link
Contributor

gcanti commented Mar 9, 2025

Should we do something like:

Yes 👍

@rehos
Copy link
Contributor Author

rehos commented Mar 9, 2025

@gcanti I have changed the default to all errors with the possibility override the parse options. Also added an extra test for the override use case with{ errors: "first" }. And the incorrect import has been fixed.

@rehos rehos requested a review from gcanti March 9, 2025 15:52
Co-authored-by: Giulio Canti <giulio.canti@gmail.com>
@rehos rehos requested a review from gcanti March 9, 2025 15:57
@gcanti gcanti merged commit c65d336 into Effect-TS:main Mar 9, 2025
11 checks passed
@gcanti
Copy link
Contributor

gcanti commented Mar 9, 2025

Thanks @rehos

@github-actions github-actions bot mentioned this pull request Mar 9, 2025
@rehos
Copy link
Contributor Author

rehos commented Mar 9, 2025

Thank you @gcanti for your quick response and feedback.

@rehos rehos deleted the feature/standard-schema-parse-options branch March 9, 2025 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants