UNPKG

hi-validator

Version:

![Test Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen) ![Build Status](https://img.shields.io/github/actions/workflow/status/yourname/hi-validator/ci.yml) ![License](https://img.shields.io/badge/license-MIT-blue)

41 lines (35 loc) 1.65 kB
declare class ValidationError extends Error { readonly path: Array<string | number>; readonly value: unknown; constructor(message: string, options?: { path?: Array<string | number>; value?: unknown; }); } type ErrorMessage<T> = string | ((value: unknown, context: ValidationContext) => string); interface ValidationContext { path: Array<string | number>; errors: ValidationError[]; } interface EnhancedValidator<T> { (value: unknown, context?: ValidationContext): value is T; withMessage(message: ErrorMessage<T>): EnhancedValidator<T>; optional(): EnhancedValidator<T | undefined>; } declare function createValidator<T>(condition: (value: unknown, context?: ValidationContext) => value is T, defaultMessage?: ErrorMessage<T>): EnhancedValidator<T>; interface ObjectValidationSchema { [key: string]: ReturnType<typeof createValidator<any>>; } declare function validateObject<T extends object>(schema: ObjectValidationSchema): (value: unknown, context?: ValidationContext) => value is T; declare const checkString: EnhancedValidator<string>; declare const checkNumber: EnhancedValidator<number>; declare const checkEmail: EnhancedValidator<string>; declare class ValidationChain<T> { private value; private validators; constructor(value: unknown); check(validator: (value: unknown) => boolean, message?: string): this; validate(): T; } declare const chain: <T>(value: unknown) => ValidationChain<T>; export { type EnhancedValidator, ValidationChain, type ValidationContext, ValidationError, chain, checkEmail, checkNumber, checkString, createValidator, validateObject };