Password
Validating password is rather simple but time consuming task, valid8r password validators provides a handful of options to best suit your validation requirements
Valid8r covers all the basic as well as advanced password validations that covers 99% edge cases
Start by importing valid8r into your file:
import valid8r from '@c4code/valid8r';
Configuration Options
Valid8r password validator provides handful of options to best meet your requirements and validation needs, the options that can be configured in password validator are:
import valid8r from '@c4code/valid8r';
const password: string = "Jo#n Smith";
// The configuration can be defaulted globally
valid8r.address(password, {
safe: false, // boolean
minLen: 8, // number
maxLen: 99, // number
requireSpChars: false, // boolean or string[]
requireNum: false, // boolean
requireUpper: true, // boolean
requireLower: true, // boolean
noConsecutiveSpaces: true, // boolean
throwErrorsAs: "throw-all", // "throw-first" | "throw-last" | "throw-all",
/*
"throw-first" => Throws or returns the first error encountered,
"throw-last" => Throws or returns the last error encountered,
"throw-all" => Throws or returns all errors in a Array of string
*/
}); // the second argument is optional
The requireSpChars
field accepts special characters which are required to be included in the address, it accepts either a boolean or a array of special characters like:
requireSpChars: [',', '#']; // Accepts these sp. characters only
// or
requireSpChars: true;
If provided a array of special characters, only that characters will be considered valid, if a boolean is provided all the special characters will be allowed or disallowed.
If not provided explicitly will not require any sp. chars.
The Configuration can be globally set for all the validators in your project, know more?
Basic Usage
Here's how we can use the email validator in it's most basic manner:
Start by importing valid8r:
import valid8r from '@c4code/valid8r';
import valid8r from '@c4code/valid8r';
const password: string = "Jo#n Smith";
// The configuration can be defaulted globally
valid8r.password(password, {
requireSpChars: ['$'],
}); // throws PasswordValidationFailed error as '#' is not in 'requireSpChars'
By default the password validator does not require any special characters to be passed explicitly, if we want to make a special character, either all or specific ones, we must set the 'requireSpChars' attribute
Safe handling
By default, all the validations in valid8r works on unsafe flag, meaning if any of the validation fails it will throw an Error.
However, if you don't want it to throw any error but return them. set the safe flag to true.
import valid8r from '@c4code/valid8r';
const password: string = "Jo#n Smith";
// The configuration can be defaulted globally
const [isValid, errors] = valid8r.password(password, {
safe: true
requireSpChars: ['$'],
});
if (!isValid) {
console.log(errors);
// [{ requireSpChars : "No Special characters found!" }]
}
Error Messages
If you don't like the default error message and want to set a custom error message for any specific fields, pass an object as a third argument of the password function:
import valid8r from '@c4code/valid8r';
const password: string = "John";
// The configuration can be defaulted globally
const [isValid, errors] = valid8r.password(password, {
safe: true
minLen: 8,
}, {
minLen: "Minimum {minLen} chars are required for password"
});
if (!isValid) {
console.log(errors);
// [{ minLen : "Minimum 8 chars are required for password" }]
}
Error Message Options
Valid8r password validator provides handful of options to configure error messages to best fit your project requirements
import valid8r from '@bitbybit/valid8r';
const password: string = "Johnyy_rett";
// The configuration can be defaulted globally
valid8r.password(password, { /* Field Configurations */ }, {
minLen:
"Password must be at least {minLen} characters long.", // string
maxLen:
"Password must not exceed {maxLen} characters.", // string
requireSpChars:
"Password must include at least one of the following special characters: ${allowedSpChars}", // string
requireNum:
"Password must include at least one numeric character.", // string
requireUpper:
"Password must include at least one uppercase letter.", // string
requireLower:
"Password must include at least one lowercase letter.", // string
noConsecutiveSpaces:
"Password must not contain consecutive spaces.", // string
}); // the second and third argument is optional
All the options are optional, if passed will be used as the configuration for that specific validator
The Error messages can be globally set for all the validators in your project, know more?
Last updated