Added input validation for creating events

This commit is contained in:
2021-12-20 12:57:18 +01:00
parent 0296333733
commit d081a88dcd
6 changed files with 77 additions and 34 deletions

View File

@@ -29,7 +29,7 @@ public class DataController {
try {
System.out.println(httpRequest.sendPostRequest(ADD_EVENT_ENDPOINT, event.getAsUrlParam()));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Es konnte keine Verbindung mit dem Server hergestellt werden.");
}
}

View File

@@ -2,9 +2,14 @@ package res;
import com.sun.jdi.event.StepEvent;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class Event {
@@ -61,7 +66,23 @@ public class Event {
String end,
LocalDateTime date,
int ownerId
) {
) throws IllegalArgumentException{
if(name.length() < 3){
throw new IllegalArgumentException("Der Name muss eine L\u00e4nge von 3 haben.");
}
Pattern pattern = Pattern.compile("[A-Za-zÄÖÜäöü0-9 =!?+*/$%€.:,;_<>()-]*");
Matcher matcher = pattern.matcher(name);
if(!matcher.matches()){
throw new IllegalArgumentException("Der Name Darf nur aus Zahlen, Buchstaben und folgenden Sonderzeichen bestehen: =!?+*/$%€.:,;_ <>()-");
}
if(priority < 0){
throw new IllegalArgumentException("Bitte eine Priorit\u00e4t w\u00e4hlen.");
}
LocalDateTime today = LocalDateTime.now().toLocalDate().atStartOfDay();
if(Duration.between(today, date).isNegative()){
throw new IllegalArgumentException("Das Datum muss in der Zukunft liegen.");
}
this.name = name;
this.priority = priority;
this.isFullDay = isFullDay;