-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AYS-421 |
@Password
Validation Annotation Has Been Created and Anno…
…tation Has Been Used for Length Limitation (#371)
- Loading branch information
1 parent
74acff1
commit 7b2d2c8
Showing
21 changed files
with
313 additions
and
132 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
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
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
40 changes: 40 additions & 0 deletions
40
src/main/java/org/ays/common/util/validation/Password.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,40 @@ | ||
package org.ays.common.util.validation; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation to validate password using {@link PasswordValidator}. | ||
*/ | ||
@Target(value = ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = PasswordValidator.class) | ||
public @interface Password { | ||
|
||
/** | ||
* Returns the error message when password does not valid. | ||
* | ||
* @return the error message | ||
*/ | ||
String message() default "must be valid"; | ||
|
||
/** | ||
* Returns the validation groups to which this constraint belongs. | ||
* | ||
* @return the validation groups | ||
*/ | ||
Class<?>[] groups() default {}; | ||
|
||
/** | ||
* Returns the payload associated to this constraint. | ||
* | ||
* @return the payload | ||
*/ | ||
Class<? extends Payload>[] payload() default {}; | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/org/ays/common/util/validation/PasswordValidator.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,36 @@ | ||
package org.ays.common.util.validation; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* Validator for password fields. This validator ensures that a password meets the minimum length requirement. | ||
*/ | ||
class PasswordValidator implements ConstraintValidator<Password, String> { | ||
|
||
/** | ||
* Validates the provided password based on predefined rules. | ||
* | ||
* @param password the password to validate. | ||
* @param constraintValidatorContext context in which the constraint is evaluated. | ||
* @return {@code true} if the password is valid, {@code false} otherwise. | ||
*/ | ||
@Override | ||
public boolean isValid(String password, ConstraintValidatorContext constraintValidatorContext) { | ||
|
||
if (!StringUtils.hasText(password)) { | ||
return true; | ||
} | ||
|
||
if (password.length() < 6) { | ||
constraintValidatorContext.disableDefaultConstraintViolation(); | ||
constraintValidatorContext.buildConstraintViolationWithTemplate("size must be at least 6 characters") | ||
.addConstraintViolation(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} |
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.