@open-tender/utils 0.5.59 → 0.5.60

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.
@@ -35,3 +35,4 @@ export * from './useSendResetForm';
35
35
  export * from './useSigninForm';
36
36
  export * from './useSignUpGuestForm';
37
37
  export * from './useSignUpForm';
38
+ export * from './usePasswordValidation';
@@ -38,3 +38,4 @@ tslib_1.__exportStar(require("./useSendResetForm"), exports);
38
38
  tslib_1.__exportStar(require("./useSigninForm"), exports);
39
39
  tslib_1.__exportStar(require("./useSignUpGuestForm"), exports);
40
40
  tslib_1.__exportStar(require("./useSignUpForm"), exports);
41
+ tslib_1.__exportStar(require("./usePasswordValidation"), exports);
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Custom hook for password field validation and state management
3
+ * Follows SRP by encapsulating all password-related state and logic
4
+ */
5
+ export declare const usePasswordValidation: () => {
6
+ handlePasswordChange: () => void;
7
+ isPasswordValid: (password: string | undefined) => boolean;
8
+ shouldShowRequirements: (password: string | undefined, hidePassword?: boolean) => boolean;
9
+ getPasswordErrors: (password: string | undefined, errors: Record<string, string>, fieldName?: string) => Record<string, string>;
10
+ };
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePasswordValidation = void 0;
4
+ const react_1 = require("react");
5
+ const passwordValidation_1 = require("../utils/passwordValidation");
6
+ /**
7
+ * Custom hook for password field validation and state management
8
+ * Follows SRP by encapsulating all password-related state and logic
9
+ */
10
+ const usePasswordValidation = () => {
11
+ const [passwordTouched, setPasswordTouched] = (0, react_1.useState)(false);
12
+ const handlePasswordChange = () => {
13
+ if (!passwordTouched) {
14
+ setPasswordTouched(true);
15
+ }
16
+ };
17
+ const isPasswordValid = (password) => {
18
+ if (!password)
19
+ return false;
20
+ const validation = (0, passwordValidation_1.validatePassword)(password);
21
+ return validation.isValid;
22
+ };
23
+ const shouldShowRequirements = (password, hidePassword = false) => {
24
+ return !hidePassword && !!password;
25
+ };
26
+ const getPasswordErrors = (password, errors, fieldName = 'password') => {
27
+ const passwordErrors = {};
28
+ if (errors[fieldName]) {
29
+ // If password is valid according to frontend but still has error, it's from backend
30
+ const isValidFrontend = password && (0, passwordValidation_1.validatePassword)(password).isValid;
31
+ if (isValidFrontend || !password) {
32
+ passwordErrors[fieldName] = errors[fieldName];
33
+ }
34
+ }
35
+ return passwordErrors;
36
+ };
37
+ return {
38
+ handlePasswordChange,
39
+ isPasswordValid,
40
+ shouldShowRequirements,
41
+ getPasswordErrors
42
+ };
43
+ };
44
+ exports.usePasswordValidation = usePasswordValidation;
@@ -18,3 +18,4 @@ export * from './version';
18
18
  export * from './theme';
19
19
  export * from './favorites';
20
20
  export * from './pizzaBuilder';
21
+ export * from './passwordValidation';
@@ -22,3 +22,4 @@ tslib_1.__exportStar(require("./version"), exports);
22
22
  tslib_1.__exportStar(require("./theme"), exports);
23
23
  tslib_1.__exportStar(require("./favorites"), exports);
24
24
  tslib_1.__exportStar(require("./pizzaBuilder"), exports);
