type ArrayType = Array | ReadonlyArray | Readonly>; type ValueValidationResult = (TValue extends ArrayType ? Array> | string | null : TValue extends object ? { [propertyName in keyof TValue]?: ValueValidationResult; } | string | null : string | null) | string | null; type ValidationErrors = { [propertyName in keyof TModel]?: ValueValidationResult; }; type AppliesTo = 'AppliesToAllValidators' | 'AppliesToCurrentValidator'; type MessageGenerator = (value: TTransformedValue, model: TModel) => string; type Message = string | MessageGenerator; type SimplePredicate = (value: TTransformedValue, model: TModel) => boolean; type SimpleAsyncPredicate = (value: TTransformedValue, model: TModel) => Promise; type SimplePredicateWithMessage = { predicate: SimplePredicate; message: Message; }; type SimpleAsyncPredicateWithMessage = { predicate: SimpleAsyncPredicate; message: Message; }; type Predicate = SimplePredicate | SimplePredicateWithMessage | Array | SimplePredicateWithMessage>; type AsyncPredicate = SimpleAsyncPredicate | SimpleAsyncPredicateWithMessage | Array | SimpleAsyncPredicateWithMessage>; interface IValidator { validate: (model: TModel) => ValidationErrors; } type IfString = TValue extends string ? TOut : never; type IfNumber = TValue extends number ? TOut : never; type IfObject = TValue extends object ? TOut : never; type NotNullRuleOptions = { includeUndefined?: boolean; }; type NullRuleOptions = { includeUndefined: boolean; }; type RuleValidators = { notEqual: (forbiddenValue: TValue) => RuleValidatorsAndExtensions; equal: (requiredValue: TValue) => RuleValidatorsAndExtensions; must: (predicate: Predicate) => RuleValidatorsAndExtensions; notNull: (options?: NotNullRuleOptions) => RuleValidatorsAndExtensions; notUndefined: () => RuleValidatorsAndExtensions; null: (ruleOptions?: NullRuleOptions) => RuleValidatorsAndExtensions; undefined: () => RuleValidatorsAndExtensions; notEmpty: IfString RuleValidatorsAndExtensions>; length: IfString RuleValidatorsAndExtensions>; maxLength: IfString RuleValidatorsAndExtensions>; minLength: IfString RuleValidatorsAndExtensions>; matches: IfString RuleValidatorsAndExtensions>; emailAddress: IfString RuleValidatorsAndExtensions>; lessThan: IfNumber RuleValidatorsAndExtensions>; lessThanOrEqualTo: IfNumber RuleValidatorsAndExtensions>; greaterThan: IfNumber RuleValidatorsAndExtensions>; greaterThanOrEqualTo: IfNumber RuleValidatorsAndExtensions>; exclusiveBetween: IfNumber RuleValidatorsAndExtensions>; inclusiveBetween: IfNumber RuleValidatorsAndExtensions>; precisionScale: IfNumber RuleValidatorsAndExtensions>; setValidator: IfObject IValidator>) => RuleValidatorsAndExtensions>; }; type WhenCondition = (condition: (model: TModel) => boolean, appliesTo?: AppliesTo) => RuleValidators; type UnlessCondition = (condition: (model: TModel) => boolean, appliesTo?: AppliesTo) => RuleValidators; type RuleValidatorsAndConditionExtensions = RuleValidators & { when: WhenCondition; unless: UnlessCondition; }; type WithMessage = (message: string) => RuleValidatorsAndConditionExtensions; type RuleValidatorsAndExtensions = RuleValidatorsAndConditionExtensions & { withMessage: WithMessage; }; /** * Constrain * @desc Constrains type `T` to only those properties that exist in type `U` */ type Constrain = { [key in keyof T]: key extends keyof U ? T[key] : never; }; type FlatType = string | number | boolean | symbol; type TransformedValue = TValue | FlatType | null | undefined; type Optional = T | null | undefined; type IfNotNeverThen = TValue extends never ? never : TOut; declare class SyncValidator { private valueValidatorBuildersByPropertyName; protected _validate: (value: TModel) => ValidationErrors; validate: (value: TModel) => ValidationErrors; private rebuildValidate; protected ruleFor: (propertyName: TPropertyName) => RuleValidators; protected ruleForTransformed: >(propertyName: TPropertyName, transformValue: (value: TValue) => TTransformedValue extends object ? Constrain : TTransformedValue) => RuleValidators; protected ruleForEach: > ? TEachValueInferred : never, TValue extends TModel[TPropertyName] & ArrayType>(propertyName: IfNotNeverThen) => IfNotNeverThen>; protected ruleForEachTransformed: > ? TEachValueInferred : never, TValue extends TModel[TPropertyName] & ArrayType, TEachTransformedValue extends TransformedValue>(propertyName: IfNotNeverThen, transformValue: (value: TEachValue) => TEachTransformedValue extends object ? Constrain : TEachTransformedValue) => IfNotNeverThen>; } interface IAsyncValidator { validateAsync: (model: TModel) => Promise>; } type AsyncRuleValidators = { notEqual: (forbiddenValue: TValue) => AsyncRuleValidatorsAndExtensions; equal: (requiredValue: TValue) => AsyncRuleValidatorsAndExtensions; must: (predicate: Predicate) => AsyncRuleValidatorsAndExtensions; mustAsync: (predicate: AsyncPredicate) => AsyncRuleValidatorsAndExtensions; notNull: (ruleOptions?: NotNullRuleOptions) => AsyncRuleValidatorsAndExtensions; notUndefined: () => AsyncRuleValidatorsAndExtensions; null: (ruleOptions?: NullRuleOptions) => AsyncRuleValidatorsAndExtensions; undefined: () => AsyncRuleValidatorsAndExtensions; notEmpty: IfString AsyncRuleValidatorsAndExtensions>; length: IfString AsyncRuleValidatorsAndExtensions>; maxLength: IfString AsyncRuleValidatorsAndExtensions>; minLength: IfString AsyncRuleValidatorsAndExtensions>; matches: IfString AsyncRuleValidatorsAndExtensions>; emailAddress: IfString AsyncRuleValidatorsAndExtensions>; lessThan: IfNumber AsyncRuleValidatorsAndExtensions>; lessThanOrEqualTo: IfNumber AsyncRuleValidatorsAndExtensions>; greaterThan: IfNumber AsyncRuleValidatorsAndExtensions>; greaterThanOrEqualTo: IfNumber AsyncRuleValidatorsAndExtensions>; exclusiveBetween: IfNumber AsyncRuleValidatorsAndExtensions>; inclusiveBetween: IfNumber AsyncRuleValidatorsAndExtensions>; precisionScale: IfNumber AsyncRuleValidatorsAndExtensions>; setValidator: IfObject IValidator>) => AsyncRuleValidatorsAndExtensions>; setAsyncValidator: IfObject IAsyncValidator>) => AsyncRuleValidatorsAndExtensions>; }; type AsyncWhenCondition = (condition: (model: TModel) => boolean, appliesTo?: AppliesTo) => AsyncRuleValidators; type AsyncUnlessCondition = (condition: (model: TModel) => boolean, appliesTo?: AppliesTo) => AsyncRuleValidators; type AsyncRuleValidatorsAndConditionExtensions = AsyncRuleValidators & { when: AsyncWhenCondition; unless: AsyncUnlessCondition; }; type AsyncWithMessage = (message: string) => AsyncRuleValidatorsAndConditionExtensions; type AsyncRuleValidatorsAndExtensions = AsyncRuleValidatorsAndConditionExtensions & { withMessage: AsyncWithMessage; }; declare class AsyncValidator { private asyncValueValidatorBuildersByPropertyName; protected _validateAsync: (value: TModel) => Promise>; validateAsync: (value: TModel) => Promise>; private rebuildValidateAsync; protected ruleFor: (propertyName: TPropertyName) => AsyncRuleValidators; protected ruleForTransformed: >(propertyName: TPropertyName, transformValue: (value: TValue) => TTransformedValue extends object ? Constrain : TTransformedValue) => AsyncRuleValidators; protected ruleForEach: > ? TEachValueInferred : never, TValue extends TModel[TPropertyName] & ArrayType>(propertyName: IfNotNeverThen) => IfNotNeverThen>; protected ruleForEachTransformed: > ? TEachValueInferred : never, TValue extends TModel[TPropertyName] & ArrayType, TEachTransformedValue extends TransformedValue>(propertyName: IfNotNeverThen, transformValue: (value: TEachValue) => TEachTransformedValue extends object ? Constrain : TEachTransformedValue) => IfNotNeverThen>; } export { type AsyncRuleValidators, AsyncValidator, type RuleValidators, type ValidationErrors, SyncValidator as Validator, type ValueValidationResult };