ValidatorCache
Experimental
Interface for the validator cache.
The cache stores compiled validators keyed by their source schema. Using WeakMap ensures that schemas can be garbage collected when no longer referenced elsewhere.
Since
2.0.0
Methods
get()
Experimental
Retrieves a compiled validator from the cache.
Type Parameters
T: T
schema:
Schema<T>— The schema to look up
Returns:CompiledValidator<T> | undefined— The cached validator, or undefined if not found
set()
Experimental
Stores a compiled validator in the cache.
Type Parameters
T: T
schema:
Schema<T>— The schema to use as key
validator:CompiledValidator<T>— The compiled validator to cache
Returns:void
has()
Experimental
Checks if a schema has a cached validator.
schema:
Schema<unknown>— The schema to check
Returns:boolean— true if the schema has a cached validator
clear()
Experimental
Clears all cached validators.
Note: This creates a new WeakMap, allowing the old entries to be garbage collected.
Returns
void
createValidatorCache()
createValidatorCache():
ValidatorCache
Experimental
Creates a new validator cache instance.
Returns: ValidatorCache
A new ValidatorCache
Since
2.0.0
Example
const cache = createValidatorCache();
const schema = string();
const validator = compile(schema);
cache.set(schema, validator);
// Later...
if (cache.has(schema)) {
const cached = cache.get(schema);
}
globalValidatorCache
constglobalValidatorCache:ValidatorCache
Experimental
Global validator cache instance.
This is the default cache used by the JIT compiler.
It can be cleared using globalValidatorCache.clear().
Since
2.0.0