Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Update Validator and ToBeValidated #57

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sep490-idp/src/main/java/sep490/idp/dto/SignupDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import sep490.idp.validation.ToValidated;
import sep490.idp.validation.ToBeValidated;

import static sep490.common.api.utils.CommonConstant.*;

@Getter
@Setter
public class SignupDTO implements ToValidated {
public class SignupDTO extends ToBeValidated {

@Pattern(regexp = EMAIL_PATTERN, message = "{validation.email.invalid}")
@Size(min = 1, max = 255, message = "{validation.email.length}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ private SignupResult validateSignupDTO(SignupDTO signupDTO) {
result.setRedirectUrl("redirect:/login");
result.setSuccess(true);

validator.getValidateFirstMessage(signupDTO).ifPresent(msg -> {
validator.validate(signupDTO);
if (!signupDTO.isValidated()) {
result.setSuccess(false);
result.setErrorMessage(msg);
result.setErrorMessage(signupDTO.getFirstErrorMsg().orElse(null));
result.setRedirectUrl("signup");
});
}
thongdanghoang marked this conversation as resolved.
Show resolved Hide resolved

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sep490.idp.dto.SignupDTO;
import sep490.idp.repository.UserRepository;

import java.util.Optional;

@Component
@Qualifier("signupValidator")
Expand All @@ -18,15 +17,20 @@ public class SignupValidator implements Validator<SignupDTO> {


@Override
public Optional<String> getValidateFirstMessage(SignupDTO signupDTO) {
if (!passwordsMatch(signupDTO)) {
return Optional.of("validation.password.invalid.notmatch");
public void validate(SignupDTO dto) {
if (!passwordsMatch(dto)) {
dto.addError("password", "validation.password.invalid.notmatch");
}
if (userRepo.existsByEmail(signupDTO.getEmail())) {
return Optional.of("validation.email.invalid.exist");
if (userRepo.existsByEmail(dto.getEmail())) {
dto.addError("password", "validation.email.invalid.exist");
}
checkValidated(dto);
}
GiaBaorr marked this conversation as resolved.
Show resolved Hide resolved

return Optional.empty();
private void checkValidated(SignupDTO dto) {
if (!dto.getErrorMap().isEmpty()) {
dto.setValidated(false);
}
GiaBaorr marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean passwordsMatch(SignupDTO signupDTO) {
Expand Down
25 changes: 25 additions & 0 deletions sep490-idp/src/main/java/sep490/idp/validation/ToBeValidated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sep490.idp.validation;

import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Getter
@Setter
public abstract class ToBeValidated {
GiaBaorr marked this conversation as resolved.
Show resolved Hide resolved

private boolean validated = false;
private Map<String, String> errorMap = new HashMap<>();
private String errorMsg;

public void addError(String key, String errorMsg) {
this.errorMap.put(key, errorMsg);
}

public Optional<String> getFirstErrorMsg() {
return Optional.ofNullable(this.getErrorMap().entrySet().iterator().next().getValue());
}
GiaBaorr marked this conversation as resolved.
Show resolved Hide resolved
}

This file was deleted.

8 changes: 3 additions & 5 deletions sep490-idp/src/main/java/sep490/idp/validation/Validator.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package sep490.idp.validation;

import java.util.Optional;

public interface Validator<T extends ToValidated> {
public interface Validator<T extends ToBeValidated> {


/**
* Retrieves the first validation message for the given toValidate object.
* Validate the whole object and set value to flag and map
*
* @param toValidate the object to be validated
* @return the first validation message as a string
*/
Optional<String> getValidateFirstMessage(T toValidate);
void validate(T toValidate);
GiaBaorr marked this conversation as resolved.
Show resolved Hide resolved
}
Loading