Repository navigation
Description
Problem
In GlobalExceptionHandler.java lines 38-40, the validation error parser contains a hardcoded fallback check specifically for the signUpRequestDto class name:
// Handle class-level constraints like @PasswordMatches
fieldName = error.getObjectName().equals("signUpRequestDto") ? "confirmPassword"
: error.getObjectName();This is a code smell that couples the global handler to a specific DTO name. If other DTOs use @PasswordMatches (such as ResetPasswordDto.java or ChangePasswordDto.java) or implement custom class-level constraints, they will fail to map the validation error to their specific matching fields (like "confirmNewPassword"). Instead, the error will bind to the entire object, causing UI form integrations to break or miss-display validation errors.
Proposed Solution
Modify the class-level validator PasswordMatchesValidator.java to dynamically attach the validation constraint violation directly to the target field node (e.g. confirmPasswordField) inside the isValid method. This converts class-level errors to standard FieldError instances automatically, allowing GlobalExceptionHandler to parse them without hardcoded DTO checks.
User Stories
- As a developer, I want validation rules to be self-contained so that I don't need to modify a global exception handler class when introducing class-level constraint annotations.
Acceptance Criteria
- The hardcoded string
"signUpRequestDto"is completely removed fromGlobalExceptionHandler.java. -
PasswordMatchesValidatordisables the default constraint violation and dynamically registers the constraint violation against the path defined by theconfirmPasswordFieldconfiguration property. - Validation errors for mismatched password inputs dynamically map to the field (e.g.,
confirmPasswordorconfirmNewPassword), and are successfully returned inside the validation sub-error payload.
Proposed Technical Details
Update the isValid method in PasswordMatchesValidator.java:
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
try {
Object password = PropertyAccessorFactory.forBeanPropertyAccess(value)
.getPropertyValue(passwordField);
Object confirmPassword = PropertyAccessorFactory.forBeanPropertyAccess(value)
.getPropertyValue(confirmPasswordField);
if (password == null || confirmPassword == null) {
return false;
}
boolean isValid = password.toString().trim().equals(confirmPassword.toString().trim());
if (!isValid) {
// Disable default class-level error mapping
context.disableDefaultConstraintViolation();
// Bind error to the specific field name dynamically
context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate())
.addPropertyNode(confirmPasswordField)
.addConstraintViolation();
}
return isValid;
} catch (Exception e) {
return false;
}
}Tasks
- Refactor
PasswordMatchesValidator.javato override default error binding. - Remove the ternary object name check inside
GlobalExceptionHandler.javaline 38. - Verify that invalid inputs for password fields in Signup, Change Password, and Reset Password endpoints return error objects with target fields correctly populated.
Activity