2021-04-12 13:32:25 +00:00
|
|
|
import { size } from "lodash";
|
|
|
|
|
import validateEquality from "components/forms/validators/validate_equality";
|
|
|
|
|
import validatePresence from "components/forms/validators/validate_presence";
|
|
|
|
|
import validPassword from "components/forms/validators/valid_password";
|
2016-11-21 15:43:08 +00:00
|
|
|
|
|
|
|
|
const validate = (formData) => {
|
|
|
|
|
const errors = {};
|
|
|
|
|
const {
|
|
|
|
|
new_password: newPassword,
|
|
|
|
|
new_password_confirmation: newPasswordConfirmation,
|
|
|
|
|
} = formData;
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
const noMatch =
|
|
|
|
|
newPassword &&
|
2017-02-07 14:29:48 +00:00
|
|
|
newPasswordConfirmation &&
|
|
|
|
|
!validateEquality(newPassword, newPasswordConfirmation);
|
|
|
|
|
|
|
|
|
|
if (!validPassword(newPassword)) {
|
2021-06-15 13:51:04 +00:00
|
|
|
errors.new_password = "Password must meet the criteria below";
|
2017-02-07 14:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-21 15:43:08 +00:00
|
|
|
if (!validatePresence(newPasswordConfirmation)) {
|
2021-04-12 13:32:25 +00:00
|
|
|
errors.new_password_confirmation =
|
|
|
|
|
"New password confirmation field must be completed";
|
2016-11-21 15:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!validatePresence(newPassword)) {
|
2021-04-12 13:32:25 +00:00
|
|
|
errors.new_password = "New password field must be completed";
|
2016-11-21 15:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
2017-02-07 14:29:48 +00:00
|
|
|
if (noMatch) {
|
2021-06-15 13:51:04 +00:00
|
|
|
errors.new_password_confirmation = "Passwords do not match";
|
2016-11-21 15:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const valid = !size(errors);
|
|
|
|
|
|
|
|
|
|
return { valid, errors };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default validate;
|