-
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.
[SERVER] implement LicensePlateValidator to validate licensePlate
- Loading branch information
Huynh Thanh Binh
committed
Aug 3, 2020
1 parent
4bc5bcb
commit 9bc9bcb
Showing
4 changed files
with
57 additions
and
4 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
28 changes: 28 additions & 0 deletions
28
...rc/main/java/com/bht/saigonparking/service/booking/annotation/LicensePlateValidation.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,28 @@ | ||
package com.bht.saigonparking.service.booking.annotation; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
/** | ||
* | ||
* @author bht | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = LicensePlateValidator.class) | ||
@Target({FIELD, METHOD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER}) | ||
public @interface LicensePlateValidation { | ||
|
||
String message() default "Number license plate is in wrong format !"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
18 changes: 18 additions & 0 deletions
18
...src/main/java/com/bht/saigonparking/service/booking/annotation/LicensePlateValidator.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.bht.saigonparking.service.booking.annotation; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
/** | ||
* | ||
* @author bht | ||
*/ | ||
public final class LicensePlateValidator implements ConstraintValidator<LicensePlateValidation, String> { | ||
|
||
@Override | ||
public boolean isValid(String numberLicensePlate, ConstraintValidatorContext constraintValidatorContext) { | ||
return numberLicensePlate | ||
.replaceAll("\\s+|\\.+", "") // remove all space and dot characters exist in string | ||
.matches("^[0-9]{1,2}[A-Za-z][0-9]?-[0-9]{4,5}$"); // example: 59H1-76217, 54L6-2908, 51B-1234, 86B-56789 | ||
} | ||
} |
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