Skip to content

Commit

Permalink
Merge pull request #7 from Theshedman/docs/update-docs
Browse files Browse the repository at this point in the history
fix(docs): update docs
  • Loading branch information
Theshedman authored Nov 28, 2023
2 parents 1f3c1ab + 47c3b68 commit a539bfb
Showing 1 changed file with 64 additions and 28 deletions.
92 changes: 64 additions & 28 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### @password-validator/core
## @password-validator/core

<div style="display:flex; gap:10px; flex-wrap:wrap">
<a href="https://www.npmjs.com/package/@password-validator/core" style="text-decoration:none">
Expand Down Expand Up @@ -29,20 +29,24 @@

---

A zero dependency library for password validation that lets you compose and combine multiple rules to create a validator that suits your needs.
A robust dependency-free, regex-free library for password validation! It lets you compose, combine and/or chain multiple validators to create a validation suite that meets your needs.

### Installation
## Installation

```bash
npm install @password-validator/core
```

### Basic Usage
## Usage

Password validator exposes 2 APIs for validating passwords:

1. __`standard`__ - This allows you register multiple validator classes with the `PasswordValidatorManager` to build a validation suite.

```ts
import { LowerCaseValidator, MaxLengthValidator, MinLengthValidator, PasswordValidatorManager, SpecialCharacterValidator, UpperCaseValidator } from '@password-validator/core';

const pm = new PasswordValidatorManager(); // Create a password validator manager
const pm = PasswordValidatorManager.standard(); // Create a password validator manager

const minLength = new MinLengthValidator(8); // Minimum length of 8 characters
const maxLength = new MaxLengthValidator(16); // Maximum length of 16 characters
Expand All @@ -56,18 +60,52 @@ pm.register(minLength, maxLength, uppercases, lowercases, specialCharacters);
// Use the manager to validate passwords
const result = pm.validate('MyPassword123!*') // { valid: true, messages: [] } --> Password is valid
const result = pm.validate('MyPassword123'); // { valid: false, messages: ['must contain at least 2 special characters.'] } --> Password is invalid
```

2. __`fluent`__ - This allows you chain multiple validators together on the `PasswordValidatorManager` in a more functional programming style. This is useful when you want to validate a password without having to register the validators with the manager.
It still uses the same validators as the `standard` API but provides an interface for chaining them together.


```ts
import { PasswordValidatorManager } from '@password-validator/core';

const result = PasswordValidatorManager.fluent()
.min(6) // Minimum length of 6 characters
.digit(1) // At least 1 digit
.specialCharacter(1) // At least 1 special character
.validate("eihi2kd#"); // { valid: true, messages: [] } --> Password is valid
```

### Validators
NB: Ensure to call the `validate` method with the `password` to be validated. Without this, the password validation will not be triggered.


A validator is class that can validate a password and return a result. Validators should typically be registered with the `PasswordValidatorManager` (as shown above). The library
comes with a number of validators (see table below) that can be used to build a validation suite.
## Validators

Each validator receives a `passwordRule` _(number)_ argument in its constructor which defines the number of expected characters associated with the validator. For example,
the `const minLength = new MinLengthValidator(10)` defined above means that the passowrd must be atleast __10 characters__ long to be considered valid.
A validator is class that can validate a password and return a result. This is used by both the `Standard` and the `Fluent` APIs. The library comes with a number of validators (see table below) that can be used to build a validation suite.

NB: The only exception to this is the `NoSpaceCharacterValidator` which requires no arguments and simply ensures the password contains no spaces.
Each validator receives a _`passwordRule` (number)_ argument, which defines the number of expected characters associated with the validator.
Consider the following example:

```ts
// Standard API
import { MinLengthValidator } from '@password-validator/core';

const minLength = new MinLengthValidator(10);
const result = minLength.validate("eihi2kd#");

// -----------OR---------------

// Fluent API
import { PasswordValidatorManager } from '@password-validator/core';

const result = PasswordValidatorManager.fluent()
.min(10)
.validate("eihi2kd#");
```

