UNPKG

@ne1410s/codl

Version:

Indulge your apps with codl: a component model library

72 lines (71 loc) 3.73 kB
import 'reflect-metadata'; import { TypedPropertyDecorator, CustomValidator } from '../types'; /** Decorators for validation purposes. */ export declare abstract class Validation { /** * Makes a property required. 'Unprovided' values (null, undefined, '') are * invalid, as is NaN. Anything else is valid (including: 0, 0n, false). */ static readonly required: PropertyDecorator; /** * Makes a property forbidden. 'Unprovided' values (null, undefined, '') are * valid. Anything else is considered invalid (including: 0, 0n, false). */ static readonly forbidden: PropertyDecorator; /** * Associates a string or array with a minimum length. 'Unprovided' values * (null, undefined, '') are not tested hence valid. Otherwise the string * length must not be less than the bound supplied. * @param lBound The minimum length. */ static readonly minLength: <T>(lBound: number) => TypedPropertyDecorator<ArrayLike<T>>; /** * Associates a string or array with a maximum length. 'Unprovided' values * (null, undefined, '') are not tested hence valid. Otherwise the string * length must not exceed the upper bound supplied. * @param uBound The maximum length. */ static readonly maxLength: <T>(uBound: number) => TypedPropertyDecorator<ArrayLike<T>>; /** * Associates a property with a minimum value. 'Unprovided' values (null, * undefined, '') are not tested hence valid. Otherwise the value must not be * less than the lower bound supplied to be valid. * @param lBound The minimum value. */ static readonly min: <T extends Number | Date>(lBound: T) => TypedPropertyDecorator<T | ArrayLike<T>>; /** * Associates a number or date property with a maximum value. 'Unprovided' * values (null, undefined, '') are not tested hence valid. Otherwise the * value must not exceed the upper bound supplied to be valid. * @param uBound The maximum value. */ static readonly max: <T extends Number | Date>(uBound: T) => TypedPropertyDecorator<T | ArrayLike<T>>; /** * Associates a property with minimum and maximum values. 'Unprovided' values * (null, undefined, '') are not tested hence valid. Otherwise the value must * be (inclusively) between the lower and upper bounds. * @param lBound The minimum value. */ static readonly range: <T extends Number | Date>(lBound: T, uBound: T) => TypedPropertyDecorator<T | ArrayLike<T>>; /** * Associates a property with a pattern. 'Unprovided' values (null, undefined, * '') are not tested hence valid. Otherwise the result of .toString() must * match the regex supplied to be valid. * @param regex The validation pattern. */ static readonly regex: (regex: string | RegExp) => PropertyDecorator; /** * Associates a property with a predefined set of allowed values. Primitive * types, enums and arrays thereof are all supported. */ static readonly options: <T extends String | Number | Boolean>(...opts: T[]) => TypedPropertyDecorator<T | ArrayLike<T>>; /** * Associates a property with a custom validator. Unlike most other validation * decorators, ALL values will be tested (rather than being skipped if it was * deemed 'unprovided') - the decision is not taken on the caller's behalf. If * the function returns false, the value is deemed invalid and a generic error * is used. Else if it returns a string, this string is used as the message, * with null or empty strings taken to indicate that the value is valid). */ static readonly custom: <T>(fn: CustomValidator) => TypedPropertyDecorator<T>; }