instanceOf
Learn about the available options, methods and use cases.
Usage
Following is the simple usage of this validator
import e from "validator";
await e.instanceOf(
Date, // Pass a constructor
{} // Optionally pass options
)
.validate(new Date()) // returns new Date()Options
Following are the available options for this validator
interface IInstanceOfValidatorOptions<
AllowUndefined extends boolean,
Input,
RestArgs extends Array<any>,
Args = [Input, ...RestArgs][number],
> extends TBaseValidatorOptions {
/** Pass custom messages for the errors */
messages?: Partial<Record<"typeError", TErrorMessage>>;
/**
* If passed `true`, the validator will act as an optional validator with the default value as the instance of the class passed.
*/
allowUndefined?: AllowUndefined;
/**
* If passed `true`, the validator will try to instantiate the class with the input value.
*/
instantiate?: boolean;
/**
* If `instantiate` is set to `true`, the validator tries to instantiate the class with the input value in the first argument, You can pass the rest of the arguments here if any.
*/
instantiationArgs?: Args[] | ((value: Input) => Args[] | Promise<Args[]>);
/**
* If `instantiate` is set to `true`, the validator tries to instantiate the class with the input value in the first argument, You can pass the rest of the arguments here if any.
*/
instantiationRestArgs?:
| RestArgs
| ((value: Input) => RestArgs | Promise<RestArgs>);
}Examples
Read the examples below to understand different use cases
Case 1 (Basic usage)
Case 2 (Usage with allowUndefined)
Case 3 (With instantiate)
Last updated
Was this helpful?