Skip to content

[FIX] Remove hardcoded signUpRequestDto validation fallback in GlobalExceptionHandler #217

Closed

Description

@aniebietafia

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 from GlobalExceptionHandler.java.
  • PasswordMatchesValidator disables the default constraint violation and dynamically registers the constraint violation against the path defined by the confirmPasswordField configuration property.
  • Validation errors for mismatched password inputs dynamically map to the field (e.g., confirmPassword or confirmNewPassword), 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.java to override default error binding.
  • Remove the ternary object name check inside GlobalExceptionHandler.java line 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

  1. added
    bug
    enhancement
    on Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    apibugcienhancementquestionsecurity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions