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

Feature: memoize result of asynchronous schema #923

Open
GregOnNet opened this issue Nov 13, 2024 · 4 comments
Open

Feature: memoize result of asynchronous schema #923

GregOnNet opened this issue Nov 13, 2024 · 4 comments
Assignees
Labels
enhancement New feature or request priority This has priority

Comments

@GregOnNet
Copy link

GregOnNet commented Nov 13, 2024

Preface - valibot is awesome

Thank you all for developing this library.
It is the most type-safe library, I have ever used.
It comes with great DX & the docs become better and better.

Use Case

Forms:

  • Sometimes we need to validate certain fields asynchronously.
  • It would be nice if the schema could cache certain inputs and the results of the asynchronous validations to not trigger asynchronous operations if not needed.

New action memoize

Wouldn't it be nice if we could memoize the executions inside a schema, where it is needed?
We could provide a synchronous and an asynchronous version...

memoize
memoizeAsync

Example

v.pipeAsync(
  v.string(),
  v.memoizeAsync(
    v.customAsync(input => fetch(`/api/email/${input}`).then(() => /* ... */)), { 
    buffer: 10, // default 1 - tells how many inputs with their respective result are cached
    itemExpiryInMs: 30_000  // default 5_000
  })
)
@fabian-hiller
Copy link
Owner

Great idea! What is buffer doing? We could work on memoize while also working on a debounce function.

@fabian-hiller fabian-hiller self-assigned this Nov 13, 2024
@fabian-hiller fabian-hiller added enhancement New feature or request priority This has priority labels Nov 13, 2024
@GregOnNet
Copy link
Author

Oh, yeah buffer… I thought it would be handy to be able to tell how many items are cached...

Example:

  1. Someone types [email protected] which triggers an API call from the async validator
  2. Then someone corrects this to [email protected] which is calling the API again
  3. Finally, someone switches back to [email protected] which is not calling the API because the outcome of the validation is still cached and not outdated.

I hope clarifies the use-case a bit better.

I also like the idea of having debounce for certain values. 💪🏻

@GregOnNet
Copy link
Author

After writing the example above, I started thinking if we are talking about cache & cacheAsync instead of Memoization…

@fabian-hiller
Copy link
Owner

I posted this issue on X to get more feedback and ideas from other users. I am currently focusing on Valibot v1 and Standard Schema and think I will work on this issue after our stable v1 is out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request priority This has priority
Projects
None yet
Development

No branches or pull requests

2 participants