UNPKG

@honorestjs/contract

Version:

Contract-first API definitions for HonorestJS - Framework-agnostic contract definitions with full type safety

138 lines 3.73 kB
import type { ZodSchema } from 'zod'; import type { Endpoint } from '../builder/endpoint'; /** * Validation result type * Either successful with data or failed with error */ export type ValidationResult<T> = { success: true; data: T; } | { success: false; error: unknown; }; /** * Validation error details */ export interface ValidationError { field: string; message: string; code?: string; } /** * Validates data against a Zod schema * * @param schema - The Zod schema to validate against * @param data - The data to validate * @returns Validation result with success flag and data or error * * @example * ```typescript * const result = await validate(UserSchema, userData) * if (result.success) { * console.log(result.data) // Typed as User * } else { * console.error(result.error) * } * ``` */ export declare function validate<T>(schema: ZodSchema<T>, data: unknown): Promise<ValidationResult<T>>; /** * Synchronous validation against a Zod schema * * @param schema - The Zod schema to validate against * @param data - The data to validate * @returns Validation result with success flag and data or error */ export declare function validateSync<T>(schema: ZodSchema<T>, data: unknown): ValidationResult<T>; /** * Input validation results for an endpoint */ export interface EndpointInputValidation { params?: ValidationResult<unknown>; query?: ValidationResult<unknown>; body?: ValidationResult<unknown>; headers?: ValidationResult<unknown>; /** * True if all validations passed */ isValid: boolean; /** * Aggregated errors from all failed validations */ errors: ValidationError[]; } /** * Validates all inputs for an endpoint * * @param endpoint - The endpoint definition * @param input - The input data to validate * @returns Validation results for all input types * * @example * ```typescript * const validation = await validateEndpointInput( * UsersContract.endpoints.getUser, * { * params: { id: '123' }, * query: { include: 'posts' } * } * ) * * if (!validation.isValid) { * console.error('Validation failed:', validation.errors) * } * ``` */ export declare function validateEndpointInput(endpoint: Endpoint, input: { params?: unknown; query?: unknown; body?: unknown; headers?: unknown; }): Promise<EndpointInputValidation>; /** * Validates endpoint output * * @param endpoint - The endpoint definition * @param output - The output data to validate * @returns Validation result * * @example * ```typescript * const result = await validateEndpointOutput( * UsersContract.endpoints.getUser, * responseData * ) * * if (!result.success) { * console.error('Output validation failed') * } * ``` */ export declare function validateEndpointOutput(endpoint: Endpoint, output: unknown): Promise<ValidationResult<unknown>>; /** * Validates an error response against the endpoint's error schemas * * @param endpoint - The endpoint definition * @param statusCode - The HTTP status code * @param errorData - The error data to validate * @returns Validation result * * @example * ```typescript * const result = await validateEndpointError( * UsersContract.endpoints.getUser, * 404, * { message: 'User not found' } * ) * ``` */ export declare function validateEndpointError(endpoint: Endpoint, statusCode: number, errorData: unknown): Promise<ValidationResult<unknown>>; /** * Creates a validation error message from a Zod error * * @param error - The error to format * @returns Formatted error message */ export declare function formatValidationError(error: unknown): string; //# sourceMappingURL=validate.d.ts.map