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