import type { StandardJSONSchemaV1, StandardSchemaV1 } from '@standard-schema/spec'; import type { FromSchema } from 'json-schema-to-ts'; import type { Schema } from './types.js'; /** * Options for creating a validator. */ export type ValidatorOptions = { draft?: '07' | '2020-12'; strict?: boolean | 'log'; }; /** * Logger receiving AJV warnings, such as unknown formats under non-strict mode. */ export type ValidatorLogger = { log: (...args: Array) => unknown; warn: (...args: Array) => unknown; error: (...args: Array) => unknown; }; /** * Options for creating a validator factory. `logger: false` silences AJV warnings. */ export type ValidatorFactoryOptions = ValidatorOptions & { logger?: ValidatorLogger | false; }; /** * Validator factory owning an isolated AJV instance. */ export type ValidatorFactory = { /** Create a validator on this factory's instance, memoized per schema object. */ createValidator: >(schema: S) => Validator; /** Number of schemas compiled, excluding memoized lookups. */ readonly compiled: number; /** * Drop the AJV instance and every validator memoized on it. Later `createValidator` calls * throw. Validators already returned keep working and keep the instance alive until they * are dropped too. Idempotent. */ dispose: () => void; }; /** * Validator function, returning a Result of the validation. */ export type Validator = (value: unknown) => StandardSchemaV1.Result; /** * Validator function factory using a JSON schema. */ export declare function createValidator>(schema: S, options?: ValidatorOptions): Validator; /** * Create a validator factory with its own AJV instance, shared with nothing else. Use it for * schemas that arrive at runtime: `dispose()` drops the AJV instance and every validator it * memoised, but validators already returned keep working and keep the instance alive. */ export declare function createValidatorFactory(options?: ValidatorFactoryOptions): ValidatorFactory; /** * Asserts the type of the given `value` using the `validator`. */ export declare function assertType(validator: Validator, value: unknown): asserts value is T; /** * Asserts the type of the given `value` using the `validator` and returns it. */ export declare function asType(validator: Validator, value: unknown): T; /** * Checks the type of the given `value` using the `validator`. */ export declare function isType(validator: Validator, value: unknown): value is T; /** * Turn a `Validator` function into a standard schema validator. */ export declare function toStandardValidator(validator: Validator): StandardSchemaV1; export declare function toStandardValidator(validator: Validator, schema: Schema, options?: ValidatorOptions): StandardSchemaV1 & StandardJSONSchemaV1; /** * Create a standard schema validator. */ export declare function createStandardValidator>(schema: S, options?: ValidatorOptions): StandardSchemaV1 & StandardJSONSchemaV1;