use-password-policy 1.0.4 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -51,18 +51,23 @@ function MyComponent() {
51
51
 
52
52
  **Props:**
53
53
 
54
- | Prop Name | Type | Description | Default Value |
55
- | ---------------------- | --------------------------- | ------------------------------------------------------------------------------------------ | -------------------------- |
56
- | `password` | string | The password to be validated. | Required |
57
- | `config` | object (optional) | An object overriding default configuration for built-in policies. | |
58
- | - `minLength` | number | Minimum password length | 8 |
59
- | - `uppercaseCharRegex` | RegExp | Regular expression for uppercase characters | /[A-Z]/ |
60
- | - `digitRegex` | RegExp | Regular expression for digits | /\d/ |
61
- | - `specialCharRegex` | RegExp | Regular expression for special characters | /[!@#$%^&*()_+-=[]{};':"\| |
62
- | `customPolicies` | array of objects (optional) | An array of custom policies to enforce. | |
63
- | - `name` | string | Name of the custom policy for clarity in feedback. | |
64
- | - `regex` | RegExp (optional) | Regular expression for custom validation. | |
65
- | - `checkFunction` | function (optional) | A function taking the password as an argument and returning true/false for a custom check. | |
54
+ | Prop Name | Props Values | Type | Description | Default Value |
55
+ | ---------------- | ---------------------- | --------------------------- | ---------------------------------------------------------------------------------------------------------- | -------------------------- |
56
+ | `password` | | string | The password to be validated. | Required |
57
+ | `config` | | object (optional) | An object overriding default configuration for built-in policies. | |
58
+ | | - `minLength` | number | Minimum password length | 8 |
59
+ | | - `uppercaseCharRegex` | RegExp | Regular expression for uppercase characters | /[A-Z]/ |
60
+ | | - `digitRegex` | RegExp | Regular expression for digits | /\d/ |
61
+ | | - `specialCharRegex` | RegExp | Regular expression for special characters | /[!@#$%^&*()_+-=[]{};':"\| |
62
+ | | - `caseCheck` | boolean | This flag determines the availability of a feature or functionality within your application's state | true |
63
+ | | - `lengthCheck` | boolean | This flag determines the availability of a feature or functionality within your application's state | true |
64
+ | | - `digitCheck` | boolean | This flag determines the availability of a feature or functionality within your application's state | true |
65
+ | | - `specialCharCheck` | boolean | This flag determines the availability of a feature or functionality within your application's state | true |
66
+ | `customPolicies` | | array of objects (optional) | An array of custom policies to enforce. | |
67
+ | | - `name` | string | Name of the custom policy for clarity in feedback. | |
68
+ | | - `regex` | RegExp (optional) | Regular expression for custom validation. | |
69
+ | | - `checkFunction` | function (optional) | A function taking the password as an argument and returning true/false for a custom check. | |
70
+ | useDefaultConfig | | boolean | This flag controls whether to use the default configuration for validations or checks in your application. | true |
66
71
 
67
72
  **Return Value:**
68
73
 
package/Type.ts CHANGED
@@ -8,3 +8,7 @@ export interface CustomPolicy {
8
8
  export interface StateObjects {
9
9
  [key: string]: boolean;
10
10
  }
11
+
12
+ export interface DefaultConfig {
13
+ [key: string]: boolean | RegExp | string | number;
14
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-password-policy",
3
- "version": "1.0.4",
3
+ "version": "1.0.7",
4
4
  "description": "",
5
5
  "main": "use-password-policy.ts",
6
6
  "scripts": {
@@ -1,6 +1,5 @@
1
1
  import { useState, useEffect, useMemo } from "react";
2
- import { CustomPolicy, StateObjects } from "./Type";
3
-
2
+ import { CustomPolicy, StateObjects, DefaultConfig } from "./Type";
4
3
 
5
4
  export const usePasswordPolicy = ({
6
5
  password,
@@ -13,15 +12,15 @@ export const usePasswordPolicy = ({
13
12
  customPolicies?: CustomPolicy[];
14
13
  useDefaultConfig?: boolean;
15
14
  }) => {
16
- const defaultConfig = {
15
+ const defaultConfig: DefaultConfig = {
17
16
  caseCheck: true,
18
17
  lengthCheck: true,
19
18
  digitCheck: true,
20
19
  specialCharCheck: true,
21
- minLength: 8, // Adjust minimum length as needed
22
- uppercaseCharRegex: /[A-Z]/, // Allow customization of regex patterns
20
+ minLength: 8,
21
+ uppercaseCharRegex: /[A-Z]/,
23
22
  digitRegex: /\d/,
24
- specialCharRegex: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?`~]/, // More inclusive special characters
23
+ specialCharRegex: /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?`~]/,
25
24
  };
26
25
 
27
26
  const merged = useMemo(() => mergedConfig(defaultConfig, config), []);
@@ -36,12 +35,22 @@ export const usePasswordPolicy = ({
36
35
 
37
36
  const [policy, setPolicy] = useState<StateObjects>(() => {
38
37
  if (useDefaultConfig) {
39
- return {
38
+ const defaultStates: StateObjects = {
40
39
  caseCheck: false,
41
40
  lengthCheck: false,
42
41
  digitCheck: false,
43
42
  specialCharCheck: false,
44
- // Add properties for custom policies from mergedConfig
43
+ };
44
+ const safeConfig: StateObjects = config || {};
45
+
46
+ Object.entries(defaultStates).map(([key]) => {
47
+ if (safeConfig.hasOwnProperty(key) && safeConfig[key] === false) {
48
+ delete defaultStates[key];
49
+ delete defaultConfig[key];
50
+ }
51
+ });
52
+ return {
53
+ ...defaultStates,
45
54
  ...customPolicies.reduce(
46
55
  (acc, policy: { name: string }) => ({
47
56
  ...acc,
@@ -64,16 +73,24 @@ export const usePasswordPolicy = ({
64
73
 
65
74
  const evaluateChecks = () => {
66
75
  const newPolicy: StateObjects = { ...policy };
76
+ const policyKeys = Object.keys(policy);
77
+
67
78
  if (useDefaultConfig) {
68
- newPolicy.caseCheck =
69
- merged.caseCheck && RegExp(merged.uppercaseCharRegex).test(password);
70
- newPolicy.lengthCheck =
71
- merged.lengthCheck && password.length >= merged.minLength;
72
- newPolicy.digitCheck =
73
- merged.digitCheck && RegExp(merged.digitRegex).test(password);
74
- newPolicy.specialCharCheck =
75
- merged.specialCharCheck &&
76
- RegExp(merged.specialCharRegex).test(password);
79
+ policyKeys.forEach((key) => {
80
+ if (merged.hasOwnProperty(key) && policy.hasOwnProperty(key)) {
81
+ const checkFunction = {
82
+ caseCheck: () => RegExp(merged.uppercaseCharRegex).test(password),
83
+ lengthCheck: () => password.length >= merged.minLength,
84
+ digitCheck: () => RegExp(merged.digitRegex).test(password),
85
+ specialCharCheck: () =>
86
+ RegExp(merged.specialCharRegex).test(password),
87
+ }[key];
88
+
89
+ if (checkFunction) {
90
+ newPolicy[key] = merged[key] && checkFunction();
91
+ }
92
+ }
93
+ });
77
94
  }
78
95
 
79
96
  customPolicies.forEach((customPolicy: CustomPolicy) => {