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

Can't use Valibot with H3 Data validation #504

Open
DylanThomasFr opened this issue Apr 4, 2024 · 6 comments
Open

Can't use Valibot with H3 Data validation #504

DylanThomasFr opened this issue Apr 4, 2024 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@DylanThomasFr
Copy link

DylanThomasFr commented Apr 4, 2024

Reference from H3 : https://h3.unjs.io/examples/validate-data

With zod for example we validate a query like so :

const query = await getValidatedQuery(event, userSchema.parse)
const params = await getValidatedRouterParams(event, userSchema.parse)
const body = await readValidatedBody(event, userSchema.parse)

The parse method is used to parse the formatted data against the schema.

The Valibot parse method requires a schema and the data to validate so it can't be used with H3 validation.

H3 is used for Nuxt and is used by a large number of developers. Would be nice to have Valibot compatible with H3.

Hope this will be helpful

@fabian-hiller
Copy link
Owner

You could use a little helper function:

import * as v from 'valibot';

function valibot<TSchema extends v.BaseSchema>(schema: TSchema): v.Output<TSchema> {
  return (input: unknown) => v.parse(schema, input);
}

const query = await getValidatedQuery(event, valibot(YourSchema));

@fabian-hiller fabian-hiller self-assigned this Apr 4, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label Apr 4, 2024
@fabian-hiller
Copy link
Owner

Not sure if it is up to date, but there is also a dedicated community library: https://github.com/Intevel/h3-valibot

@DylanThomasFr
Copy link
Author

DylanThomasFr commented Apr 5, 2024

@fabian-hiller Thank you for your reply !

Yes this is what I have done for now but I figured it would be nice to have it built in Valibot.

Then developers don't need to make it themselves and it can be added nicely to the H3 doc later on.

@fabian-hiller
Copy link
Owner

I understand, but as a library we can't provide adapters for every possible other library out there. It would make more sense to me if H3 would provide a Valibot adapter for their users.

@IlyaSemenov
Copy link
Contributor

The code excerpt above is not actually a provider for a specific library. It's a generic schema curry helper, and I believe it indeed deserves a method in Valibot.

import * as v from "valibot"

const schema = v.object({ ... })

const validate = v.parser(schema) // new method: convert a schema to an unary parser function

const input = validate(unsafeInput) // this logic is not related to h3, it's generally useful

The naming is a subject for discussion of course, but the actual idea quite makes sense.

@fabian-hiller
Copy link
Owner

Thank you for your feedback! Adding it might actually fix some problems with other libraries as well. If we find a good name, I might add such a utility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants