Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JSR 303 validation groups #32

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/codeStyleSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 125 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ the new AngularJS Model Validator.
- [Servlet](#servlet)
- [Dependency on valdr](#dependency-on-valdr)
- [Mapping of Bean Validation constraints to valdr constraints](#mapping-of-bean-validation-constraints-to-valdr-constraints)
- [Including validation group information in the generated JSON](#including-validation-group-information-in-the-generated-json)
- [Support](#support)
- [License](#license)

Expand All @@ -43,6 +44,8 @@ client start or on-demand)
- whether to output simple or full type names
- the output file name (CLI only)
- CORS `Access-Control-Allow-Origin` HTTP header value (Servlet only)
- whether to output JSR-303 validation groups
- which packages to scan for validation group marker interfaces
- Servlet offers built-in [CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) support

## Use
Expand Down Expand Up @@ -137,6 +140,53 @@ The [BuiltInConstraint.java](https://github.com/netceteragroup/valdr-bean-valida
| [Email](https://docs.jboss.org/hibernate/validator/5.1/api/org/hibernate/validator/constraints/Email.html) |[email](https://github.com/netceteragroup/valdr#email) | proprietary Hibernate Validator (not in Bean Validation spec) |
| [URL](https://docs.jboss.org/hibernate/validator/5.1/api/org/hibernate/validator/constraints/URL.html) |[url](https://github.com/netceteragroup/valdr#url) | proprietary Hibernate Validator (not in Bean Validation spec) |

## Including validation group information in the generated JSON

Validation groups allow you to enforce a different set of validation constraints in different situations.

To generate group information set the following two parameters:
* outputValidationGroups: true
* validationGroupPackages: [<an array of fully qualified names of packages containing your validation group interfaces>]

The generated JSON will include for each constraint the value of the ```groups``` attribute of the constraint with the inheritance
hierarchy of the validation group marker interfaces expanded. If a constraint does not have the groups attribute specified,
the default value is considered to be javax.validation.Default.class, and the groups array in the generated JSON will contain "Default" and
all your interfaces extending javax.validation.Default and belonging to one of the packages in validationGroupPackages.

When given
```java
public interface Draft extends Default {
}
public interface Published extends Draft {
}
...
@NotNull
private String mandatoryForAll;

@NotNull(groups = Draft.class)
private String mandatoryForDraft;

@NotNull(groups = {Published.class})
private String mandatoryForPublished;
```

The generated JSON will include

```JSON
mandatoryForAll:
required:
groups: ["Published", "Draft", "Default"]
...
mandatoryForAll:
required:
groups: ["Published", "Draft"]
...
mandatoryForPublished:
required:
groups: ["Published"]
```


## Support

[Ask a question on Stack Overflow](http://stackoverflow.com/questions/ask?tags=valdr-bean-validation) and tag it with [`valdr-bean-validation`](http://stackoverflow.com/tags/valdr-bean-validation).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.validation.groups.Default;

public class Person {
@NotNull(message = "\\foo")
private String firstName;
@Size(min = 4, max = 31)
@Size(min = 4, max = 31, groups={Default.class})
private String lastName;
@CreditCardNumber
private String creditCardNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"customAnnotationClasses": ["org.hibernate.validator.constraints.CreditCardNumber"],
"corsAllowOriginPattern": "*",
"outputFullTypeName": false,
"outputFile": "${project.build.directory}/${project.build.finalName}/constraints.json"
"outputFile": "${project.build.directory}/${project.build.finalName}/constraints.json",
"outputValidationGroups": false
}
3 changes: 2 additions & 1 deletion valdr-bean-validation-demo/valdr-bean-validation-demo.iml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.0.0" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-simple:1.7.7" level="project" />
</component>
</module>
</module>

3 changes: 2 additions & 1 deletion valdr-bean-validation-parent.iml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
</module>

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class AnnotatedClass {
private final Class clazz;
private final List<String> excludedFields;
private final Iterable<Class<? extends Annotation>> relevantAnnotationClasses;
private final boolean outputValidationGroups;

/**
* @param clazz wrapped class
Expand All @@ -27,10 +28,27 @@ public class AnnotatedClass {
* AnnotatedClass#extractValidationRules()} is invoked
*/
AnnotatedClass(Class clazz, List<String> excludedFields, Iterable<Class<? extends Annotation>>
relevantAnnotationClasses) {
relevantAnnotationClasses) {
this.clazz = clazz;
this.excludedFields = excludedFields;
this.relevantAnnotationClasses = relevantAnnotationClasses;
this.outputValidationGroups = false;
}

/**
* @param clazz wrapped class
* @param excludedFields collection of fully qualified field names which are skipped by the parser
* @param relevantAnnotationClasses only these annotation classes are considered when {@link
* AnnotatedClass#extractValidationRules()} is invoked
* @param outputValidationGroups Whether to output the value of the validation groups attribute of the validation
* annotations.
*/
AnnotatedClass(Class clazz, List<String> excludedFields, Iterable<Class<? extends Annotation>>
relevantAnnotationClasses, boolean outputValidationGroups) {
this.clazz = clazz;
this.excludedFields = excludedFields;
this.relevantAnnotationClasses = relevantAnnotationClasses;
this.outputValidationGroups = outputValidationGroups;
}

/**
Expand All @@ -45,7 +63,7 @@ ClassConstraints extractValidationRules() {
for (Field field : allFields) {
if (isNotExcluded(field)) {
FieldConstraints fieldValidationRules = new AnnotatedField(field,
relevantAnnotationClasses).extractValidationRules();
relevantAnnotationClasses, outputValidationGroups).extractValidationRules();
if (fieldValidationRules.size() > 0) {
classConstraints.put(field.getName(), fieldValidationRules);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
public class AnnotatedField {
private final Field field;
private final Iterable<Class<? extends Annotation>> relevantAnnotationClasses;
private final boolean outputValidationGroups;

/**
* @param field wrapped field
* @param relevantAnnotationClasses only these annotation classes are considered when {@link
* AnnotatedField#extractValidationRules()} is invoked
*/
AnnotatedField(Field field, Iterable<Class<? extends Annotation>> relevantAnnotationClasses) {
AnnotatedField(Field field, Iterable<Class<? extends Annotation>> relevantAnnotationClasses, boolean outputValidationGroups) {
this.field = field;
this.relevantAnnotationClasses = relevantAnnotationClasses;
this.outputValidationGroups = outputValidationGroups;
}

/**
Expand All @@ -35,7 +37,7 @@ FieldConstraints extractValidationRules() {

for (Annotation annotation : annotations) {
if (Iterables.contains(relevantAnnotationClasses, annotation.annotationType())) {
ConstraintAttributes constraintAttributes = new ConstraintAttributes(annotation);
ConstraintAttributes constraintAttributes = new ConstraintAttributes(annotation, outputValidationGroups);
BuiltInConstraint supportedValidator = BuiltInConstraint.valueOfAnnotationClassOrNull(annotation
.annotationType());
if (supportedValidator == null) {
Expand Down
Loading