Skip to content

Commit 403bafb

Browse files
committed
add code for bean validation
1 parent ffae5b0 commit 403bafb

15 files changed

Lines changed: 611 additions & 1 deletion

File tree

Bean-Validation-2-0/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
<artifactId>hibernate-validator-cdi</artifactId>
2828
<version>6.0.7.Final</version>
2929
</dependency>
30-
3130
</dependencies>
3231

3332
<build>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.readlearncode.constraints.optional;
2+
3+
import javax.validation.constraints.NotBlank;
4+
import javax.validation.constraints.NotNull;
5+
import javax.validation.constraints.Size;
6+
import java.util.Optional;
7+
8+
/**
9+
* Source code github.com/readlearncode
10+
*
11+
* @author Alex Theedom www.readlearncode.com
12+
* @version 1.0
13+
*/
14+
public class OptionalExample {
15+
16+
private Optional<@NotBlank @Size(min = 4, max = 10) String> text;
17+
18+
@NotNull
19+
private Optional<String> name;
20+
21+
public Optional<String> getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = Optional.ofNullable(name);
27+
}
28+
29+
public Optional<String> getText() {
30+
return text;
31+
}
32+
33+
public void setText(String text) {
34+
this.text = Optional.of(text);
35+
}
36+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.readlearncode.constraints.repeatable;
2+
3+
import javax.validation.constraints.Pattern;
4+
5+
/**
6+
* Source code github.com/readlearncode
7+
*
8+
* @author Alex Theedom www.readlearncode.com
9+
* @version 1.0
10+
*/
11+
public class RepeatedConstraint {
12+
13+
@Pattern(regexp = ".*com")
14+
@Pattern(regexp = "^([a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,}$")
15+
private String text;
16+
17+
public String getText() {
18+
return text;
19+
}
20+
21+
public void setText(String text) {
22+
this.text = text;
23+
}
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.readlearncode.containervalidation;
2+
3+
import javax.validation.constraints.NotBlank;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
/**
10+
* Source code github.com/readlearncode
11+
*
12+
* @author Alex Theedom www.readlearncode.com
13+
* @version 1.0
14+
*/
15+
public class Book {
16+
17+
private List<@NotBlank String> chapterTitles = new ArrayList<>();
18+
19+
private Map<@NotBlank String, @NotBlank String> authorChapter = new HashMap<>();
20+
21+
public List<String> getChapterTitles() {
22+
return chapterTitles;
23+
}
24+
25+
public void addChapterTitle(String chapterTitle) {
26+
chapterTitles.add(chapterTitle);
27+
}
28+
29+
public Map<String, String> getAuthorChapter() {
30+
return authorChapter;
31+
}
32+
33+
public void addAuthorChapter(String author, String chapter) {
34+
authorChapter.put(author,chapter);
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.readlearncode.containervalidation;
2+
3+
/**
4+
* Source code github.com/readlearncode
5+
*
6+
* @author Alex Theedom www.readlearncode.com
7+
* @version 1.0
8+
*/
9+
public class Choice {
10+
11+
private int id;
12+
13+
private String name;
14+
15+
public Choice(int id, String name) {
16+
this.id = id;
17+
this.name = name;
18+
}
19+
20+
public int getId() {
21+
return id;
22+
}
23+
24+
public void setId(int id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.readlearncode.containervalidation;
2+
3+
import javax.validation.constraints.NotBlank;
4+
import javax.validation.constraints.NotEmpty;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
/**
11+
* Source code github.com/readlearncode
12+
*
13+
* @author Alex Theedom www.readlearncode.com
14+
* @version 1.0
15+
*/
16+
public class ClientChoice {
17+
18+
private List<@ValidateChoice Choice> choices = new ArrayList<>();
19+
20+
private Map<@NotBlank String, @NotEmpty List<@ValidateChoice Choice>> clientChoices = new HashMap<>();
21+
22+
23+
public void addChoices(Choice choice) {
24+
choices.add(choice);
25+
}
26+
27+
public void addClientChoices(String name, List<Choice> choices) {
28+
clientChoices.put(name, choices);
29+
}
30+
31+
public List<Choice> getChoices() {
32+
return choices;
33+
}
34+
35+
public Map<String, List<Choice>> getClientChoices() {
36+
return clientChoices;
37+
}
38+
39+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.readlearncode.containervalidation;
2+
3+
import javax.validation.Valid;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* Source code github.com/readlearncode
9+
*
10+
* @author Alex Theedom www.readlearncode.com
11+
* @version 1.0
12+
*/
13+
public class ClientFavourites {
14+
15+
private List<@Valid Favourite> favourites = new ArrayList<>();
16+
17+
public List<Favourite> getFavourites() {
18+
return favourites;
19+
}
20+
21+
public void addFavourites(Favourite favourite) {
22+
favourites.add(favourite);
23+
}
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.readlearncode.containervalidation;
2+
3+
import javax.validation.constraints.NotBlank;
4+
import javax.validation.constraints.Positive;
5+
import javax.validation.constraints.Size;
6+
7+
/**
8+
* Source code github.com/readlearncode
9+
*
10+
* @author Alex Theedom www.readlearncode.com
11+
* @version 1.0
12+
*/
13+
public class Favourite {
14+
15+
@Positive
16+
private int id;
17+
18+
@NotBlank
19+
@Size(min = 4)
20+
private String name;
21+
22+
public Favourite(int id, String name) {
23+
this.id = id;
24+
this.name = name;
25+
}
26+
27+
public int getId() {
28+
return id;
29+
}
30+
31+
public void setId(int id) {
32+
this.id = id;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.readlearncode.containervalidation;
2+
3+
/**
4+
* Source code github.com/readlearncode
5+
*
6+
* @author Alex Theedom www.readlearncode.com
7+
* @version 1.0
8+
*/
9+
10+
import javax.validation.Constraint;
11+
import javax.validation.Payload;
12+
import java.lang.annotation.Documented;
13+
import java.lang.annotation.Retention;
14+
import java.lang.annotation.Target;
15+
16+
import static java.lang.annotation.ElementType.*;
17+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
18+
19+
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
20+
@Retention(RUNTIME)
21+
@Documented
22+
@Constraint(validatedBy = {ValidateChoiceValidator.class})
23+
public @interface ValidateChoice {
24+
25+
String message() default "";
26+
27+
Class<?>[] groups() default {};
28+
29+
Class<? extends Payload>[] payload() default {};
30+
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.readlearncode.containervalidation;
2+
3+
import javax.validation.ConstraintValidator;
4+
import javax.validation.ConstraintValidatorContext;
5+
6+
/**
7+
* Source code github.com/readlearncode
8+
*
9+
* @author Alex Theedom www.readlearncode.com
10+
* @version 1.0
11+
*/
12+
public class ValidateChoiceValidator implements ConstraintValidator<ValidateChoice, Choice> {
13+
14+
@Override
15+
public void initialize(ValidateChoice validateChoice) {
16+
}
17+
18+
@Override
19+
public boolean isValid(Choice choice, ConstraintValidatorContext context) {
20+
21+
if (choice.getName() != null && choice.getName().length() > 4) return true;
22+
23+
return false;
24+
}
25+
}

0 commit comments

Comments
 (0)