/** * @file Schema Validator * @description Production-grade schema validation library with Zod-style API, * full TypeScript inference, composable validators, and async support. * * Features: * - Type-safe schema definitions with TypeScript inference * - Chainable API for schema composition * - Custom error messages with path tracking * - Async validation support * - Schema coercion and transformation * - Tree-shakeable exports * * @example * ```typescript * import { v, infer as Infer } from '@/lib/data/validation'; * * const UserSchema = v.object({ * id: v.string().uuid(), * email: v.string().email(), * age: v.number().min(0).optional(), * }); * * type User = Infer; * * const result = UserSchema.safeParse(data); * ``` */ /** * Validation issue structure */ export interface ValidationIssue { /** Error code for programmatic handling */ code: string; /** Human-readable error message */ message: string; /** Path to the invalid field */ path: (string | number)[]; /** Expected value/type */ expected?: string; /** Received value/type */ received?: string; /** Additional context */ context?: Record; } /** * Validation result for safeParse */ export type ValidationResult = { success: true; data: T; issues: never[]; } | { success: false; data: undefined; issues: ValidationIssue[]; }; /** * Async validation result */ export type AsyncValidationResult = Promise>; /** * Schema parse options */ export interface ParseOptions { /** Path prefix for error messages */ path?: (string | number)[]; /** Enable coercion (string to number, etc.) */ coerce?: boolean; /** Collect all errors instead of failing fast */ abortEarly?: boolean; /** Custom context passed to validators */ context?: Record; } /** * Base schema type for inference */ export type SchemaOutput> = T['_output']; /** * Infer TypeScript type from schema */ export type Infer> = SchemaOutput; /** * Abstract base class for all schema types */ export declare abstract class BaseSchema { /** Type marker for inference */ readonly _output: TOutput; /** Whether the schema is optional */ protected _optional: boolean; /** Whether the schema is nullable */ protected _nullable: boolean; /** Default value when undefined */ protected _default?: TOutput; /** Transform function applied after validation */ protected _transform?: (value: TOutput) => TOutput; /** Custom error message */ protected _message?: string; /** Schema description for documentation */ protected _description?: string; /** * Parse and validate input, throwing on error */ parse(value: unknown, options?: ParseOptions): TOutput; /** * Parse and validate input, returning result object */ safeParse(value: unknown, options?: ParseOptions): ValidationResult; /** * Async parse and validate */ parseAsync(value: unknown, options?: ParseOptions): Promise; /** * Async safeParse */ safeParseAsync(value: unknown, options?: ParseOptions): AsyncValidationResult; /** * Mark schema as optional */ optional(): BaseSchema; /** * Mark schema as nullable */ nullable(): BaseSchema; /** * Set default value */ default(value: TOutput): BaseSchema; /** * Add transform function */ transform(fn: (value: TOutput) => TNew): BaseSchema; /** * Add description */ describe(description: string): this; /** * Core validation logic to be implemented by subclasses */ protected abstract _validate(value: unknown, options: ParseOptions): ValidationResult; /** * Create a validation issue */ protected _createIssue(code: string, message: string, path: (string | number)[], context?: Record): ValidationIssue; /** * Clone the schema for immutable modifications */ protected abstract _clone(): this; } /** * String validation schema */ export declare class StringSchema extends BaseSchema { private _minLength?; private _maxLength?; private _pattern?; private _format?; private _trim; private _lowercase; private _uppercase; min(length: number, message?: string): this; max(length: number, message?: string): this; length(length: number, message?: string): this; regex(pattern: RegExp, message?: string): this; email(message?: string): this; url(message?: string): this; uuid(message?: string): this; datetime(message?: string): this; trim(): this; toLowerCase(): this; toUpperCase(): this; nonempty(message?: string): this; protected _validate(value: unknown, options: ParseOptions): ValidationResult; protected _clone(): this; private _validateFormat; } /** * Number validation schema */ export declare class NumberSchema extends BaseSchema { private _min?; private _max?; private _integer; private _positive; private _negative; private _finite; min(value: number, message?: string): this; max(value: number, message?: string): this; int(message?: string): this; positive(message?: string): this; negative(message?: string): this; nonnegative(message?: string): this; finite(message?: string): this; protected _validate(value: unknown, options: ParseOptions): ValidationResult; protected _clone(): this; } /** * Boolean validation schema */ export declare class BooleanSchema extends BaseSchema { protected _validate(value: unknown, options: ParseOptions): ValidationResult; protected _clone(): this; } /** * Date validation schema */ export declare class DateSchema extends BaseSchema { private _min?; private _max?; min(date: Date, message?: string): this; max(date: Date, message?: string): this; protected _validate(value: unknown, options: ParseOptions): ValidationResult; protected _clone(): this; } /** * Array validation schema */ export declare class ArraySchema> extends BaseSchema[]> { private readonly _element; private _minLength?; private _maxLength?; constructor(element: TElement); min(length: number, message?: string): this; max(length: number, message?: string): this; length(length: number, message?: string): this; nonempty(message?: string): this; protected _validate(value: unknown, options: ParseOptions): ValidationResult[]>; protected _clone(): this; } /** * Type helper for object schema shape */ type ObjectShape = Record>; /** * Infer object type from shape */ type InferObjectShape = { [K in keyof T]: SchemaOutput; }; /** * Object validation schema */ export declare class ObjectSchema extends BaseSchema> { private readonly _shape; private _strict; private _catchall?; private _passthrough; constructor(shape: TShape); /** * Get the shape */ get shape(): TShape; /** * Reject unknown keys */ strict(): this; /** * Pass through unknown keys */ passthrough(): this; /** * Validate unknown keys with a schema */ catchall>(schema: T): this; /** * Pick specific keys */ pick(keys: K[]): ObjectSchema>; /** * Omit specific keys */ omit(keys: K[]): ObjectSchema>; /** * Extend with additional fields */ extend(extension: TExtension): ObjectSchema; /** * Merge with another object schema */ merge(other: ObjectSchema): ObjectSchema; /** * Make all fields partial */ partial(): ObjectSchema<{ [K in keyof TShape]: ReturnType; }>; protected _validate(value: unknown, options: ParseOptions): ValidationResult>; protected _clone(): this; } /** * Enum validation schema */ export declare class EnumSchema extends BaseSchema { private readonly _values; constructor(values: T); get values(): T; protected _validate(value: unknown, options: ParseOptions): ValidationResult; protected _clone(): this; } /** * Literal validation schema */ export declare class LiteralSchema extends BaseSchema { private readonly _value; constructor(value: T); get value(): T; protected _validate(value: unknown, options: ParseOptions): ValidationResult; protected _clone(): this; } /** * Union validation schema */ export declare class UnionSchema[]> extends BaseSchema> { private readonly _schemas; constructor(schemas: T); protected _validate(value: unknown, options: ParseOptions): ValidationResult>; protected _clone(): this; } /** * Record validation schema */ export declare class RecordSchema, TValue extends BaseSchema> extends BaseSchema>> { private readonly _keySchema; private readonly _valueSchema; constructor(keySchema: TKey, valueSchema: TValue); protected _validate(value: unknown, options: ParseOptions): ValidationResult>>; protected _clone(): this; } /** * Unknown validation schema (accepts anything) */ export declare class UnknownSchema extends BaseSchema { protected _validate(value: unknown, _options: ParseOptions): ValidationResult; protected _clone(): this; } /** * Schema validation error */ export declare class SchemaValidationError extends Error { readonly issues: ValidationIssue[]; constructor(issues: ValidationIssue[]); /** * Get flat error map */ flatten(): Record; /** * Format errors for display */ format(): Record; } /** * Schema factory functions */ export declare const v: { string: () => StringSchema; number: () => NumberSchema; boolean: () => BooleanSchema; date: () => DateSchema; array: >(element: T) => ArraySchema; object: (shape: T) => ObjectSchema; enum: (values: T) => EnumSchema; literal: (value: T) => LiteralSchema; union: []>(...schemas: T) => UnionSchema; record: , TValue extends BaseSchema>(keySchema: TKey, valueSchema: TValue) => RecordSchema; unknown: () => UnknownSchema; any: () => UnknownSchema; optional: >(schema: T) => BaseSchema | undefined>; nullable: >(schema: T) => BaseSchema | null>; }; export { type InferObjectShape as InferObject }; export type Schema = BaseSchema; export declare class ValidationError extends Error { issues: ValidationIssue[]; constructor(issues: ValidationIssue[]); }