-
-
Notifications
You must be signed in to change notification settings - Fork 232
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: add formData schema #539
Conversation
Thanks for the PR and your contribution to the project. At the moment I am not sure if we should add On the other hand, I see value in adding functions like |
Yes, you are right that a However, we still need a way to validate data from
I thought that the only way to meet all these requirements at once is to write a wrapper function between parse(chooseFunctionNameHere(MySchema), formData, { abortEarly: true }) Currently, I have opted to use a custom schema in this PR because it allows implementing all the requirements and gives access to the internal functions of It's probably possible to implement this as a separate package, but I would prefer that Maybe it would be worth allowing returning a dataset from custom schemas? custom((input) => { typed: false, value: input, issues: [...] }) in addition to custom((input) => boolean) This might pave the way for writing complex, including mutating data, third-party schemas like import { custom } from 'valibot'
import type { BaseIssue, BaseSchema, Dataset, InferOutput } from 'valibot'
export function decodeFormData<
TSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>>
>(schema: TSchema) {
return custom((input) => {
// parse input and prepare dataset based on schema here
return dataset as Dataset<InferOutput<TSchema>, InferIssue<TSchema>>
})
} What do you think? |
I think it might be the right approach to implement Should we add these advanced schemas to the core package? Should they be added to |
Closing in favor of #672 |
Fixes #81, implements the suggestion from fabian-hiller/decode-formdata#10.
This PR introduces a new formData schema that enables the extraction and validation of required FormData directly from the server. Here’s how it works:
For example, if you send the following FormData to the server:
You can now define a validation schema like this:
You can use this schema to extract and validate the required info:
This will give you validated and typed form data like this:
This PR streamlines the way we handle and validate
FormData
from the client, ensuring the data is correctly typed and validated upon reception.