-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from gdesiato/validators-branch
added user and authentication dto validators
- Loading branch information
Showing
13 changed files
with
325 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/desiato/puresynth/exceptions/ErrorMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.desiato.puresynth.exceptions; | ||
|
||
public record ErrorMessage(String message) { | ||
public ErrorMessage { | ||
if (message == null || message.isBlank()) { | ||
throw new IllegalArgumentException("Error message cannot be null or blank"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/desiato/puresynth/exceptions/ValidationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.desiato.puresynth.exceptions; | ||
|
||
|
||
import java.util.List; | ||
|
||
public class ValidationException extends RuntimeException { | ||
|
||
private final List<ErrorMessage> errorMessages; | ||
|
||
public ValidationException(List<ErrorMessage> errorMessages) { | ||
super("Validation failed"); | ||
this.errorMessages = errorMessages; | ||
} | ||
|
||
public List<ErrorMessage> getErrorMessages() { | ||
return errorMessages; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/desiato/puresynth/validators/AbstractValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.desiato.puresynth.validators; | ||
|
||
import com.desiato.puresynth.exceptions.ErrorMessage; | ||
import com.desiato.puresynth.exceptions.ValidationException; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public abstract class AbstractValidator<T> { | ||
|
||
protected abstract void validate(T request, List<ErrorMessage> errorMessages); | ||
|
||
public void validate(T request) { | ||
List<ErrorMessage> errorMessages = new ArrayList<>(); | ||
validate(request, errorMessages); | ||
|
||
if (!errorMessages.isEmpty()) { | ||
throw new ValidationException(errorMessages); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/desiato/puresynth/validators/AuthenticationRequestValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.desiato.puresynth.validators; | ||
|
||
import com.desiato.puresynth.dtos.AuthenticationRequestDTO; | ||
import com.desiato.puresynth.exceptions.ErrorMessage; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
public class AuthenticationRequestValidator extends AbstractValidator<AuthenticationRequestDTO> { | ||
|
||
@Override | ||
protected void validate(AuthenticationRequestDTO request, List<ErrorMessage> errorMessages) { | ||
|
||
if (request.email() == null || request.email().isBlank()) { | ||
errorMessages.add(new ErrorMessage("Email cannot be blank.")); | ||
} else if (!request.email().contains("@")) { | ||
errorMessages.add(new ErrorMessage("Invalid email format.")); | ||
} | ||
|
||
if (request.password() == null || request.password().isBlank()) { | ||
errorMessages.add(new ErrorMessage("Password cannot be blank.")); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/desiato/puresynth/validators/UserRequestValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.desiato.puresynth.validators; | ||
|
||
import com.desiato.puresynth.dtos.UserRequestDTO; | ||
import com.desiato.puresynth.exceptions.ErrorMessage; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
@Component | ||
public class UserRequestValidator extends AbstractValidator<UserRequestDTO> { | ||
|
||
@Override | ||
protected void validate(UserRequestDTO userRequestDTO, List<ErrorMessage> errorMessages) { | ||
|
||
if (userRequestDTO.email() != null) { | ||
if (userRequestDTO.email().isBlank()) { | ||
errorMessages.add(new ErrorMessage("Email cannot be blank.")); | ||
} else if (!userRequestDTO.email().contains("@")) { | ||
errorMessages.add(new ErrorMessage("Invalid email format.")); | ||
} | ||
} else { | ||
errorMessages.add(new ErrorMessage("Email cannot be null.")); | ||
} | ||
|
||
if (userRequestDTO.password() != null && !userRequestDTO.password().isBlank()) { | ||
log.info("Password is valid"); | ||
} else if (userRequestDTO.password() == null) { | ||
errorMessages.add(new ErrorMessage("Password cannot be null.")); | ||
} else { | ||
errorMessages.add(new ErrorMessage("Password cannot be blank.")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.