UNPKG

joiful

Version:

TypeScript Declarative Validation. Decorate your class properties to validate them using Joi.

31 lines (30 loc) 1.71 kB
import * as Joi from 'joi'; import { TypedPropertyDecorator, MapAllowUnions, StringOrSymbolKey } from '../core'; export declare class NotImplemented extends Error { constructor(feature: string); } export declare type LabelProvider = <TClass, TKey extends StringOrSymbolKey<TClass>>(propertyKey: TKey, target: TClass) => string | undefined | null; export interface JoifulOptions { joi?: typeof Joi; labelProvider?: LabelProvider | undefined; } declare type MethodNames<T> = { [K in keyof T]: T extends (...args: any[]) => any ? K : never; }; interface DecoratorContext<TSchema extends Joi.Schema> { schema: TSchema; options?: JoifulOptions; } export declare type ModifierProviders<TSchema extends Joi.Schema, TModifiers> = { [K in keyof MethodNames<TModifiers>]: TModifiers[K] extends (...args: any[]) => any ? (...args: Parameters<TModifiers[K]>) => (context: DecoratorContext<TSchema>) => TSchema : never; }; export interface GetBaseSchemaFunction<TSchema, TAllowedTypes> { <TClass extends MapAllowUnions<TClass, TKey, TAllowedTypes>, TKey extends StringOrSymbolKey<TClass>>(options: { joi: typeof Joi; target: TClass; propertyKey: TKey; }): TSchema; } export declare type PropertyDecorator<TAllowedTypes, TSchemaModifiers> = (TypedPropertyDecorator<TAllowedTypes> & TSchemaModifiers); export declare const createPropertyDecorator: <TAllowedTypes, TSchemaModifiers>() => <TSchema extends Joi.Schema>(getBaseSchema: GetBaseSchemaFunction<TSchema, TAllowedTypes>, getModifierProviders: (getJoi: () => typeof Joi) => ModifierProviders<TSchema, TSchemaModifiers>, options: JoifulOptions) => PropertyDecorator<TAllowedTypes, TSchemaModifiers>; export {};