Skip to content

Feature: memoize result of asynchronous schema #923

@GregOnNet

Description

@GregOnNet

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

Activity

fabian-hiller

fabian-hiller commented on Nov 13, 2024

@fabian-hiller
Owner

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

GregOnNet

GregOnNet commented on Nov 13, 2024

@GregOnNet
Author

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

Example:

  1. Someone types gregor@domain.de which triggers an API call from the async validator
  2. Then someone corrects this to gregor@domain.uk which is calling the API again
  3. Finally, someone switches back to gregor@domain.de 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

GregOnNet commented on Nov 14, 2024

@GregOnNet
Author

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

fabian-hiller

fabian-hiller commented on Nov 15, 2024

@fabian-hiller
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.

added this to the v2 milestone on Jan 22, 2025
modified the milestones: v2, v1.2 on May 7, 2025
dosubot

dosubot commented on Aug 6, 2025

@dosubot

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!

added
staleIssue has not had recent activity or appears to be solved. Stale issues will be automatically closed
on Aug 6, 2025
removed
staleIssue has not had recent activity or appears to be solved. Stale issues will be automatically closed
on Aug 6, 2025
GregOnNet

GregOnNet commented on Aug 8, 2025

@GregOnNet
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestpriorityThis has priority

Projects

No projects

Relationships

None yet

    Participants

    @GregOnNet@fabian-hiller

    Issue actions

      Feature: `memoize` result of asynchronous schema · Issue #923 · fabian-hiller/valibot