-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HV-1780 Add @Normalized contraint that validates unicode normalizatio…
…n form
- Loading branch information
Showing
14 changed files
with
302 additions
and
0 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
28 changes: 28 additions & 0 deletions
28
...or/src/test/java/org/hibernate/validator/ap/testmodel/ModelWithNormalizedConstraints.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 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.ap.testmodel; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.hibernate.validator.constraints.Normalized; | ||
|
||
public class ModelWithNormalizedConstraints { | ||
|
||
@Normalized | ||
public Collection<String> collection; | ||
|
||
@Normalized | ||
public List<String> list; | ||
|
||
@Normalized | ||
public Set<String> set; | ||
|
||
@Normalized | ||
public String string; | ||
} |
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
31 changes: 31 additions & 0 deletions
31
engine/src/main/java/org/hibernate/validator/cfg/defs/NormalizedDef.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,31 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
|
||
package org.hibernate.validator.cfg.defs; | ||
|
||
import org.hibernate.validator.cfg.ConstraintDef; | ||
import org.hibernate.validator.constraints.Normalized; | ||
|
||
import java.text.Normalizer; | ||
|
||
|
||
/** | ||
* Constraint definition for {@link Normalized}. | ||
* @author Craig Andrews | ||
* @since 6.1.6 | ||
*/ | ||
public class NormalizedDef extends ConstraintDef<NormalizedDef, Normalized> { | ||
|
||
public NormalizedDef() { | ||
super( Normalized.class ); | ||
} | ||
|
||
public NormalizedDef normalizationStrategy(Normalizer.Form form) { | ||
addParameter( "normalizationForm", form ); | ||
return this; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
engine/src/main/java/org/hibernate/validator/constraints/Normalized.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,62 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.constraints; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.CONSTRUCTOR; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.ElementType.TYPE_USE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.text.Normalizer; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
import org.hibernate.validator.constraints.Normalized.List; | ||
|
||
|
||
/** | ||
* Validate that a character sequence is of normalization form. | ||
* <p> | ||
* It is possible to validate a normalized value by setting the normalization strategy. | ||
* | ||
* @author Craig Andrews | ||
* @since 6.1.6 | ||
*/ | ||
@Documented | ||
@Constraint(validatedBy = { }) | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
@Repeatable(List.class) | ||
public @interface Normalized { | ||
|
||
Normalizer.Form form() default Normalizer.Form.NFC; | ||
|
||
String message() default "{org.hibernate.validator.constraints.Normalized.message}"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
|
||
/** | ||
* Defines several {@code @Normalized} annotations on the same element. | ||
*/ | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
@Documented | ||
public @interface List { | ||
Normalized[] value(); | ||
} | ||
} | ||
|
37 changes: 37 additions & 0 deletions
37
...in/java/org/hibernate/validator/internal/constraintvalidators/hv/NormalizedValidator.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,37 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.internal.constraintvalidators.hv; | ||
|
||
import java.text.Normalizer; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
import org.hibernate.validator.constraints.Normalized; | ||
|
||
/** | ||
* Check that a character sequence is normalized. | ||
* | ||
* @author Craig Andrews | ||
*/ | ||
public class NormalizedValidator implements ConstraintValidator<Normalized, CharSequence> { | ||
|
||
private Normalizer.Form form; | ||
|
||
@Override | ||
public void initialize(Normalized parameters) { | ||
form = parameters.form(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(CharSequence value, ConstraintValidatorContext constraintValidatorContext) { | ||
if ( value == null ) { | ||
return true; | ||
} | ||
return Normalizer.isNormalized( value, form ); | ||
} | ||
} |
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
Oops, something went wrong.