import type { StandardJSONSchemaV1, StandardSchemaV1 } from '@standard-schema/spec'; import type { MessagesProviderContact, Refs, RootNode } from '@vinejs/compiler/types'; import { ITYPE, OTYPE } from '../symbols.js'; import { ValidationError } from '../errors/validation_error.js'; import type { Infer, SchemaTypes, MetaDataValidator, ValidationOptions, ErrorReporterContract } from '../types.js'; import { type JSONSchema7 } from 'json-schema'; /** * Vine Validator exposes the API to validate data using a pre-compiled * schema. This class provides high-performance validation by compiling * schemas once and reusing them for multiple validations. * * @template Schema - The schema type being validated * @template MetaData - The metadata type passed to validation * * @example * const validator = vine.compile(schema) * const result = await validator.validate(data) */ export declare class VineValidator> implements StandardSchemaV1 { #private; schema: Schema; protected options: { convertEmptyStringsToNull: boolean; metaDataValidator?: MetaDataValidator; messagesProvider: MessagesProviderContact; errorReporter: () => ErrorReporterContract; }; /** * Reference to static input type for TypeScript inference */ [ITYPE]: Schema[typeof ITYPE]; /** * Reference to static output type for TypeScript inference */ [OTYPE]: Schema[typeof OTYPE]; /** * Messages provider instance used for internationalization * and custom error message formatting */ 'messagesProvider': MessagesProviderContact; /** * Error reporter factory function used for formatting * and collecting validation errors */ 'errorReporter': () => ErrorReporterContract; /** * Validates data against the compiled schema. Returns the validated and typed data * or throws a ValidationError if validation fails. * * @param data - The data to validate * @param options - Optional validation options including metadata, custom error reporter, and messages provider * * @example * await validator.validate({ name: 'John', age: 30 }) * * @example * // With metadata * await validator.validate(data, { * meta: { userId: '123' } * }) * * @example * // With custom error reporter and messages provider * await validator.validate(data, { * meta: { userId: auth.user.id }, * errorReporter: () => new CustomErrorReporter(), * messagesProvider: customMessagesProvider * }) */ 'validate': (data: any, ...[options]: [undefined] extends MetaData ? [options?: ValidationOptions | undefined] : [options: ValidationOptions]) => Promise>; /** * Creates a new VineValidator instance with a compiled schema. * * @param schema - The schema to compile for validation * @param options - Configuration options for the validator * @param options.convertEmptyStringsToNull - Whether to convert empty strings to null * @param options.metaDataValidator - Optional metadata validator function * @param options.messagesProvider - Messages provider for error formatting * @param options.errorReporter - Error reporter factory function */ constructor(schema: Schema, options: { convertEmptyStringsToNull: boolean; metaDataValidator?: MetaDataValidator; messagesProvider: MessagesProviderContact; errorReporter: () => ErrorReporterContract; }); /** * Performs validation without throwing a ValidationError exception. * Instead, returns a tuple where the first element is the error (if any) * and the second is the validated data (if successful). * * @param data - The data to validate * @param options - Optional validation options including metadata, custom error reporter, and messages provider * * @example * const [error, result] = await validator.tryValidate(data) * if (error) { * console.log(error.messages) * } else { * console.log(result) * } * * @example * // With metadata * const [error, result] = await validator.tryValidate(data, { * meta: { userId: '123' } * }) * * @example * // With custom error reporter * const [error, result] = await validator.tryValidate(data, { * meta: { userId: auth.user.id }, * errorReporter: () => new CustomErrorReporter(), * messagesProvider: customMessagesProvider * }) */ 'tryValidate'(data: any, ...[options]: [undefined] extends MetaData ? [options?: ValidationOptions | undefined] : [options: ValidationOptions]): Promise<[ValidationError, null] | [null, Infer]>; /** * Returns the compiled schema and refs as a JSON-serializable object. * Useful for caching compiled schemas or debugging validation logic. * * @example * const compiled = validator.toJSON() * console.log(compiled.schema) * console.log(compiled.refs) */ 'toJSON'(): { schema: RootNode; refs: Refs; }; /** * Converts the validator's schema to JSON Schema Draft 7 format. * The result is cached for subsequent calls. * * @example * const jsonSchema = validator.toJSONSchema() * console.log(JSON.stringify(jsonSchema, null, 2)) */ 'toJSONSchema'(): JSONSchema7; /** * Standard Schema V1 compliance implementation. * Provides interoperability with other validation libraries through the * Standard Schema specification. * * @see https://github.com/standard-schema/standard-schema */ readonly '~standard': StandardSchemaV1.Props & StandardJSONSchemaV1.Props; }