import type { FromSchema } from 'json-schema-to-ts'; import type { Schema } from './types.js'; import { type Validator, type ValidatorFactoryOptions } from './validation.js'; /** * Options for creating a validator cache. */ export type ValidatorCacheOptions = { /** Options for each factory the cache creates. Default factory options if omitted. */ factory?: ValidatorFactoryOptions; /** Distinct compiles on one factory before it is disposed and replaced. Default 256. */ maxCompiles?: number; /** Validators (and cached compile errors) kept, least recently used evicted first. Default 64. */ maxEntries?: number; }; /** * Counters describing the state of a validator cache. */ export type ValidatorCacheStats = { /** Number of times the factory was replaced since creation or the last `clear()`. */ generation: number; /** Compiles on the current factory, failed compiles included. */ compiles: number; /** Entries currently in the LRU. */ entries: number; }; /** * Bounded cache of validators for schemas that arrive at runtime. */ export type ValidatorCache = { /** * Get the validator for a schema, compiling it on a miss. Schemas that are equal up to object * key order share one validator. A failed compile is cached and rethrown on later calls. */ get: >(schema: TSchema) => Validator; /** Current counters. */ stats: () => ValidatorCacheStats; /** Dispose the current factory and drop every entry. The cache stays usable. Idempotent. */ clear: () => void; /** Clear the cache and make later `get` calls throw. Idempotent. */ dispose: () => void; /** `true` once `dispose()` has been called. */ readonly disposed: boolean; }; /** * Create a validator cache: a least recently used map of validators over isolated factories that * are disposed and replaced after `maxCompiles` distinct compiles, so memory stays bounded for * schemas that arrive at runtime. * * Schemas must be self-contained and plain JSON. A validator checks a snapshot of the schema, so * `ValidationError.schema` is that snapshot and issues follow sorted key order. Look a validator * up at the moment of use and do not hold it longer than needed: a long-lived holder keeps its * disposed AJV instance alive. */ export declare function createValidatorCache(options?: ValidatorCacheOptions): ValidatorCache;