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

fix(Form): standard schema validation no longer wrapped in value object #3104

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from

Conversation

Gerbuuun
Copy link

πŸ”— Linked issue

Not an issue, but it was noticed in this PR: #2880

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

The standard schema validation implementation wrapped the state in an object with value as key:

const result = await schema['~standard'].validate({
  value: state,
})

The wrapped object is now also part of the object to be validated. This means all error messages also have value added to their path and breaks it. I highly suspect this is an artifact left behind from copying the valibot validator from UI2?

Anyways, in the standard-schema repo the example does not show to wrap the input data.

New implementation:

const result = await schema['~standard'].validate(state)

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Copy link

pkg-pr-new bot commented Jan 14, 2025

npm i https://pkg.pr.new/@nuxt/ui@3104

commit: 174fcc3

@Gerbuuun
Copy link
Author

Ok, the standard-schema example does show getting the data by result.value. Also typescript is saying that. But this PR fixes the problem anyways? I'm a bit confused. Looking into it furter

@OhB00
Copy link

OhB00 commented Jan 15, 2025

Using safeParser (https://valibot.dev/api/safeParser/) is a workaround, but I feel like you shouldn't need to do this.

@sandros94
Copy link
Collaborator

Using safeParser (https://valibot.dev/api/safeParser/) is a workaround, but I feel like you shouldn't need to do this.

Indeed, since at that point you are effectively using Valibot instead of standard-schema

@sandros94 sandros94 added v3 #1289 bug Something isn't working labels Jan 15, 2025
@Gerbuuun
Copy link
Author

Gerbuuun commented Jan 15, 2025

So both actually "work" but let's just follow the spec and only return the value from the validation result (otherwise there is the extra value field). @fabian-hiller any reason why Valibot returns the validated data in result and in result.value?

edit: I moved up the isStandardSchema check function to test this bug but forgot to put it back. I'd argue that we should put at the top anyways as more libraries are going to adopt standard schema.

@fabian-hiller
Copy link
Contributor

fabian-hiller commented Jan 15, 2025

Quick clarification. We probably implemented Standard Schema in Nuxt UI before we changed a few things. Initially, Standard Schema required an object to be passed to its validate function with a key called value. We have simplified this API so that the value can be passed directly. If the latest Standard Schema spec is supported, any Valibot (and soon Zod and co.) specific code can probably be removed. I can offer to try to review this PR in the next few days.

The Standard Schema spec README is up-to-date and can be followed when implementing the spec.

@fabian-hiller
Copy link
Contributor

I have reviewed the changes and they look good to me. Thanks for fixing it and supporting the spec! Happy to answer any questions about the spec and Valibot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v3 #1289
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants