Skip to content

Commit

Permalink
Improve internal validator typing.
Browse files Browse the repository at this point in the history
  • Loading branch information
RillingDev committed Mar 15, 2021
1 parent c84fbd1 commit b64dd5e
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/validator/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,41 @@ import type {
*
* @internal
*/
type ValidatableElementFunction<TResult> = (
val: ValidatableElementValue,
element: ValidatableElement,
e?: Event
) => TResult;
type ValidatableElementFunction<
TResult,
UElement extends ValidatableElement,
VElementValue extends ValidatableElementValue
> = (val: VElementValue, element: UElement, e?: Event) => TResult;

/**
* Function that returns a validation message.
*
* @public
*/
type ValidationMessageFunction = ValidatableElementFunction<string>;
type ValidationMessageFunction<
UElement extends ValidatableElement,
VElementValue extends ValidatableElementValue
> = ValidatableElementFunction<string, UElement, VElementValue>;

/**
* Function that checks if the element value is valid.
*
* @public
*/
type ValidatorFunction = ValidatableElementFunction<boolean>;
type ValidatorFunction<
UElement extends ValidatableElement,
VElementValue extends ValidatableElementValue
> = ValidatableElementFunction<boolean, UElement, VElementValue>;

/**
* Interface for a single validator.
*
* @public
*/
export interface Validator {
fn: ValidatorFunction;
msg: string | ValidationMessageFunction;
export interface Validator<
UElement extends ValidatableElement = ValidatableElement,
VElementValue extends ValidatableElementValue = ValidatableElementValue
> {
fn: ValidatorFunction<UElement, VElementValue>;
msg: string | ValidationMessageFunction<UElement, VElementValue>;
}

0 comments on commit b64dd5e

Please sign in to comment.