npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

password-validator-ap

v1.1.0

Published

A password validation library

Downloads

35

Readme

Password Validator

A lightweight JavaScript library for validating passwords against common security rules.
The validator checks string type, length, character requirements, blacklist, and more.

npm version npm downloads License: MIT

Student project disclaimer

This is a project made entirely by me, Alex Palm, for the university course 1DV610 at Linneuniversitetet. This is a disclaimer because of that fact. If you were to use this library, use it with caution. However feel free to let me know if there are improvements to be made through the available communication channels here.

Table of Contents

Install

npm install password-validator-ap

Usage/code examples

Basic usage (no config file):

import PasswordValidator from "password-validator-ap";

const validator = new PasswordValidator(); 
validator.setMinLength(6) // Set the minimum length of the password
validator.setMaxLength(80) // Set the maximum length of the password

try {
	validator.validate("MyPassword123!", "myusername");
	console.log("Password is valid!");
} catch (err) {
	console.error("Validation error:", err.message);
}

Usage with config file (async):

import PasswordValidator from "password-validator-ap";

(async () => {
	const validator = await PasswordValidator.loadConfig(); // Loads the config file if present
	try {
		validator.validate("MyPassword123!", "myusername"); // Send arguments for validation
		console.log("Password is valid!");
	} catch (err) {
		console.error("Validation error:", err.message);
	}
})();

Checking min/max range and blacklist:

console.log("Min length:", validator.getMinLength()); // get set minimum length for valid password
console.log("Max length:", validator.getMaxLength()); // get set maximum length for valid password
console.log("Blacklist:", validator.getBlacklist()); // get list of blacklisted passwords

Example of config file

<password-validator.config.js>

export default {
  minLength: 66,
  maxLength: 99,
  blacklist: ["password", "admin", "abc123"], // Add strings to be blacklisted or import array with strings
};

API

Constructor

new PasswordValidator(config = {})

Creates a new instance.

  • config (object, optional): Configuration object. Supports minLength, maxLength, and blacklist.

static async loadConfig() Loads configuration from password-validator.config.js and returns a PasswordValidator instance.

  • Returns: Promise<PasswordValidator>

validate(password, username) Validates a password against all rules.

  • password (string): The password to validate.
  • username (string): The username to compare against.
  • Returns: boolean (true if valid)
  • Throws: Error, TypeError, or RangeError if validation fails.

setMinLength(min) Sets the minimum password length.

  • min (number): Minimum length.

setMaxLength(max) Sets the maximum password length.

  • max (number): Maximum length.

getMinLength() Returns the current minimum password length.

  • Returns: number

getMaxLength() Returns the current maximum password length.

  • Returns: number

getBlacklist() Returns the current blacklist of disallowed passwords.

  • Returns: string[]

Validation Methods (Advanced Usage) You can use these methods directly for custom validation flows:

  • validateString(str) — Throws if not a string.
  • validateLength(str, minLength, maxLength) — Throws if length is invalid.
  • containsUppercase(str) — Throws if no uppercase letter.
  • containsLowercase(str) — Throws if no lowercase letter.
  • containsDigit(str) — Throws if no digit.
  • containsSpecialChar(str) — Throws if no special character.
  • doesNotContainWhitespace(str) — Throws if whitespace is present.
  • containsSameCharacter(str) — Throws if all characters are the same.
  • passwordEqualToUsername(password, username) — Throws if password equals username.
  • passwordIsBlacklisted(str) — Throws if password is blacklisted.

Rules in validator

| Rule | Description | |------------------------|-----------------------------------------------------------------------------| | String type | Password must be a string value. Throws an error if not a string. | | Length check | Password length must be between 6 and 80 characters. | | Uppercase requirement | Password must contain at least one uppercase letter (A–Z). | | Lowercase requirement | Password must contain at least one lowercase letter (a–z). | | Digit requirement | Password must contain at least one digit (0–9). | | Special character | Password must contain at least one special character (e.g. !@#$%^&*). | | Whitespace restriction | Password must not contain whitespace (spaces, tabs, newlines). | | Unique characters | Password must not consist of just a single repeated character. | | Not equal to username | Password must not be the same as the provided username. | | Blacklist restriction | Password must not appear in the configured blacklist of common passwords.|

Bugs/feedback

  • List off known bugs to be aware off * To send feedback on this library, such as constructive feedback and points of improvement, create a issue on the Github page:

Testing/coverage

Automatic testing is done with the Jest framework. As off version 1.0.2 the application has 60-70% coverage.

Roadmap

  • Allow custom validation rules
  • Support multiple errors at once instead of failing on the first
  • Add configuration options (e.g., set custom min/max length)

License

MIT License. See License: MIT for details