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

Export common type for BaseSchema and BaseSchemaAsync #198

Open
IlyaSemenov opened this issue Oct 5, 2023 · 3 comments
Open

Export common type for BaseSchema and BaseSchemaAsync #198

IlyaSemenov opened this issue Oct 5, 2023 · 3 comments
Assignees
Labels
question Further information is requested workaround Workaround fixes problem

Comments

@IlyaSemenov
Copy link
Contributor

As a developer creating integrations with valibot (not using valibot directly, but creating library solutions for other developers on top of valibot), I am missing an option to accept abstract valibot schema, either sync or async. Currently, I have to use v.BaseSchema<Input, Output> | v.BaseSchemaAsync<Input, Output> boilerplate.

Example (simplified) code:

import * as v from "valibot"

/**
 * Vue composable: validate input and submit if it's valid.
 * Simplified for brevity.
 */
export function useForm<Input, ValidInput = Input>(options: {
  fields: Input
  schema: v.BaseSchema<Input, ValidInput> | v.BaseSchemaAsync<Input, ValidInput>
  submit: (data: ValidInput) => void | PromiseLike<void>
}) {
  const { schema, fields } = options

  return async function submit() {
    const res = await v.safeParseAsync(schema, fields)
    if (res.success) {
      await options.submit(res.output)
    }
  }
}

I propose to export a new common type (such as BaseSchemaMaybeAsync), so that in the code above it'd possible to write:

schema: v.BaseSchemaMaybeAsync<Input, ValidInput>
@fabian-hiller fabian-hiller self-assigned this Oct 5, 2023
@fabian-hiller fabian-hiller added the question Further information is requested label Oct 5, 2023
@fabian-hiller
Copy link
Owner

Thanks for your feedback on this. I'm not sure yet if I want to add such a type to the library. I think the explicit assignment is better in most cases. As a workaround for now you can create this type yourself.

type BaseSchemaMaybeAsync<Input, ValidInput> =
  | v.BaseSchema<Input, ValidInput>
  | v.BaseSchemaAsync<Input, ValidInput>;

@fabian-hiller fabian-hiller added the workaround Workaround fixes problem label Oct 5, 2023
@IlyaSemenov
Copy link
Contributor Author

IlyaSemenov commented Oct 5, 2023

Thank you for the response. Of course I understand that I can create types and other extensions on my own, especially as trivial as this one.

If you don't think the use case is relevant (even though I provided an example) and/or that this is an anti-pattern, I suppose this issue should be just closed as not planned. I'm inclined to publish some kind of valibotx anyway, which will re-export everything from valibot along with the unobtrusive extensions that you don't want (e.g. #171). :)

@fabian-hiller
Copy link
Owner

My spontaneous feedback is that I prefer an explicit assignment. This is also the approach I currently use in the Valibot source code. However, I understand your approach and depending on the context, it may be better.

I'm open to new ideas, and even if I don't directly agree with a change or extension, I leave it open to change my mind if I get feedback from more people. So I will leave the labels as they are for now and maybe change them in a few months.

Feel free to create a package like valibotx that extends the functionality of Valibot. Maybe in the long run I will take some ideas and implement them directly into Valibot.

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

No branches or pull requests

2 participants