Skip to main content

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()โ€‹

Function

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โ€‹

Variable

const globalValidatorCache: 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