-
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-550 |
NoSpacesAround
Annotation Has Been Created to Check Leadi…
…ng and Trailing Spaces (#404)
- Loading branch information
1 parent
b146de0
commit 83312d5
Showing
8 changed files
with
124 additions
and
30 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
39 changes: 39 additions & 0 deletions
39
src/main/java/org/ays/common/util/validation/NoSpacesAround.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,39 @@ | ||
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 a text using {@link NoSpacesAroundValidator}. | ||
*/ | ||
@Target(value = ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = NoSpacesAroundValidator.class) | ||
public @interface NoSpacesAround { | ||
|
||
/** | ||
* Returns the error message when the text is not valid. | ||
* | ||
* @return the error message | ||
*/ | ||
String message() default "cannot start or end with space"; | ||
|
||
/** | ||
* 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 {}; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/ays/common/util/validation/NoSpacesAroundValidator.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,24 @@ | ||
package org.ays.common.util.validation; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* A custom validator implementation for the {@link NoSpacesAround} annotation. | ||
* Validates whether the provided text does not have | ||
* trailing or leading spaces. | ||
*/ | ||
class NoSpacesAroundValidator implements ConstraintValidator<NoSpacesAround, String> { | ||
|
||
@Override | ||
public boolean isValid(String text, ConstraintValidatorContext constraintValidatorContext) { | ||
|
||
if (!StringUtils.hasText(text)) { | ||
return true; | ||
} | ||
|
||
return !(text.startsWith(" ") || text.endsWith(" ")); | ||
} | ||
|
||
} |
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