2021-04-12 13:32:25 +00:00
|
|
|
import { size } from "lodash";
|
|
|
|
|
import validateEquality from "components/forms/validators/validate_equality";
|
|
|
|
|
import validEmail from "components/forms/validators/valid_email";
|
|
|
|
|
import validPassword from "components/forms/validators/valid_password";
|
2016-11-16 16:58:25 +00:00
|
|
|
|
|
|
|
|
const validate = (formData) => {
|
|
|
|
|
const errors = {};
|
|
|
|
|
const {
|
|
|
|
|
email,
|
|
|
|
|
password,
|
|
|
|
|
password_confirmation: passwordConfirmation,
|
2021-06-24 20:42:29 +00:00
|
|
|
name,
|
2016-11-16 16:58:25 +00:00
|
|
|
} = formData;
|
|
|
|
|
|
|
|
|
|
if (!email) {
|
2021-04-12 13:32:25 +00:00
|
|
|
errors.email = "Email must be present";
|
2024-04-25 17:03:30 +00:00
|
|
|
} else if (!validEmail(email)) {
|
|
|
|
|
errors.email = "Email must be a valid email";
|
2016-11-16 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
2021-06-24 20:42:29 +00:00
|
|
|
if (!name) {
|
|
|
|
|
errors.name = "Full name must be present";
|
2016-11-16 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-26 21:59:05 +00:00
|
|
|
const { isValid, error } = validPassword(password);
|
|
|
|
|
if (password && passwordConfirmation && !isValid) {
|
|
|
|
|
errors.password = error;
|
2017-02-07 14:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
if (
|
|
|
|
|
password &&
|
|
|
|
|
passwordConfirmation &&
|
|
|
|
|
!validateEquality(password, passwordConfirmation)
|
|
|
|
|
) {
|
|
|
|
|
errors.password_confirmation =
|
|
|
|
|
"Password confirmation does not match password";
|
2016-11-16 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!password) {
|
2021-04-12 13:32:25 +00:00
|
|
|
errors.password = "Password must be present";
|
2016-11-16 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!passwordConfirmation) {
|
2021-04-12 13:32:25 +00:00
|
|
|
errors.password_confirmation = "Password confirmation must be present";
|
2016-11-16 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const valid = !size(errors);
|
|
|
|
|
|
|
|
|
|
return { valid, errors };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default { validate };
|