25
+ tslib_1.__exportStar(require("./passwordValidation"), exports);
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Password validation utility matching backend password policy
3
+ *
4
+ * Requirements:
5
+ * - Minimum 10 characters
6
+ * - At least one uppercase letter
7
+ * - At least one lowercase letter
8
+ * - At least one number
9
+ * - At least one special character from allowed set: !@#$%^&_-+=<>
10
+ *
11
+ * Backend regex: ^[A-Za-z0-9\!\@\#\$\%\^\&\_\-\+\=\<\>]*$
12
+ */
13
+ export interface PasswordValidationResult {
14
+ isValid: boolean;
15
+ errors: string[];
16
+ }
17
+ export interface PasswordRequirement {
18
+ met: boolean;
19
+ message: string;
20
+ }
21
+ export declare const validatePassword: (password: string) => PasswordValidationResult;
22
+ export declare const getPasswordRequirements: (password: string) => PasswordRequirement[];
23
+ export declare const getPasswordErrorMessage: (password: string) => string;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ /**
3
+ * Password validation utility matching backend password policy
4
+ *
5
+ * Requirements:
6
+ * - Minimum 10 characters
7
+ * - At least one uppercase letter
8
+ * - At least one lowercase letter
9
+ * - At least one number
10
+ * - At least one special character from allowed set: !@#$%^&_-+=<>
11
+ *
12
+ * Backend regex: ^[A-Za-z0-9\!\@\#\$\%\^\&\_\-\+\=\<\>]*$
13
+ */
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.getPasswordErrorMessage = exports.getPasswordRequirements = exports.validatePassword = void 0;
16
+ // Constants
17
+ const MIN_LENGTH = 10;
18
+ const ALLOWED_SPECIAL_CHARS = '!@#$%^&_-+=<>';
19
+ // Regex patterns - DRY principle
20
+ const REGEX = {
21
+ ALLOWED_CHARS: /^[A-Za-z0-9!@#$%^&_\-+=<>]*$/,
22
+ UPPERCASE: /[A-Z]/,
23
+ LOWERCASE: /[a-z]/,
24
+ NUMBER: /[0-9]/,
25
+ SPECIAL_CHAR: /[!@#$%^&_\-+=<>]/,
26
+ };
27
+ // Error messages - DRY principle
28
+ const ERROR_MESSAGES = {
29
+ MIN_LENGTH: `Password must be at least ${MIN_LENGTH} characters long`,
30
+ INVALID_CHARS: `Password contains invalid characters. Only letters, numbers, and (${ALLOWED_SPECIAL_CHARS}) are allowed`,
31
+ UPPERCASE: 'Password must include at least one uppercase letter',
32
+ LOWERCASE: 'Password must include at least one lowercase letter',
33
+ NUMBER: 'Password must include at least one number',
34
+ SPECIAL_CHAR: `Password must include at least one special character (${ALLOWED_SPECIAL_CHARS})`,
35
+ };
36
+ // SRP: Individual validation functions
37
+ const hasMinLength = (password) => password.length >= MIN_LENGTH;
38
+ const hasOnlyAllowedChars = (password) => REGEX.ALLOWED_CHARS.test(password);
39
+ const hasUppercase = (password) => REGEX.UPPERCASE.test(password);
40
+ const hasLowercase = (password) => REGEX.LOWERCASE.test(password);
41
+ const hasNumber = (password) => REGEX.NUMBER.test(password);
42
+ const hasSpecialChar = (password) => REGEX.SPECIAL_CHAR.test(password);
43
+ const validatePassword = (password) => {
44
+ const errors = [];
45
+ if (!password || !hasMinLength(password)) {
46
+ errors.push(ERROR_MESSAGES.MIN_LENGTH);
47
+ }
48
+ if (!hasOnlyAllowedChars(password)) {
49
+ errors.push(ERROR_MESSAGES.INVALID_CHARS);
50
+ }
51
+ if (!hasUppercase(password)) {
52
+ errors.push(ERROR_MESSAGES.UPPERCASE);
53
+ }
54
+ if (!hasLowercase(password)) {
55
+ errors.push(ERROR_MESSAGES.LOWERCASE);
56
+ }
57
+ if (!hasNumber(password)) {
58
+ errors.push(ERROR_MESSAGES.NUMBER);
59
+ }
60
+ if (!hasSpecialChar(password)) {
61
+ errors.push(ERROR_MESSAGES.SPECIAL_CHAR);
62
+ }
63
+ return {
64
+ isValid: errors.length === 0,
65
+ errors,
66
+ };
67
+ };
68
+ exports.validatePassword = validatePassword;
69
+ const getPasswordRequirements = (password) => {
70
+ const requirements = [
71
+ {
72
+ met: hasMinLength(password),
73
+ message: `At least ${MIN_LENGTH} characters`,
74
+ },
75
+ {
76
+ met: hasUppercase(password),
77
+ message: 'One uppercase letter',
78
+ },
79
+ {
80
+ met: hasLowercase(password),
81
+ message: 'One lowercase letter',
82
+ },
83
+ {
84
+ met: hasNumber(password),
85
+ message: 'One number',
86
+ },
87
+ {
88
+ met: hasOnlyAllowedChars(password) && hasSpecialChar(password),
89
+ message: `One special character from: ${ALLOWED_SPECIAL_CHARS} (no other special characters allowed)`,
90
+ },
91
+ ];
92
+ return requirements;
93
+ };
94
+ exports.getPasswordRequirements = getPasswordRequirements;
95
+ const getPasswordErrorMessage = (password) => {
96
+ const validation = (0, exports.validatePassword)(password);
97
+ if (validation.isValid)
98
+ return '';
99
+ // Return all errors as a single message
100
+ return validation.errors.join('. ');
101
+ };
102
+ exports.getPasswordErrorMessage = getPasswordErrorMessage;
@@ -35,3 +35,4 @@ export * from './useSendResetForm';
35
35
  export * from './useSigninForm';
36
36
  export * from './useSignUpGuestForm';
37
37
  export * from './useSignUpForm';
38
+ export * from './usePasswordValidation';
@@ -35,3 +35,4 @@ export * from './useSendResetForm';
35
35
  export * from './useSigninForm';
36
36
  export * from './useSignUpGuestForm';
37
37
  export * from './useSignUpForm';
38
+ export * from './usePasswordValidation';
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Custom hook for password field validation and state management
3
+ * Follows SRP by encapsulating all password-related state and logic
4
+ */
5
+ export declare const usePasswordValidation: () => {
6
+ handlePasswordChange: () => void;
7
+ isPasswordValid: (password: string | undefined) => boolean;
8
+ shouldShowRequirements: (password: string | undefined, hidePassword?: boolean) => boolean;
9
+ getPasswordErrors: (password: string | undefined, errors: Record<string, string>, fieldName?: string) => Record<string, string>;
10
+ };
@@ -0,0 +1,40 @@
1
+ import { useState } from 'react';
2
+ import { validatePassword } from '../utils/passwordValidation';
3
+ /**
4
+ * Custom hook for password field validation and state management
5
+ * Follows SRP by encapsulating all password-related state and logic
6
+ */
7
+ export const usePasswordValidation = () => {
8
+ const [passwordTouched, setPasswordTouched] = useState(false);
9
+ const handlePasswordChange = () => {
10
+ if (!passwordTouched) {
11
+ setPasswordTouched(true);
12
+ }
13
+ };
14
+ const isPasswordValid = (password) => {
15
+ if (!password)
16
+ return false;
17
+ const validation = validatePassword(password);
18
+ return validation.isValid;
19
+ };
20
+ const shouldShowRequirements = (password, hidePassword = false) => {
21
+ return !hidePassword && !!password;
22
+ };
23
+ const getPasswordErrors = (password, errors, fieldName = 'password') => {
24
+ const passwordErrors = {};
25
+ if (errors[fieldName]) {
26
+ // If password is valid according to frontend but still has error, it's from backend
27
+ const isValidFrontend = password && validatePassword(password).isValid;
28
+ if (isValidFrontend || !password) {
29
+ passwordErrors[fieldName] = errors[fieldName];
30
+ }
31
+ }
32
+ return passwordErrors;
33
+ };
34
+ return {
35
+ handlePasswordChange,
36
+ isPasswordValid,
37
+ shouldShowRequirements,
38
+ getPasswordErrors
39
+ };
40
+ };
@@ -18,3 +18,4 @@ export * from './version';
18
18
  export * from './theme';
19
19
  export * from './favorites';
20
20
  export * from './pizzaBuilder';
21
+ export * from './passwordValidation';
@@ -19,3 +19,4 @@ export * from './version';
19
19
  export * from './theme';
20
20
  export * from './favorites';
21
21
  export * from './pizzaBuilder';
22
+ export * from './passwordValidation';
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Password validation utility matching backend password policy
3
+ *
4
+ * Requirements:
5
+ * - Minimum 10 characters
6
+ * - At least one uppercase letter
7
+ * - At least one lowercase letter
8
+ * - At least one number
9
+ * - At least one special character from allowed set: !@#$%^&_-+=<>
10
+ *
11
+ * Backend regex: ^[A-Za-z0-9\!\@\#\$\%\^\&\_\-\+\=\<\>]*$
12
+ */
13
+ export interface PasswordValidationResult {
14
+ isValid: boolean;
15
+ errors: string[];
16
+ }
17
+ export interface PasswordRequirement {
18
+ met: boolean;
19
+ message: string;
20
+ }
21
+ export declare const validatePassword: (password: string) => PasswordValidationResult;
22
+ export declare const getPasswordRequirements: (password: string) => PasswordRequirement[];
23
+ export declare const getPasswordErrorMessage: (password: string) => string;
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Password validation utility matching backend password policy
3
+ *
4
+ * Requirements:
5
+ * - Minimum 10 characters
6
+ * - At least one uppercase letter
7
+ * - At least one lowercase letter
8
+ * - At least one number
9
+ * - At least one special character from allowed set: !@#$%^&_-+=<>
10
+ *
11
+ * Backend regex: ^[A-Za-z0-9\!\@\#\$\%\^\&\_\-\+\=\<\>]*$
12
+ */
13
+ // Constants
14
+ const MIN_LENGTH = 10;
15
+ const ALLOWED_SPECIAL_CHARS = '!@#$%^&_-+=<>';
16
+ // Regex patterns - DRY principle
17
+ const REGEX = {
18
+ ALLOWED_CHARS: /^[A-Za-z0-9!@#$%^&_\-+=<>]*$/,
19
+ UPPERCASE: /[A-Z]/,
20
+ LOWERCASE: /[a-z]/,
21
+ NUMBER: /[0-9]/,
22
+ SPECIAL_CHAR: /[!@#$%^&_\-+=<>]/,
23
+ };
24
+ // Error messages - DRY principle
25
+ const ERROR_MESSAGES = {
26
+ MIN_LENGTH: `Password must be at least ${MIN_LENGTH} characters long`,
27
+ INVALID_CHARS: `Password contains invalid characters. Only letters, numbers, and (${ALLOWED_SPECIAL_CHARS}) are allowed`,
28
+ UPPERCASE: 'Password must include at least one uppercase letter',
29
+ LOWERCASE: 'Password must include at least one lowercase letter',
30
+ NUMBER: 'Password must include at least one number',
31
+ SPECIAL_CHAR: `Password must include at least one special character (${ALLOWED_SPECIAL_CHARS})`,
32
+ };
33
+ // SRP: Individual validation functions
34
+ const hasMinLength = (password) => password.length >= MIN_LENGTH;
35
+ const hasOnlyAllowedChars = (password) => REGEX.ALLOWED_CHARS.test(password);
36
+ const hasUppercase = (password) => REGEX.UPPERCASE.test(password);
37
+ const hasLowercase = (password) => REGEX.LOWERCASE.test(password);
38
+ const hasNumber = (password) => REGEX.NUMBER.test(password);
39
+ const hasSpecialChar = (password) => REGEX.SPECIAL_CHAR.test(password);
40
+ export const validatePassword = (password) => {
41
+ const errors = [];
42
+ if (!password || !hasMinLength(password)) {
43
+ errors.push(ERROR_MESSAGES.MIN_LENGTH);
44
+ }
45
+ if (!hasOnlyAllowedChars(password)) {
46
+ errors.push(ERROR_MESSAGES.INVALID_CHARS);
47
+ }
48
+ if (!hasUppercase(password)) {
49
+ errors.push(ERROR_MESSAGES.UPPERCASE);
50
+ }
51
+ if (!hasLowercase(password)) {
52
+ errors.push(ERROR_MESSAGES.LOWERCASE);
53
+ }
54
+ if (!hasNumber(password)) {
55
+ errors.push(ERROR_MESSAGES.NUMBER);
56
+ }
57
+ if (!hasSpecialChar(password)) {
58
+ errors.push(ERROR_MESSAGES.SPECIAL_CHAR);
59
+ }
60
+ return {
61
+ isValid: errors.length === 0,
62
+ errors,
63
+ };
64
+ };
65
+ export const getPasswordRequirements = (password) => {
66
+ const requirements = [
67
+ {
68
+ met: hasMinLength(password),
69
+ message: `At least ${MIN_LENGTH} characters`,
70
+ },
71
+ {
72
+ met: hasUppercase(password),
73
+ message: 'One uppercase letter',
74
+ },
75
+ {
76
+ met: hasLowercase(password),
77
+ message: 'One lowercase letter',
78
+ },
79
+ {
80
+ met: hasNumber(password),
81
+ message: 'One number',
82
+ },
83
+ {
84
+ met: hasOnlyAllowedChars(password) && hasSpecialChar(password),
85
+ message: `One special character from: ${ALLOWED_SPECIAL_CHARS} (no other special characters allowed)`,
86
+ },
87
+ ];
88
+ return requirements;
89
+ };
90
+ export const getPasswordErrorMessage = (password) => {
91
+ const validation = validatePassword(password);
92
+ if (validation.isValid)
93
+ return '';
94
+ // Return all errors as a single message
95
+ return validation.errors.join('. ');
96
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.5.59",
3
+ "version": "0.5.60",
4
4
  "description": "A library of utils for use with Open Tender applications that utilize our cloud-based Order API.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",