diff --git a/lodash-decorators/lodash-decorators-tests.ts b/lodash-decorators/lodash-decorators-tests.ts index 7808f802e4..60a659c8d5 100644 --- a/lodash-decorators/lodash-decorators-tests.ts +++ b/lodash-decorators/lodash-decorators-tests.ts @@ -314,3 +314,57 @@ class Person11 { } } +// +// https://github.com/steelsojka/lodash-decorators/tree/master/src/validate +// + +import { Validate } from 'lodash-decorators/validate'; + +class Person12 { + name: string; + constructor() {} + + @Validate(_.isString) + setName(name: any) { + this.name = name as string; + } +} + +class Person13 { + name: string; + age: number; + constructor() {} + + @Validate( + _.isString, + [_.isNumber, _.partial(_.lt, 10) as ((_: any) => boolean)] + ) + setData(name: any, age: any) { + this.name = name as string; + this.age = age as number; + } +} + +const person13 = new Person13(); + +person13.setData('test', 5); //=> TypeError +person13.setData('test', 12); //=> Valid + + +// +// Additional typings +// + +import { validateReturn } from 'lodash-decorators/validate'; + +class Calc { + @validateReturn((c: number) => c > 0) + add(a: number, b: number): number { + return a + b; + } + + @validateReturn([c => c > 0]) + mul(a: number, b: number): number { + return a + b; + } +} diff --git a/lodash-decorators/lodash-decorators.d.ts b/lodash-decorators/lodash-decorators.d.ts index daa06627c5..ac01c5cf6e 100644 --- a/lodash-decorators/lodash-decorators.d.ts +++ b/lodash-decorators/lodash-decorators.d.ts @@ -220,3 +220,53 @@ declare module "lodash-decorators/extensions" { export const readonly: MethodDecorator; export const Readonly: MethodDecorator; } + +declare module "lodash-decorators/validate" { + // Originally copied from ../node_modules/typescript/lib/lib.es6.d.ts + export interface ClassDecorator { + (target: TFunction): TFunction|void; + } + export interface PropertyDecorator { + (target: Object, propertyKey: string | symbol): void; + } + export interface MethodDecorator { + (target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor | void; + } + export interface ParameterDecorator { + (target: Object, propertyKey: string | symbol, parameterIndex: number): void; + } + + export interface TypedMethodDecorator { + (target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor | void; + } + + export interface Predicate { + (t: T): boolean; + } + type Predicates = Predicate|Predicate[]; + + export interface ValidateDecorator { + (p1: Predicates): + TypedMethodDecorator<((param1: T1) => R)>; + (p1: Predicates, p2?: Predicates): + TypedMethodDecorator<((param1: T1, param2: T2) => R)>; + (p1: Predicates, p2?: Predicates, p3?: Predicates): + TypedMethodDecorator<((param1: T1, param2: T2, param3: T3) => R)>; + (p1: Predicates, p2?: Predicates, p3?: Predicates, p4?: Predicates): + TypedMethodDecorator<((param1: T1, param2: T2, param3: T3, param4: T4) => R)>; + (p1: Predicates, p2?: Predicates, p3?: Predicates, p4?: Predicates, p5?: Predicates): + TypedMethodDecorator<((param1: T1, param2: T2, param3: T3, param4: T4, param5: T5) => R)>; + (p1: Predicates, p2?: Predicates, p3?: Predicates, p4?: Predicates, p5?: Predicates, p6?: Predicates): + TypedMethodDecorator<((param1: T1, param2: T2, param3: T3, param4: T4, param5: T5, param6: T6) => R)>; + } + + export interface ValidateReturnDecorator { + (p1: Predicates): TypedMethodDecorator<((...args: any[]) => R)>; + } + + export const validate: ValidateDecorator; + export const Validate: ValidateDecorator; + + export const validateReturn: ValidateReturnDecorator; + export const ValidateReturn: ValidateReturnDecorator; +}