password-validator

Validates password according to flexible and intuitive specifications

Usage no npm install needed!

<script type="module">
  import passwordValidator from 'https://cdn.skypack.dev/password-validator';
</script>

README

logo

npm version npm downloads build status coverage status

Install

npm install password-validator

Usage

var passwordValidator = require('password-validator');

// Create a schema
var schema = new passwordValidator();

// Add properties to it
schema
.is().min(8)                                    // Minimum length 8
.is().max(100)                                  // Maximum length 100
.has().uppercase()                              // Must have uppercase letters
.has().lowercase()                              // Must have lowercase letters
.has().digits(2)                                // Must have at least 2 digits
.has().not().spaces()                           // Should not have spaces
.is().not().oneOf(['Passw0rd', 'Password123']); // Blacklist these values

// Validate against a password string
console.log(schema.validate('validPASS123'));
// => true
console.log(schema.validate('invalidPASS'));
// => false

// Get a full list of rules which failed
console.log(schema.validate('joke', { list: true }));
// => [ 'min', 'uppercase', 'digits' ]

Rules

Rules supported as of now are:

Rules Descriptions
digits([count]) specifies password must include digits (optionally provide count paramenter to specify at least n digits)
letters([count]) specifies password must include letters (optionally provide count paramenter to specify at least n letters)
lowercase([count]) specifies password must include lowercase letters (optionally provide count paramenter to specify at least n lowercase letters)
uppercase([count]) specifies password must include uppercase letters (optionally provide count paramenter to specify at least n uppercase letters)
symbols([count]) specifies password must include symbols (optionally provide count paramenter to specify at least n symbols)
spaces([count]) specifies password must include spaces (optionally provide count paramenter to specify at least n spaces)
min(len) specifies minimum length
max(len) specifies maximum length
oneOf(list) specifies the whitelisted values
not([regex]) inverts the result of validations applied next
is() inverts the effect of not()
has([regex]) inverts the effect of not() and applies a regex (optional)

Options

The following options can be passed to validate method:

  • list - If set, validate method returns a list of rules which failed instead of true/false.

Resources

For APIs of other older versions, head to Wiki.

License

MIT License