-
Notifications
You must be signed in to change notification settings - Fork 209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Password validation on account #1282
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,6 +8,31 @@ const buildErrors = (prev, id, error) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
return updatedErrors; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Processes Joi validation errors and returns a filtered object of error messages for fields that have been touched. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @param {Object} validation - The Joi validation result object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @param {Object} validation.error - The error property of the validation result containing details of validation failures. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @param {Object[]} validation.error.details - An array of error details from the Joi validation. Each item contains information about the path and the message. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @param {Object} touchedErrors - An object representing which fields have been interacted with. Keys are field IDs (field names), and values are booleans indicating whether the field has been touched. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @returns {Object} - An object where keys are the field IDs (if they exist in `touchedErrors` and are in the error details) and values are their corresponding error messages. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const getTouchedFieldErrors = (validation, touchedErrors) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nicely done, really appreciate the JSdocs |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let newErrors = {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (validation?.error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
newErrors = validation.error.details.reduce((errors, detail) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const fieldId = detail.path[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (touchedErrors[fieldId] && !(fieldId in errors)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
errors[fieldId] = detail.message; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return errors; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, {}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return newErrors; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+20
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Yo dawg, let's make this function more robust! 🍝 The function needs some defensive programming to handle edge cases and improve efficiency. Here's a more robust implementation: const getTouchedFieldErrors = (validation, touchedErrors) => {
+ if (!validation?.error?.details || !touchedErrors) {
+ return {};
+ }
- let newErrors = {};
-
- if (validation?.error) {
- newErrors = validation.error.details.reduce((errors, detail) => {
- const fieldId = detail.path[0];
- if (touchedErrors[fieldId] && !(fieldId in errors)) {
- errors[fieldId] = detail.message;
- }
- return errors;
- }, {});
- }
-
- return newErrors;
+ return validation.error.details.reduce((errors, detail) => {
+ const fieldId = detail.path?.[0];
+ if (fieldId && touchedErrors[fieldId] && !(fieldId in errors)) {
+ errors[fieldId] = detail.message;
+ }
+ return errors;
+ }, {});
}; 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const hasValidationErrors = (form, validation, setErrors) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { error } = validation.validate(form, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
abortEarly: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -53,4 +78,4 @@ const hasValidationErrors = (form, validation, setErrors) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export { buildErrors, hasValidationErrors }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export { buildErrors, hasValidationErrors, getTouchedFieldErrors }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Fix the password confirmation validation! 🍝
The validation context is not properly handling the password confirmation match. The current implementation passes the new password as context, but it should also validate against the confirm field.
This change requires updating the Joi validation schema to check if
confirm
matchesnewPassword
when both fields are touched.