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 🙏

© 2024 – Pkg Stats / Ryan Hefner

checkpass

v3.0.0

Published

A micro-library for password validation and setting password constraints.

Downloads

304

Readme

Checkpass

Checkpass allows you to specify the required password strength and then verify whether the requirements are being met. For example you could specify the minimum required length of the password, whether numberic characters are required, whether special characters are required, whether capital letters are required and more. You can also pass in a deny list for disallowing certain characters from the password if necessary.

Checkout the Checkpass Playground

Getting Started

Documentation

Installation

npm install checkpass

API and Usage

import checkpass, { Constraints } from "checkpass";

const constraints: Constraints = {
  minLength: 6,
  minCapitalLetters: 2,
  minNumbers: 2,
  maxNumbers: 5,
  minSpecialCharacters: 2,
  maxSpecialCharacters: 6,
  minUniqueCharacters: 4,
  disallowCharacters: ["$", "*"],
};

const password = "TestingR02#Pass!?<";

/* Call checkpass with the arguments */
const result = checkpass(password, constraints);

/* 
  Checkpass returns an object providing details of all the constraints.
  You can use the return value to see if any of the constraints that you 
  had applied return true; which means that the constraint has failed the check.
*/

if (result.minLength.value) return result.minLength.message;
if (result.minCapitalLetters.value) return result.minCapitalLetters.message;

... and so on for all the required checks. The message will be an empty string if the value is false which indicates that the constraint has passed the check.

The above snippet shows a use case where you check the password against the minimum length, minimum capital letters, min and max numeric characters and special characters and disallows the use of characters($ and *).

If all the required checks pass, then the corresponding result object will have all the values for the constraints set to false and the corresponding message string will be empty as shown below.

Return type for the result

const result: CheckErrors = {
  minLength: { value: false, message: "" },
  maxLength: { value: false, message: "" },
  minCapitalLetters: { value: false, message: "" },
  maxCapitalLetters: { value: false, message: "" },
  minNumbers: { value: false, message: "" },
  maxNumbers: { value: false, message: "" },
  minSpecialCharacters: { value: false, message: "" },
  maxSpecialCharacters: { value: false, message: "" },
  minUniqueCharacters: { value: false, message: "" },
  disallowCharacters: { value: false, message: "" },
  disallowSpaces: { value: false, message: "" },
};

Here is a brief list of checks against which checkpass can check your password.

  • Check 1: Check for spaces and disallow any spaces that remain after trimming the string.
  • Check 2: Check the specified length constraints.
  • Check 3: Check the specified capital letter constraints.
  • Check 4: Check the specified numeric character constraints.
  • Check 5: Check the specified special character constraints.
  • Check 6: Check for unique character constraints if specified. (optional)
  • Check 7: Check if any characters are to be disallowed if specified. (optional)

Besides this, Checkpass also checks whether the specifed constraints can be exercised and throws an error in case of erroneous user inputs such as if a negative number is specified for any constraint or if the minimum value constraint is larger than the maximum value for example.

Specifying the constraints

Constraints against which the password needs to be checked can be specified using an object with the following properties.

  minLength: number;
  maxLength?: number; /* optional */
  minCapitalLetters: number;
  maxCapitalLetters?: number; /* optional */
  minNumbers: number;
  maxNumbers?: number; /* optional */
  minSpecialCharacters: number;
  maxSpecialCharacters?: number; /* optional */
  minUniqueCharacters?: number; /* optional */
  disallowCharacters?: string[]; /* optional */

In case of the required properties you can pass 0 if you do not wish to check against that constraint.

For example

const constraints = {
  minLength: 8,
  minSpecialCharacters: 3,
  minCapitalLetters: 0,
  minNumbers: 0,
};

The password being checked against the above constraints object would require minimum 8 characters with 3 special characters. Here we do not need to use any numeric characters or capital letters since the value specified for them is 0. In case no constraints object is passed checkpass will assume a default object wherein all the required properties are set to zero by default and hence the password is not being checked against anything at all!


License

Checkpass is licensed under MIT License.