valibot icon indicating copy to clipboard operation
valibot copied to clipboard

Feature: `memoize` result of asynchronous schema

Open GregOnNet opened this issue 1 year ago • 6 comments

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
  })
)

GregOnNet avatar Nov 13 '24 14:11 GregOnNet

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

fabian-hiller avatar Nov 13 '24 17:11 fabian-hiller

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 avatar Nov 13 '24 18:11 GregOnNet

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

GregOnNet avatar Nov 14 '24 08:11 GregOnNet

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.

fabian-hiller avatar Nov 15 '24 17:11 fabian-hiller

Hi, @GregOnNet. I'm Dosu, and I'm helping the Valibot team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You proposed adding a memoize feature to Valibot for caching async schema validation results to reduce redundant calls.
  • The feature idea includes both sync and async versions with configurable cache size and expiry.
  • Fabian-hiller expressed support and mentioned working on a debounce function alongside memoize.
  • You clarified the use case with an example and noted the feature might be more about caching than memoization.
  • Fabian-hiller plans to address this after releasing Valibot v1 and gathering more community feedback.

Next Steps:

  • Please let me know if this feature is still relevant to the latest version of Valibot by commenting on this issue.
  • If I don’t hear back within 30 days, I will automatically close this issue.

Thanks for your understanding and contribution!

dosubot[bot] avatar Aug 06 '25 16:08 dosubot[bot]

Hi,

it might be relevant especially for form validation with async validations. But Fabian is also working on formish, that might solve this use-case.

So maybe double check with Fabian.

It is fine to close it unless there is a bigger need.

GregOnNet avatar Aug 08 '25 07:08 GregOnNet