string
Learn about the available options, methods and use cases.
Usage
Following is the simple usage of this validator
import e from "validator";
await e.string(
{} // Optionally pass options
)
.validate("foo") // returns "foo"Options
Following are the available options for this validator
interface IStringValidatorOptions extends TBaseValidatorOptions {
/** Pass custom messages for the errors */
messages?: Partial<
Record<
| "typeError"
| "smallerLength"
| "greaterLength"
| "matchFailed"
| "invalidChoice"
| "numberLike"
| "notNumberLike"
| "invalidURL",
TErrorMessage
>
>;
/** Validate string as URL */
isUrl?: boolean;
/** Transform string to URL object. (isUrl option is required) */
returnURLInstance?: boolean;
/** Validate string minimum length */
minLength?: number;
/** Validate string maximum length */
maxLength?: number;
/** Pass a string enum */
choices?: string[];
/** Pass a list of acceptable regular expressions */
patterns?: RegExp[];
/** Validate if string isNaN */
isNaN?: boolean;
}Methods
Following are the available methods on this validator
Examples
Read the examples below to understand different use cases
Case 1 (Using validator options)
Case 2 (Using validator methods)
Case 3
Last updated
Was this helpful?