-
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.
Updated to allow case-insensitive searches
- Loading branch information
Showing
8 changed files
with
78 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,6 @@ | |
} | ||
|
||
String[] value() default {}; | ||
|
||
boolean caseSensitive() default 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
17 changes: 13 additions & 4 deletions
17
src/main/java/us/cltnc/validators/constraints/impl/OneOfValidator.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 |
---|---|---|
@@ -1,20 +1,29 @@ | ||
package us.cltnc.validators.constraints.impl; | ||
|
||
import java.text.Collator; | ||
import java.util.Arrays; | ||
import java.util.TreeSet; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
import us.cltnc.validators.constraints.OneOf; | ||
|
||
public class OneOfValidator implements ConstraintValidator<OneOf, String> { | ||
private String[] value; | ||
TreeSet<String> value; | ||
|
||
public void initialize(OneOf constraintAnnotation) { | ||
this.value = constraintAnnotation.value(); | ||
public void initialize(OneOf annotation) { | ||
if (! annotation.caseSensitive()) { | ||
Collator c = Collator.getInstance(); | ||
c.setStrength(Collator.PRIMARY); | ||
this.value = new TreeSet<String>(c); | ||
} else { | ||
this.value = new TreeSet<String>(); | ||
} | ||
this.value.addAll(Arrays.asList(annotation.value())); | ||
} | ||
|
||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
return Arrays.asList(this.value).contains(value); | ||
return value == null || this.value.contains(value); | ||
} | ||
} |
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