The `new MinLengthValidator(10)` or `PasswordValidatorManager.fluent().min(10)` defined above means that the passowrd must be atleast __10 characters__ long to be considered valid.

NB: The only exception to this is the `NoSpaceCharacterValidator` (`PasswordValidatorManager.fluent().noSpace()`) which requires no arguments and simply ensures the password contains no spaces.

### Conflicting Validators

Expand All @@ -77,31 +115,29 @@ a `PasswordValidatorConflictException` error. This is because minimum length can
```ts
import { MinLengthValidator, MaxLengthValidator } from '@password-validator/core';

const pm = new PasswordValidatorManager(); // Create a password validator manager
const pm = PasswordValidatorManager.standard(); // Create a password validator manager

const minLength = new MinLengthValidator(12);
const maxLength = new MaxLengthValidator(8);

pm.register(minLength, maxLength);
pm.register(minLength, maxLength); // Throws PasswordValidatorConflictException --> `minLength cannot be greater than maxLength`

const result = pm.validate('MyPassword123!*') // Throws PasswordValidatorConflictException --> `minLength cannot be greater than maxLength`
const result = pm.validate('MyPassword123!*')
```
---

### Validators & Managers Tables
### Summary Table

Supported Validators and Managers as of now are:

| Rules | Descriptions |
|:----------------------------------------------------------------------------|:-----------------------------------------------------------------------------------------|
| **Validations** |
| **DigitValidator(passwordRule: number)** | specifies password must include _passwordRule_ number of digits |
| **LowerCaseValidator(passwordRule: number)** | specifies password must include at least _passwordRule_ number of lowercase character(s) |
| **UpperCaseValidator(passwordRule: number)** | specifies password must include at least _passwordRule_ number of uppercase character(s) |
| **MaxLengthValidator(passwordRule: number)** | specifies password must not exceed _passwordRule_ number of character(s) |
| **MinLengthValidator(passwordRule: number)** | specifies password must not be less than _passwordRule_ number of character(s) |
| **SpecialCharacterValidator(passwordRule: number)** | specifies password must include at least _passwordRule_ number of special character(s) |
| **NoSpaceCharacterValidator()** | specifies password must not include spaces |
| **Managers** |
| **PasswordValidatorManager().register(...validators: PasswordValidator[])** | Register multiple validators at once to build a validation suite. |

| Standard | Fluent |Description|
|----------|----------|----------|
|**`PasswordValidatorManager.standard()`**|**`PasswordValidatorManager.fluent()`**|The base instance for validation|
|**`DigitValidator(passwordRule: number)`**|**`.digit(passwordRule: number)`**|specifies password must include passwordRule number of digits|
|**`LowerCaseValidator(passwordRule: number)`**|**`.lower(passwordRule: number)`**|specifies password must include at least passwordRule number of lowercase character(s)|
|**`UpperCaseValidator(passwordRule: number)`**|**`.upper(passwordRule: number)`**|specifies password must include at least passwordRule number of uppercase character(s)|
|**`MaxLengthValidator(passwordRule: number)`**|**`.max(passwordRule: number)`**|specifies password must not exceed passwordRule number of character(s)|
|**`MinLengthValidator(passwordRule: number)`**|**`.min(passwordRule: number)`**|specifies password must not be less than passwordRule number of character(s)|
|**`SpecialCharacterValidator(passwordRule: number)`**|**`.specialCharacter(passwordRule: number)`**| specifies password must include at least passwordRule number of special character(s)|
|**`NoSpaceCharacterValidator()`**|**`.noSpace()`**|specifies password must not include spaces|

1 comment on commit a539bfb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 97.3% 144/148
🟢 Branches 92.31% 36/39
🟢 Functions 100% 59/59
🟢 Lines 97.24% 141/145

Test suite run success

63 tests passing in 1 suite.

Report generated by 🧪jest coverage report action from a539bfb

Please sign in to comment.