funval
Version:
A minimalist library for data validation using functions interfaces.
18 lines (17 loc) • 1.54 kB
TypeScript
import { AnyType, FunctionType } from './utils';
import { ErrorLike } from './Error';
import { Output, Input } from './Type';
export declare type SyncFunctionValidator<T = AnyType, I extends Array<AnyType> = [Input<T>]> = FunctionType<T, I>;
export declare type AsyncFunctionValidator<T = AnyType, I extends Array<AnyType> = [Input<T>]> = FunctionType<PromiseLike<T>, I>;
export declare type FunctionValidator<T = AnyType, I extends Array<AnyType> = [Input<T>]> = FunctionType<PromiseLike<T> | T, I>;
export declare type SchemaType<T = AnyType> = [T] extends [FunctionType] ? FunctionValidator<T> : [T] extends [object] ? {
[K in keyof T]: SchemaType<T[K]>;
} : FunctionValidator<T> | T;
export declare type SyncSchemaType<T> = [T] extends [FunctionType] ? SyncFunctionValidator<T> : [T] extends [object] ? {
[K in keyof T]: SyncSchemaType<T[K]>;
} : [T] extends [string] ? SyncFunctionValidator<T> | RegExp | T : SyncFunctionValidator<T> | T;
export declare type ValidatorOutput<T> = T extends SyncSchemaType<Output<T>> ? Output<T> : PromiseLike<Output<T>>;
export declare type SchemaValidator<T, I extends Array<AnyType> = [Input<T>]> = FunctionType<ValidatorOutput<T>, I>;
export declare type SchemaAsyncValidator<T, I extends Array<AnyType> = [Input<T>]> = FunctionType<PromiseLike<Output<T>>, I>;
export default function Schema<T>(schema: T, error?: ErrorLike): SchemaValidator<T>;
export declare function Async<V, T extends FunctionValidator<V>, I extends Parameters<T>[0]>(validator: T): FunctionType<PromiseLike<V>, [I]>;