Merge branch 'dev' into NCBACK-13

This commit is contained in:
vladp
2025-12-18 17:43:35 +07:00
8 changed files with 103 additions and 92 deletions
@@ -33,13 +33,15 @@ public class RegRequestHandler implements RequestHandler {
public BaseResponse handle(BaseRequest request) throws ValidationException {
RegRequest regRequest = objectMapper.convertValue(request.getMessageBody(), RegRequest.class);
if (userRepository.existsByEmail(regRequest.getEmail())) {
if (userRepository.existsByEmail(regRequest.getEmail()) || userRepository.existsByPhone(regRequest.getPhone())) {
RegAnswer regAnswer = new RegAnswer();
regAnswer.setFieldErrors(Arrays.asList(Map.of("email", regRequest.getEmail())));
regAnswer.setFieldErrors(Arrays.asList(Map.of("phone", regRequest.getPhone())));
throw new NotValidFieldException("User already exists with email: " + regRequest.getEmail(),
new BaseResponse(request.getMsgId(), MessageCode.REG_EMAIL_EXISTS.getCode(),
MessageCode.REG_EMAIL_EXISTS.getDescription(), regAnswer));
throw new NotValidFieldException("User already exists with email:" + regRequest.getEmail() + " or phone: " +
regRequest.getPhone(), new BaseResponse(request.getMsgId(),
MessageCode.REG_EMAIL_OR_PHONE_EXISTS.getCode(),
MessageCode.REG_EMAIL_OR_PHONE_EXISTS.getDescription(), regAnswer));
}
BindingResult bindingResult = new BeanPropertyBindingResult(regRequest, "regRequest");
@@ -1,10 +1,14 @@
package ru.soune.nocopy.handler.validator;
import org.apache.commons.validator.routines.DomainValidator;
import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import ru.soune.nocopy.dto.RegRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;
@Component
@@ -52,27 +56,6 @@ public class RegRequestValidator implements Validator {
errors.rejectValue("fullName", "fullName.invalid.chars",
"Name can only contain letters, spaces, hyphens and apostrophes");
}
String[] nameParts = trimmedName.split("\\s+");
if (nameParts.length < 2) {
errors.rejectValue("fullName", "fullName.missing.parts",
"Please enter both first and last name");
}
for (int i = 0; i < nameParts.length; i++) {
String part = nameParts[i];
if (part.length() < 2 && part.length() > 0) {
errors.rejectValue("fullName", "fullName.part.too.short",
"Each name part must be at least 2 characters");
break;
}
if (!Character.isUpperCase(part.charAt(0))) {
errors.rejectValue("fullName", "fullName.capitalization",
"Each name part should start with capital letter");
break;
}
}
}
private void validateCompanyName(String companyName, Errors errors) {
@@ -110,64 +93,24 @@ public class RegRequestValidator implements Validator {
private void validateEmail(String email, Errors errors) {
if (email == null || email.trim().isEmpty()) {
errors.rejectValue("email", "email.is.empty",
"Email must not be empty");
errors.rejectValue("email", "email.is.empty", "Email must not be empty");
return;
}
String trimmedEmail = email.trim().toLowerCase();
if (trimmedEmail.length() > 128) {
errors.rejectValue("email", "email.too.long",
"Email must be less than 128 characters");
if (trimmedEmail.length() > 254) {
errors.rejectValue("email", "email.too.long", "Email is too long");
return;
}
if (!trimmedEmail.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$")) {
errors.rejectValue("email", "email.invalid.format",
"Invalid email format");
EmailValidator emailValidator = EmailValidator.getInstance(true, true);
if (!emailValidator.isValid(trimmedEmail)) {
errors.rejectValue("email", "email.invalid", "Invalid email address");
return;
}
String[] parts = trimmedEmail.split("@");
if (parts.length != 2) {
return;
}
String localPart = parts[0];
String domain = parts[1];
if (localPart.length() > 64) {
errors.rejectValue("email", "email.local.too.long",
"Email username too long");
}
if (!localPart.matches("^[a-z0-9+_.-]+$")) {
errors.rejectValue("email", "email.local.invalid.chars",
"Email contains invalid characters");
}
validateEmailDomain(domain, errors);
}
private void validateEmailDomain(String domain, Errors errors) {
if (domain.length() > 255) {
errors.rejectValue("email", "email.domain.too.long",
"Email domain too long");
}
String[] domainParts = domain.split("\\.");
if (domainParts.length < 2) {
errors.rejectValue("email", "email.domain.invalid",
"Invalid domain name");
}
String tld = domainParts[domainParts.length - 1];
if (tld.length() < 2) {
errors.rejectValue("email", "email.tld.invalid",
"Invalid domain extension");
}
}
private void validatePhone(String phone, Errors errors) {
if (phone == null || phone.trim().isEmpty()) {