Learn about the available options, methods and use cases.
Usage
Following is the simple usage of this validator
import e from "validator";
await e.tuple(
[e.string(), e.boolean()], // Pass a list of validators
{} // Optionally pass options
)
.validate(["foo", true]) // returns ["foo", true]
Options
Following are the available options for this validator
interface ITupleValidatorOptions extends TBaseValidatorOptions {
/** Pass custom messages for the errors */
messages?: Partial<
Record<"typeError" | "smallerLength" | "greaterLength", TErrorMessage>
>;
/** Validate array minimum length */
minLength?: number;
/** Validate array maximum length */
maxLength?: number;
/**
* Partialize the underlying validator (makes undefined values in the props acceptable)
*
* Use e.partial() instead, if working with typescript
*/
partial?: boolean;
/**
* Converts the underlying validator's props that are partialized/optional to required
*
* Use e.required() instead, if working with typescript
*/
required?: boolean;
/**
* (Casting Option) Requires `cast` to be `true`
*
* Set a splitter that will be used to split elements in the string and convert it into array during the cast.
*/
splitter?: string | RegExp;
}
Methods
Following are the available methods on this validator
// Pass a rest validator that validates unexpected items in the tuple
.rest(validator: BaseValidator<any, any, any>)
.length(options: { min?: number; max?: number } | number)
.min(length: number)
.max(length: number)
Examples
Read the examples below to understand different use cases