-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
app/src/main/java/it/chalmers/gamma/adapter/primary/web/ValidatedWith.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,14 @@ | ||
package it.chalmers.gamma.adapter.primary.web; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import it.chalmers.gamma.app.validation.Validator; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RUNTIME) | ||
@Target(FIELD) | ||
public @interface ValidatedWith { | ||
Class<? extends Validator<?>> value(); | ||
} |
44 changes: 44 additions & 0 deletions
44
app/src/main/java/it/chalmers/gamma/adapter/primary/web/WebValidationHelper.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,44 @@ | ||
package it.chalmers.gamma.adapter.primary.web; | ||
|
||
import it.chalmers.gamma.app.validation.FailedValidation; | ||
import it.chalmers.gamma.app.validation.ValidationResult; | ||
import it.chalmers.gamma.app.validation.Validator; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.FieldError; | ||
|
||
public class WebValidationHelper { | ||
|
||
public static void validateObject(Object obj, BindingResult bindingResult) { | ||
Field[] fields = obj.getClass().getDeclaredFields(); | ||
for (Field field : fields) { | ||
ValidatedWith validatedWith = field.getAnnotation(ValidatedWith.class); | ||
if (validatedWith != null) { | ||
field.setAccessible(true); | ||
try { | ||
Validator<Object> validator = | ||
(Validator<Object>) validatedWith.value().getDeclaredConstructor().newInstance(); | ||
Object fieldValue = field.get(obj); | ||
ValidationResult validationResult = validator.validate(fieldValue); | ||
if (validationResult instanceof FailedValidation failedValidation) { | ||
bindingResult.addError( | ||
new FieldError( | ||
"form", | ||
field.getName(), | ||
fieldValue, | ||
true, | ||
null, | ||
null, | ||
failedValidation.message())); | ||
} | ||
} catch (NoSuchMethodException | ||
| InstantiationException | ||
| IllegalAccessException | ||
| InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
app/src/main/java/it/chalmers/gamma/app/validation/FailedValidation.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,3 @@ | ||
package it.chalmers.gamma.app.validation; | ||
|
||
public record FailedValidation(String message) implements ValidationResult {} |
3 changes: 3 additions & 0 deletions
3
app/src/main/java/it/chalmers/gamma/app/validation/SuccessfulValidation.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,3 @@ | ||
package it.chalmers.gamma.app.validation; | ||
|
||
public record SuccessfulValidation() implements ValidationResult {} |
50 changes: 50 additions & 0 deletions
50
app/src/main/java/it/chalmers/gamma/app/validation/ValidationHelper.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,50 @@ | ||
package it.chalmers.gamma.app.validation; | ||
|
||
import java.util.function.Function; | ||
import org.springframework.web.util.HtmlUtils; | ||
|
||
public class ValidationHelper { | ||
|
||
@SafeVarargs | ||
public static <T> Validator<T> withValidators(Validator<T>... validators) { | ||
return value -> { | ||
for (Validator<T> validator : validators) { | ||
if (validator.validate(value) instanceof FailedValidation failedValidation) { | ||
return failedValidation; | ||
} | ||
} | ||
|
||
return new SuccessfulValidation(); | ||
}; | ||
} | ||
|
||
public static Validator<String> IS_NOT_EMPTY = | ||
value -> result(!(value == null || value.isEmpty()), "Cannot be empty"); | ||
|
||
public static Validator<String> SANITIZED_HTML = | ||
value -> | ||
result( | ||
value.equals(HtmlUtils.htmlEscape(value, "UTF-8")), | ||
"Cannot have illegal html characters"); | ||
|
||
public static Function<Integer, Validator<String>> MAX_LENGTH = | ||
(maxLength) -> | ||
value -> result(value.length() <= maxLength, "Must be between 1 and " + maxLength); | ||
|
||
public static Function<Integer, Validator<String>> MIN_LENGTH = | ||
(minLength) -> value -> result(value.length() >= minLength, "Must be at least " + minLength); | ||
|
||
public static ValidationResult result(boolean valid, String message) { | ||
if (valid) { | ||
return new SuccessfulValidation(); | ||
} else { | ||
return new FailedValidation(message); | ||
} | ||
} | ||
|
||
public static <T> void throwIfFailed(ValidationResult validationResult) { | ||
if (validationResult instanceof FailedValidation failedValidation) { | ||
throw new IllegalArgumentException(failedValidation.message()); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
app/src/main/java/it/chalmers/gamma/app/validation/ValidationResult.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,3 @@ | ||
package it.chalmers.gamma.app.validation; | ||
|
||
public sealed interface ValidationResult permits FailedValidation, SuccessfulValidation {} |
5 changes: 5 additions & 0 deletions
5
app/src/main/java/it/chalmers/gamma/app/validation/Validator.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,5 @@ | ||
package it.chalmers.gamma.app.validation; | ||
|
||
public interface Validator<T> { | ||
ValidationResult validate(T value); | ||
} |
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 @@ | ||
<th:block th:replace="~{common/alert :: error('File is larger than 2MB')}"/> |
1 change: 1 addition & 0 deletions
1
app/src/main/resources/templates/home/failed-to-edit-me-avatar.html
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 @@ | ||
<th:block th:replace="common/alert ::error('Failed to upload avatar. Is it a JPG, GIF or PNG?')"></th:block> |