@open-tender/utils 0.5.58 → 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.
- package/dist/cjs/hooks/index.d.ts +1 -0
- package/dist/cjs/hooks/index.js +1 -0
- package/dist/cjs/hooks/usePasswordValidation.d.ts +10 -0
- package/dist/cjs/hooks/usePasswordValidation.js +44 -0
- package/dist/cjs/hooks/useSignUpForm.js +31 -3
- package/dist/cjs/hooks/useSignUpGuestForm.js +29 -3
- package/dist/cjs/utils/index.d.ts +1 -0
- package/dist/cjs/utils/index.js +1 -0
- package/dist/cjs/utils/passwordValidation.d.ts +23 -0
- package/dist/cjs/utils/passwordValidation.js +102 -0
- package/dist/esm/hooks/index.d.ts +1 -0
- package/dist/esm/hooks/index.js +1 -0
- package/dist/esm/hooks/usePasswordValidation.d.ts +10 -0
- package/dist/esm/hooks/usePasswordValidation.js +40 -0
- package/dist/esm/hooks/useSignUpForm.js +31 -3
- package/dist/esm/hooks/useSignUpGuestForm.js +29 -3
- package/dist/esm/utils/index.d.ts +1 -0
- package/dist/esm/utils/index.js +1 -0
- package/dist/esm/utils/passwordValidation.d.ts +23 -0
- package/dist/esm/utils/passwordValidation.js +96 -0
- package/package.json +2 -2
package/dist/cjs/hooks/index.js
CHANGED
|
@@ -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;
|
|
@@ -107,6 +107,32 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig, hidePasswo
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
}, [loading, error]);
|
|
110
|
+
const validateRequiredFields = (values) => {
|
|
111
|
+
var _a, _b, _c, _d, _e, _f;
|
|
112
|
+
const validationErrors = {};
|
|
113
|
+
// Check non-optional fields from CustomerCreate
|
|
114
|
+
if (!((_a = values.first_name) === null || _a === void 0 ? void 0 : _a.trim())) {
|
|
115
|
+
validationErrors.first_name = 'First name is required';
|
|
116
|
+
}
|
|
117
|
+
if (!((_b = values.last_name) === null || _b === void 0 ? void 0 : _b.trim())) {
|
|
118
|
+
validationErrors.last_name = 'Last name is required';
|
|
119
|
+
}
|
|
120
|
+
if (!((_c = values.email) === null || _c === void 0 ? void 0 : _c.trim())) {
|
|
121
|
+
validationErrors.email = 'Email is required';
|
|
122
|
+
}
|
|
123
|
+
if (!((_d = values.phone) === null || _d === void 0 ? void 0 : _d.trim())) {
|
|
124
|
+
validationErrors.phone = 'Phone is required';
|
|
125
|
+
}
|
|
126
|
+
// Check password if not hidden
|
|
127
|
+
if (!((_e = values.password) === null || _e === void 0 ? void 0 : _e.trim())) {
|
|
128
|
+
validationErrors.password = 'Password is required';
|
|
129
|
+
}
|
|
130
|
+
// Check company if required
|
|
131
|
+
if (companyRequired && !((_f = values.company) === null || _f === void 0 ? void 0 : _f.trim())) {
|
|
132
|
+
validationErrors.company = 'Company is required';
|
|
133
|
+
}
|
|
134
|
+
return validationErrors;
|
|
135
|
+
};
|
|
110
136
|
const handleChange = (name, value) => {
|
|
111
137
|
const inputValue = name === 'phone'
|
|
112
138
|
? (0, utils_1.makePhone)((value !== null && value !== void 0 ? value : ''))
|
|
@@ -128,10 +154,12 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig, hidePasswo
|
|
|
128
154
|
if (values.birth_date) {
|
|
129
155
|
values.birth_date = (0, utils_1.slashesToDashes)(values.birth_date);
|
|
130
156
|
}
|
|
157
|
+
const validationErrors = validateRequiredFields(values);
|
|
131
158
|
if (require_home_store && !data.home_store) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
159
|
+
validationErrors.home_store = 'Please select a home store';
|
|
160
|
+
}
|
|
161
|
+
if (Object.keys(validationErrors).length > 0) {
|
|
162
|
+
setErrors(validationErrors);
|
|
135
163
|
return;
|
|
136
164
|
}
|
|
137
165
|
const loginToken = includeLoginRecaptcha
|
|
@@ -78,6 +78,28 @@ const useSignUpGuestForm = (email, guest, loading, error, guestErrors, signUp, s
|
|
|
78
78
|
: value;
|
|
79
79
|
setData(Object.assign(Object.assign({}, data), { [name]: val }));
|
|
80
80
|
};
|
|
81
|
+
const validateRequiredFields = (values) => {
|
|
82
|
+
var _a, _b, _c, _d, _e;
|
|
83
|
+
const validationErrors = {};
|
|
84
|
+
// Check non-optional fields from CustomerCreate
|
|
85
|
+
if (!((_a = values.first_name) === null || _a === void 0 ? void 0 : _a.trim())) {
|
|
86
|
+
validationErrors.first_name = 'First name is required';
|
|
87
|
+
}
|
|
88
|
+
if (!((_b = values.last_name) === null || _b === void 0 ? void 0 : _b.trim())) {
|
|
89
|
+
validationErrors.last_name = 'Last name is required';
|
|
90
|
+
}
|
|
91
|
+
if (!((_c = values.email) === null || _c === void 0 ? void 0 : _c.trim())) {
|
|
92
|
+
validationErrors.email = 'Email is required';
|
|
93
|
+
}
|
|
94
|
+
if (!((_d = values.phone) === null || _d === void 0 ? void 0 : _d.trim())) {
|
|
95
|
+
validationErrors.phone = 'Phone is required';
|
|
96
|
+
}
|
|
97
|
+
// Check password if not Thanx (password is removed for Thanx)
|
|
98
|
+
if (!((_e = values.password) === null || _e === void 0 ? void 0 : _e.trim())) {
|
|
99
|
+
validationErrors.password = 'Password is required';
|
|
100
|
+
}
|
|
101
|
+
return validationErrors;
|
|
102
|
+
};
|
|
81
103
|
const handleSubmit = (evt) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
82
104
|
var _a, _b, _c;
|
|
83
105
|
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
@@ -87,10 +109,14 @@ const useSignUpGuestForm = (email, guest, loading, error, guestErrors, signUp, s
|
|
|
87
109
|
if (values.birth_date) {
|
|
88
110
|
values.birth_date = (0, utils_1.slashesToDashes)(values.birth_date);
|
|
89
111
|
}
|
|
112
|
+
// Validate required fields
|
|
113
|
+
const validationErrors = validateRequiredFields(values);
|
|
90
114
|
if (require_home_store && !data.home_store) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
115
|
+
validationErrors.home_store = 'Please select a home store';
|
|
116
|
+
}
|
|
117
|
+
// If there are validation errors, set them and return early
|
|
118
|
+
if (Object.keys(validationErrors).length > 0) {
|
|
119
|
+
setErrors(validationErrors);
|
|
94
120
|
return;
|
|
95
121
|
}
|
|
96
122
|
const loginToken = includeLoginRecaptcha
|
package/dist/cjs/utils/index.js
CHANGED
|
@@ -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;
|
package/dist/esm/hooks/index.js
CHANGED
|
@@ -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
|
+
};
|
|
@@ -104,6 +104,32 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig, hid
|
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
106
|
}, [loading, error]);
|
|
107
|
+
const validateRequiredFields = (values) => {
|
|
108
|
+
var _a, _b, _c, _d, _e, _f;
|
|
109
|
+
const validationErrors = {};
|
|
110
|
+
// Check non-optional fields from CustomerCreate
|
|
111
|
+
if (!((_a = values.first_name) === null || _a === void 0 ? void 0 : _a.trim())) {
|
|
112
|
+
validationErrors.first_name = 'First name is required';
|
|
113
|
+
}
|
|
114
|
+
if (!((_b = values.last_name) === null || _b === void 0 ? void 0 : _b.trim())) {
|
|
115
|
+
validationErrors.last_name = 'Last name is required';
|
|
116
|
+
}
|
|
117
|
+
if (!((_c = values.email) === null || _c === void 0 ? void 0 : _c.trim())) {
|
|
118
|
+
validationErrors.email = 'Email is required';
|
|
119
|
+
}
|
|
120
|
+
if (!((_d = values.phone) === null || _d === void 0 ? void 0 : _d.trim())) {
|
|
121
|
+
validationErrors.phone = 'Phone is required';
|
|
122
|
+
}
|
|
123
|
+
// Check password if not hidden
|
|
124
|
+
if (!((_e = values.password) === null || _e === void 0 ? void 0 : _e.trim())) {
|
|
125
|
+
validationErrors.password = 'Password is required';
|
|
126
|
+
}
|
|
127
|
+
// Check company if required
|
|
128
|
+
if (companyRequired && !((_f = values.company) === null || _f === void 0 ? void 0 : _f.trim())) {
|
|
129
|
+
validationErrors.company = 'Company is required';
|
|
130
|
+
}
|
|
131
|
+
return validationErrors;
|
|
132
|
+
};
|
|
107
133
|
const handleChange = (name, value) => {
|
|
108
134
|
const inputValue = name === 'phone'
|
|
109
135
|
? makePhone((value !== null && value !== void 0 ? value : ''))
|
|
@@ -125,10 +151,12 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig, hid
|
|
|
125
151
|
if (values.birth_date) {
|
|
126
152
|
values.birth_date = slashesToDashes(values.birth_date);
|
|
127
153
|
}
|
|
154
|
+
const validationErrors = validateRequiredFields(values);
|
|
128
155
|
if (require_home_store && !data.home_store) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
156
|
+
validationErrors.home_store = 'Please select a home store';
|
|
157
|
+
}
|
|
158
|
+
if (Object.keys(validationErrors).length > 0) {
|
|
159
|
+
setErrors(validationErrors);
|
|
132
160
|
return;
|
|
133
161
|
}
|
|
134
162
|
const loginToken = includeLoginRecaptcha
|
|
@@ -75,6 +75,28 @@ export const useSignUpGuestForm = (email, guest, loading, error, guestErrors, si
|
|
|
75
75
|
: value;
|
|
76
76
|
setData(Object.assign(Object.assign({}, data), { [name]: val }));
|
|
77
77
|
};
|
|
78
|
+
const validateRequiredFields = (values) => {
|
|
79
|
+
var _a, _b, _c, _d, _e;
|
|
80
|
+
const validationErrors = {};
|
|
81
|
+
// Check non-optional fields from CustomerCreate
|
|
82
|
+
if (!((_a = values.first_name) === null || _a === void 0 ? void 0 : _a.trim())) {
|
|
83
|
+
validationErrors.first_name = 'First name is required';
|
|
84
|
+
}
|
|
85
|
+
if (!((_b = values.last_name) === null || _b === void 0 ? void 0 : _b.trim())) {
|
|
86
|
+
validationErrors.last_name = 'Last name is required';
|
|
87
|
+
}
|
|
88
|
+
if (!((_c = values.email) === null || _c === void 0 ? void 0 : _c.trim())) {
|
|
89
|
+
validationErrors.email = 'Email is required';
|
|
90
|
+
}
|
|
91
|
+
if (!((_d = values.phone) === null || _d === void 0 ? void 0 : _d.trim())) {
|
|
92
|
+
validationErrors.phone = 'Phone is required';
|
|
93
|
+
}
|
|
94
|
+
// Check password if not Thanx (password is removed for Thanx)
|
|
95
|
+
if (!((_e = values.password) === null || _e === void 0 ? void 0 : _e.trim())) {
|
|
96
|
+
validationErrors.password = 'Password is required';
|
|
97
|
+
}
|
|
98
|
+
return validationErrors;
|
|
99
|
+
};
|
|
78
100
|
const handleSubmit = (evt) => __awaiter(void 0, void 0, void 0, function* () {
|
|
79
101
|
var _a, _b, _c;
|
|
80
102
|
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
@@ -84,10 +106,14 @@ export const useSignUpGuestForm = (email, guest, loading, error, guestErrors, si
|
|
|
84
106
|
if (values.birth_date) {
|
|
85
107
|
values.birth_date = slashesToDashes(values.birth_date);
|
|
86
108
|
}
|
|
109
|
+
// Validate required fields
|
|
110
|
+
const validationErrors = validateRequiredFields(values);
|
|
87
111
|
if (require_home_store && !data.home_store) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
112
|
+
validationErrors.home_store = 'Please select a home store';
|
|
113
|
+
}
|
|
114
|
+
// If there are validation errors, set them and return early
|
|
115
|
+
if (Object.keys(validationErrors).length > 0) {
|
|
116
|
+
setErrors(validationErrors);
|
|
91
117
|
return;
|
|
92
118
|
}
|
|
93
119
|
const loginToken = includeLoginRecaptcha
|
package/dist/esm/utils/index.js
CHANGED
|
@@ -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.
|
|
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",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"typescript": "^4.8.2"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"@open-tender/types": "^0.4.
|
|
60
|
+
"@open-tender/types": "^0.4.172",
|
|
61
61
|
"@reduxjs/toolkit": "^1.8.5",
|
|
62
62
|
"react": "^18.2.0"
|
|
63
63
|
},
|