password-validator vs validator vs zxcvbn
Password Validation Libraries
password-validatorvalidatorzxcvbnSimilar Packages:

Password Validation Libraries

These libraries provide various functionalities for validating and assessing the strength of passwords in web applications. They help developers ensure that users create secure passwords, enhancing overall application security. Each library has its unique approach and features, catering to different needs in password management and validation.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
password-validator028435.8 kB7-MIT
validator023,761817 kB3753 months agoMIT
zxcvbn015,901-1419 years agoMIT

Feature Comparison: password-validator vs validator vs zxcvbn

Password Strength Assessment

  • password-validator:

    This library allows you to define custom password strength rules, such as minimum length, required characters, and patterns. It provides feedback on whether a password meets the specified criteria, ensuring compliance with security policies.

  • validator:

    While primarily focused on data validation, 'validator' does not specifically assess password strength but can be used in conjunction with other libraries to validate password format and structure, such as length and character types.

  • zxcvbn:

    'zxcvbn' offers a sophisticated analysis of password strength by evaluating the entropy and predictability of the password. It provides a score and feedback on how to improve password strength, making it user-friendly.

Customization

  • password-validator:

    Highly customizable, allowing developers to define their own rules and policies for password creation. You can easily adjust the complexity requirements based on your application's needs, making it flexible for various use cases.

  • validator:

    Less focused on password-specific customization, but it offers a variety of validation functions that can be adapted for different input types. It is more suited for general-purpose validation rather than password-specific rules.

  • zxcvbn:

    Limited customization options, as it primarily focuses on analyzing password strength rather than enforcing specific rules. Its strength lies in its ability to provide insights based on common password patterns.

Ease of Use

  • password-validator:

    Designed for simplicity, this library is easy to integrate and use. Developers can quickly implement password validation with minimal setup, making it accessible for projects of all sizes.

  • validator:

    Offers a straightforward API for validation tasks, but may require additional setup for password-specific validation. It is user-friendly for general validation but may not be as intuitive for password rules.

  • zxcvbn:

    Although it provides detailed feedback on password strength, it may require more understanding of its scoring system for effective use. Developers need to familiarize themselves with its output to provide meaningful feedback to users.

Performance

  • password-validator:

    Lightweight and efficient, it performs validations quickly without significant overhead, making it suitable for applications where performance is critical during user input.

  • validator:

    Performance is generally good for validation tasks, but it may not be as optimized specifically for password validation compared to dedicated libraries. It performs well for various input validations but may have overhead for complex rules.

  • zxcvbn:

    While it provides in-depth analysis, it may introduce some latency due to its comprehensive evaluation of password strength. However, the trade-off is valuable insights into password security.

Community and Support

  • password-validator:

    Has a growing community with adequate documentation and examples, making it easier for developers to find support and resources for implementation.

  • validator:

    Well-established with extensive documentation and community support, making it a reliable choice for developers looking for validation solutions across various input types.

  • zxcvbn:

    Supported by a strong community and backed by research, it has good documentation and examples, providing developers with the resources needed to effectively implement password strength assessments.

How to Choose: password-validator vs validator vs zxcvbn

  • password-validator:

    Choose 'password-validator' if you need a simple and customizable solution for enforcing password policies. It allows you to define specific rules for password complexity, making it ideal for applications that require tailored password validation.

  • validator:

    Select 'validator' if you need a comprehensive validation library that covers not just passwords but also other input types. It provides a wide range of validation functions, making it suitable for applications that require extensive data validation beyond just passwords.

  • zxcvbn:

    Opt for 'zxcvbn' if you want a library that evaluates password strength based on real-world data and heuristics. It provides a more nuanced assessment of password strength, making it ideal for applications that prioritize user education on password security.

README for password-validator

logo

npm version npm downloads gh action 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' ]

Advanced usage

Details about failed validations

Sometimes just knowing that the password validation failed or what failed is not enough and it is important too get more context. In those cases, details option can be used to get more details about what failed.

console.log(schema.validate('joke', { details: true }));

The above code will output:

[
  {
    validation: 'min',
    arguments: 8,
    message: 'The string should have a minimum length of 8 characters'
  },
  {
    validation: 'uppercase',
    message: 'The string should have a minimum of 1 uppercase letter'
  },
  {
    validation: 'digits',
    arguments: 2,
    message: 'The string should have a minimum of 2 digits'
  }
]

Custom validation messages

The validation messages can be overriden by providing a description of the validation. For example:

schema.not().uppercase(8, 'maximum 8 chars in CAPS please')

The above validation, on failure, should return the following object:

  {
    validation: 'min',
    arguments: 8,
    inverted: true,
    message: 'maximum 8 chars in CAPS please'
  },

Plugins

Plugin functions can be added to the password validator schema for custom password validation going beyond the rules provided here. For example:

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

var schema = new passwordValidator()
    .min(3, 'Password too small')
    .usingPlugin(validator.isEmail, 'Password should be an email');

schema.validate('not-an-email', { details: true })
// [{ validation: 'usingPlugin', arguments: [Function: isEmail], message: 'Password should be an email' }]

Rules

Rules supported as of now are:

RulesDescriptions
digits([count], [description])specifies password must include digits (optionally provide count paramenter to specify at least n digits)
letters([count], [description])specifies password must include letters (optionally provide count paramenter to specify at least n letters)
lowercase([count], [description])specifies password must include lowercase letters (optionally provide count paramenter to specify at least n lowercase letters)
uppercase([count], [description])specifies password must include uppercase letters (optionally provide count paramenter to specify at least n uppercase letters)
symbols([count], [description])specifies password must include symbols (optionally provide count paramenter to specify at least n symbols)
spaces([count], [description])specifies password must include spaces (optionally provide count paramenter to specify at least n spaces)
min(len, [description])specifies minimum length
max(len, [description])specifies maximum length
oneOf(list)specifies the whitelisted values
not([regex], [description])inverts the result of validations applied next
is()inverts the effect of not()
has([regex], [description])inverts the effect of not() and applies a regex (optional)
usingPlugin(fn, [description])Executes custom function and include its result in password validation

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