-
Notifications
You must be signed in to change notification settings - Fork 14
Feat: Add basic field validations to domain entities #61
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
Changes from all commits
f6b6255
4131745
068cd70
da735a9
4e3a058
a08a0b2
e7d28ce
155c687
6d90d32
87be9d6
39cdd65
5a615c6
c0d85bf
f5bff48
c40a777
30874ff
0091b0e
615c396
30355a9
5eac2c5
827bf95
87d8e4c
f3893be
f3b8df2
f2d38af
3ba7869
0858be6
3b92d00
ed6cbf2
45fe6ae
135bfdc
4c75b7d
f418645
c66c952
2ed55ec
9709558
d073cf4
97df320
31d0e72
6b0a202
477969c
624070e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| package cat.udl.eps.softarch.fll.domain; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
|
|
@@ -13,6 +11,8 @@ | |
| import lombok.Data; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.ToString; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
xertrec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Entity | ||
| @Data | ||
|
|
@@ -30,11 +30,21 @@ public class Coach extends UriEntity<Integer> { | |
| @NotBlank | ||
| @Column(unique = true) | ||
|
|
||
| private String emailAddress; | ||
|
|
||
| private String phoneNumber; | ||
|
|
||
| @ManyToMany(mappedBy = "trainedBy") | ||
| @ToString.Exclude | ||
| private Set<Team> teams = new HashSet<>(); | ||
|
|
||
| public static Coach create(String name, String emailAddress) { | ||
| DomainValidation.requireNonBlank(name, "name"); | ||
| DomainValidation.requireValidEmail(emailAddress, "emailAddress"); | ||
|
|
||
| Coach coach = new Coach(); | ||
| coach.name = name; | ||
| coach.emailAddress = emailAddress; | ||
| return coach; | ||
| } | ||
|
Comment on lines
+41
to
+49
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| package cat.udl.eps.softarch.fll.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import com.fasterxml.jackson.annotation.JsonManagedReference; | ||
| import jakarta.persistence.CascadeType; | ||
| import jakarta.persistence.Entity; | ||
|
|
@@ -11,39 +9,56 @@ | |
| import jakarta.validation.constraints.Size; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
xertrec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Entity | ||
| @Table(name = "competition_tables") | ||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor | ||
| @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) | ||
| public class CompetitionTable extends UriEntity<String> { | ||
|
|
||
| @Id | ||
| @EqualsAndHashCode.Include | ||
| private String id; | ||
pol-rivero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @OneToMany(mappedBy = "competitionTable", cascade = {CascadeType.PERSIST, CascadeType.MERGE}) | ||
| @Getter | ||
| @OneToMany(mappedBy = "competitionTable", cascade = CascadeType.ALL) | ||
pol-rivero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @JsonManagedReference("table-matches") | ||
| @Setter(lombok.AccessLevel.NONE) | ||
| private List<Match> matches = new ArrayList<>(); | ||
|
|
||
| @Getter | ||
| @OneToMany(mappedBy = "supervisesTable") | ||
| @Size(max = 3, message = "A table can have a maximum of 3 referees") | ||
| @JsonManagedReference("table-referees") | ||
| @Setter(lombok.AccessLevel.NONE) | ||
| private List<Referee> referees = new ArrayList<>(); | ||
|
|
||
| public static CompetitionTable create(String id) { | ||
| DomainValidation.requireNonBlank(id, "id"); | ||
| CompetitionTable table = new CompetitionTable(); | ||
| table.id = id; | ||
| return table; | ||
|
Comment on lines
+32
to
+36
|
||
| } | ||
|
Comment on lines
+32
to
+37
|
||
|
|
||
| @Override | ||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(String id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public void setMatches(List<Match> matches) { | ||
| new ArrayList<>(this.matches).forEach(this::removeMatch); | ||
| if (matches != null) { | ||
| matches.forEach(this::addMatch); | ||
| } | ||
| } | ||
|
|
||
| public void setReferees(List<Referee> referees) { | ||
| new ArrayList<>(this.referees).forEach(this::removeReferee); | ||
| if (referees != null) { | ||
| referees.forEach(this::addReferee); | ||
| } | ||
| } | ||
|
|
||
| public void addMatch(Match match) { | ||
| if (match == null) { | ||
| return; | ||
|
|
@@ -55,10 +70,11 @@ public void addMatch(Match match) { | |
|
|
||
| CompetitionTable previousTable = match.getCompetitionTable(); | ||
| if (previousTable != null && previousTable != this) { | ||
| previousTable.getMatches().removeIf(m -> m == match); | ||
| previousTable.removeMatch(match); | ||
| } | ||
| if (!matches.contains(match)) { | ||
| matches.add(match); | ||
| } | ||
|
|
||
| this.matches.add(match); | ||
| match.setCompetitionTable(this); | ||
| } | ||
|
|
||
|
|
@@ -67,18 +83,8 @@ public void removeMatch(Match match) { | |
| return; | ||
| } | ||
|
|
||
| if (this.matches.removeIf(m -> m == match)) { | ||
| match.setCompetitionTable(null); | ||
| } | ||
| } | ||
|
|
||
| public void setReferees(List<Referee> referees) { | ||
| if (referees == this.referees) { | ||
| return; | ||
| } | ||
| List<Referee> incoming = (referees == null) ? List.of() : new ArrayList<>(referees); | ||
| new ArrayList<>(this.referees).forEach(this::removeReferee); | ||
| incoming.forEach(this::addReferee); | ||
| matches.remove(match); | ||
| match.setCompetitionTable(null); | ||
| } | ||
|
|
||
| public void addReferee(Referee referee) { | ||
|
|
@@ -96,10 +102,12 @@ public void addReferee(Referee referee) { | |
|
|
||
| CompetitionTable previousTable = referee.getSupervisesTable(); | ||
| if (previousTable != null && previousTable != this) { | ||
| previousTable.getReferees().removeIf(r -> r == referee); | ||
| previousTable.removeReferee(referee); | ||
| } | ||
|
|
||
| this.referees.add(referee); | ||
| if (referees.contains(referee)) { | ||
| return; | ||
| } | ||
| referees.add(referee); | ||
| referee.setSupervisesTable(this); | ||
| } | ||
|
|
||
|
|
@@ -108,8 +116,7 @@ public void removeReferee(Referee referee) { | |
| return; | ||
| } | ||
|
|
||
| if (this.referees.removeIf(r -> r == referee)) { | ||
| referee.setSupervisesTable(null); | ||
| } | ||
| referees.remove(referee); | ||
| referee.setSupervisesTable(null); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,83 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package cat.udl.eps.softarch.fll.domain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.time.LocalDate; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.regex.Pattern; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public final class DomainValidation { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static final Pattern EMAIL_PATTERN = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pattern.compile("^[\\w\\-.]+@([\\w-]+\\.)+[\\w-]{2,}$"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private DomainValidation() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void requireNonBlank(String value, String fieldName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " must not be null"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value.isBlank()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " must not be blank"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
xertrec marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void requireValidEmail(String email, String fieldName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requireNonBlank(email, fieldName); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!EMAIL_PATTERN.matcher(email).matches()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " must be a valid email address"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void requireNonNegative(Integer value, String fieldName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " must not be null"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " must not be negative"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void requireMin(Integer value, Integer minValue, String fieldName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " must not be null"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value < minValue) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " must not be less than " + minValue); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void requirePast(LocalDate value, String fieldName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " must not be null"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!value.isBefore(LocalDate.now())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " must be in the past"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void requireLengthBetween(String value, Integer minLength, Integer maxLength, String fieldName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " must not be null"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value.length() < minLength) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " length must not be less than " + minLength); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value.length() > maxLength) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " length must not be more than " + maxLength); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pol-rivero marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void requireMaxLength(String value, Integer maxLength, String fieldName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " must not be null"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value.length() > maxLength) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " length must not be more than " + maxLength); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void requireNonNull(Object value, String fieldName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (value == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new DomainValidationException(fieldName + " must not be null"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+11
to
+80
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private DomainValidation() { | |
| } | |
| public static void requireNonBlank(String value, String fieldName) { | |
| if (value == null) { | |
| throw new DomainValidationException(fieldName + " must not be null"); | |
| } | |
| if (value.isBlank()) { | |
| throw new DomainValidationException(fieldName + " must not be blank"); | |
| } | |
| } | |
| public static void requireValidEmail(String email, String fieldName) { | |
| requireNonBlank(email, fieldName); | |
| if (!EMAIL_PATTERN.matcher(email).matches()) { | |
| throw new DomainValidationException(fieldName + " must be a valid email address"); | |
| } | |
| } | |
| public static void requireNonNegative(Integer value, String fieldName) { | |
| if (value == null) { | |
| throw new DomainValidationException(fieldName + " must not be null"); | |
| } | |
| if (value < 0) { | |
| throw new DomainValidationException(fieldName + " must not be negative"); | |
| } | |
| } | |
| public static void requireMin(Integer value, Integer minValue, String fieldName) { | |
| if (value == null) { | |
| throw new DomainValidationException(fieldName + " must not be null"); | |
| } | |
| if (value < minValue) { | |
| throw new DomainValidationException(fieldName + " must not be less than " + minValue); | |
| } | |
| } | |
| public static void requirePast(LocalDate value, String fieldName) { | |
| if (value == null) { | |
| throw new DomainValidationException(fieldName + " must not be null"); | |
| } | |
| if (!value.isBefore(LocalDate.now())) { | |
| throw new DomainValidationException(fieldName + " must be in the past"); | |
| } | |
| } | |
| public static void requireLengthBetween(String value, Integer minLength, Integer maxLength, String fieldName) { | |
| if (value == null) { | |
| throw new DomainValidationException(fieldName + " must not be null"); | |
| } | |
| if (value.length() < minLength) { | |
| throw new DomainValidationException(fieldName + " length must not be less than " + minLength); | |
| } | |
| if (value.length() > maxLength) { | |
| throw new DomainValidationException(fieldName + " length must not be more than " + maxLength); | |
| } | |
| } | |
| public static void requireMaxLength(String value, Integer maxLength, String fieldName) { | |
| if (value == null) { | |
| throw new DomainValidationException(fieldName + " must not be null"); | |
| } | |
| if (value.length() > maxLength) { | |
| throw new DomainValidationException(fieldName + " length must not be more than " + maxLength); | |
| } | |
| } | |
| public static void requireNonNull(Object value, String fieldName) { | |
| if (value == null) { | |
| throw new DomainValidationException(fieldName + " must not be null"); | |
| private static final String ERROR_CODE_NULL_VALUE = "NULL_VALUE"; | |
| private static final String ERROR_CODE_INVALID_EMAIL = "INVALID_EMAIL"; | |
| private static final String ERROR_CODE_NEGATIVE_VALUE = "NEGATIVE_VALUE"; | |
| private static final String ERROR_CODE_BELOW_MIN_VALUE = "BELOW_MIN_VALUE"; | |
| private static final String ERROR_CODE_VALUE_NOT_IN_PAST = "VALUE_NOT_IN_PAST"; | |
| private static final String ERROR_CODE_STRING_TOO_SHORT = "STRING_TOO_SHORT"; | |
| private static final String ERROR_CODE_STRING_TOO_LONG = "STRING_TOO_LONG"; | |
| private static final String ERROR_CODE_EXCEEDS_MAX_LENGTH = "EXCEEDS_MAX_LENGTH"; | |
| private static final String ERROR_CODE_NULL_OBJECT = "NULL_OBJECT"; | |
| private DomainValidation() { | |
| } | |
| private static void throwValidationException(String errorCode, String message) { | |
| throw new DomainValidationException("[" + errorCode + "] " + message); | |
| } | |
| public static void requireNonBlank(String value, String fieldName) { | |
| if (value == null) { | |
| throwValidationException(ERROR_CODE_NULL_VALUE, fieldName + " must not be null"); | |
| } | |
| if (value.isBlank()) { | |
| throwValidationException(ERROR_CODE_STRING_TOO_SHORT, fieldName + " must not be blank"); | |
| } | |
| } | |
| public static void requireValidEmail(String email, String fieldName) { | |
| requireNonBlank(email, fieldName); | |
| if (!EMAIL_PATTERN.matcher(email).matches()) { | |
| throwValidationException(ERROR_CODE_INVALID_EMAIL, fieldName + " must be a valid email address"); | |
| } | |
| } | |
| public static void requireNonNegative(Integer value, String fieldName) { | |
| if (value == null) { | |
| throwValidationException(ERROR_CODE_NULL_VALUE, fieldName + " must not be null"); | |
| } | |
| if (value < 0) { | |
| throwValidationException(ERROR_CODE_NEGATIVE_VALUE, fieldName + " must not be negative"); | |
| } | |
| } | |
| public static void requireMin(Integer value, Integer minValue, String fieldName) { | |
| if (value == null) { | |
| throwValidationException(ERROR_CODE_NULL_VALUE, fieldName + " must not be null"); | |
| } | |
| if (value < minValue) { | |
| throwValidationException(ERROR_CODE_BELOW_MIN_VALUE, fieldName + " must not be less than " + minValue); | |
| } | |
| } | |
| public static void requirePast(LocalDate value, String fieldName) { | |
| if (value == null) { | |
| throwValidationException(ERROR_CODE_NULL_VALUE, fieldName + " must not be null"); | |
| } | |
| if (!value.isBefore(LocalDate.now())) { | |
| throwValidationException(ERROR_CODE_VALUE_NOT_IN_PAST, fieldName + " must be in the past"); | |
| } | |
| } | |
| public static void requireLengthBetween(String value, Integer minLength, Integer maxLength, String fieldName) { | |
| if (value == null) { | |
| throwValidationException(ERROR_CODE_NULL_VALUE, fieldName + " must not be null"); | |
| } | |
| if (value.length() < minLength) { | |
| throwValidationException(ERROR_CODE_STRING_TOO_SHORT, fieldName + " length must not be less than " + minLength); | |
| } | |
| if (value.length() > maxLength) { | |
| throwValidationException(ERROR_CODE_STRING_TOO_LONG, fieldName + " length must not be more than " + maxLength); | |
| } | |
| } | |
| public static void requireMaxLength(String value, Integer maxLength, String fieldName) { | |
| if (value == null) { | |
| throwValidationException(ERROR_CODE_NULL_VALUE, fieldName + " must not be null"); | |
| } | |
| if (value.length() > maxLength) { | |
| throwValidationException(ERROR_CODE_EXCEEDS_MAX_LENGTH, fieldName + " length must not be more than " + maxLength); | |
| } | |
| } | |
| public static void requireNonNull(Object value, String fieldName) { | |
| if (value == null) { | |
| throwValidationException(ERROR_CODE_NULL_OBJECT, fieldName + " must not be null"); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package cat.udl.eps.softarch.fll.domain; | ||
|
|
||
| public class DomainValidationException extends RuntimeException { | ||
|
|
||
| public DomainValidationException(String message) { | ||
| super(message); | ||
| } | ||
| } | ||
|
Comment on lines
+1
to
+8
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,42 @@ | ||
| package cat.udl.eps.softarch.fll.domain; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.ManyToMany; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import lombok.AccessLevel; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.Setter; | ||
| import lombok.ToString; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
xertrec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Entity | ||
| @Table(name = "floaters") | ||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
xertrec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true) | ||
| public class Floater extends Volunteer { | ||
|
|
||
|
|
||
| @NotBlank(message = "Student code is mandatory") | ||
| @Column(unique = true) | ||
| private String studentCode; | ||
|
|
||
| @ManyToMany(mappedBy = "floaters") | ||
| @ToString.Exclude | ||
| private Set<Team> assistedTeams = new HashSet<>(); | ||
|
|
||
| public static Floater create(String name, String emailAddress, String phoneNumber, String studentCode) { | ||
| DomainValidation.requireNonBlank(studentCode, "studentCode"); | ||
|
|
||
| Floater floater = new Floater(); | ||
| floater.initFields(name, emailAddress, phoneNumber); | ||
| floater.studentCode = studentCode; | ||
| return floater; | ||
xertrec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Award.create(...)validates inputs, butAwardstill has a public no-arg constructor and Lombok-generated setters via@Data, so invalid awards (blankname, nulledition/winner) can still be instantiated and mutated directly, bypassing the new validation. To fully enforce the invariant at creation time, consider making the no-arg constructorprotectedand validating/removing setters for mandatory fields.