-
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
29 changed files
with
220 additions
and
98 deletions.
There are no files selected for viewing
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
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
33 changes: 19 additions & 14 deletions
33
app/src/main/java/it/chalmers/gamma/app/common/Email.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 |
---|---|---|
@@ -1,28 +1,33 @@ | ||
package it.chalmers.gamma.app.common; | ||
|
||
import static it.chalmers.gamma.app.validation.ValidationHelper.*; | ||
|
||
import it.chalmers.gamma.app.user.domain.UserIdentifier; | ||
import it.chalmers.gamma.app.validation.ValidationResult; | ||
import it.chalmers.gamma.app.validation.Validator; | ||
import java.io.Serializable; | ||
import java.util.Objects; | ||
import java.util.regex.Pattern; | ||
import org.springframework.web.util.HtmlUtils; | ||
|
||
public record Email(String value) implements UserIdentifier, Serializable { | ||
|
||
private static final Pattern emailPattern = | ||
Pattern.compile( | ||
"^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"); | ||
|
||
public Email { | ||
Objects.requireNonNull(value); | ||
throwIfFailed(new EmailValidator().validate(value)); | ||
} | ||
|
||
value = HtmlUtils.htmlEscape(value, "UTF-8"); | ||
public static class EmailValidator implements Validator<String> { | ||
|
||
if (!emailPattern.matcher(value).matches()) { | ||
throw new IllegalArgumentException("Email does not look valid"); | ||
} | ||
} | ||
private static final Pattern emailPattern = | ||
Pattern.compile( | ||
"^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"); | ||
|
||
private static final Validator<String> emailRegex = | ||
value -> | ||
result( | ||
EmailValidator.emailPattern.matcher(value).matches(), "Email does not look valid"); | ||
|
||
public static boolean isValidEmail(String possibleEmail) { | ||
return emailPattern.matcher(possibleEmail).matches(); | ||
@Override | ||
public ValidationResult validate(String value) { | ||
return withValidators(IS_NOT_EMPTY, SANITIZED_HTML, emailRegex).validate(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
16 changes: 10 additions & 6 deletions
16
app/src/main/java/it/chalmers/gamma/app/user/domain/FirstName.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 |
---|---|---|
@@ -1,18 +1,22 @@ | ||
package it.chalmers.gamma.app.user.domain; | ||
|
||
import static it.chalmers.gamma.app.validation.ValidationHelper.*; | ||
|
||
import it.chalmers.gamma.app.validation.ValidationResult; | ||
import it.chalmers.gamma.app.validation.Validator; | ||
import java.io.Serializable; | ||
import java.util.Objects; | ||
import org.springframework.web.util.HtmlUtils; | ||
|
||
public record FirstName(String value) implements Serializable { | ||
|
||
public FirstName { | ||
Objects.requireNonNull(value); | ||
throwIfFailed(new FirstNameValidator().validate(value)); | ||
} | ||
|
||
value = HtmlUtils.htmlEscape(value, "UTF-8"); | ||
public static final class FirstNameValidator implements Validator<String> { | ||
|
||
if (value.isEmpty() || value.length() > 50) { | ||
throw new IllegalArgumentException("First name length must be between 1 and 50"); | ||
@Override | ||
public ValidationResult validate(String value) { | ||
return withValidators(IS_NOT_EMPTY, SANITIZED_HTML, MAX_LENGTH.apply(50)).validate(value); | ||
} | ||
} | ||
} |
16 changes: 10 additions & 6 deletions
16
app/src/main/java/it/chalmers/gamma/app/user/domain/LastName.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 |
---|---|---|
@@ -1,18 +1,22 @@ | ||
package it.chalmers.gamma.app.user.domain; | ||
|
||
import static it.chalmers.gamma.app.validation.ValidationHelper.*; | ||
|
||
import it.chalmers.gamma.app.validation.ValidationResult; | ||
import it.chalmers.gamma.app.validation.Validator; | ||
import java.io.Serializable; | ||
import java.util.Objects; | ||
import org.springframework.web.util.HtmlUtils; | ||
|
||
public record LastName(String value) implements Serializable { | ||
|
||
public LastName { | ||
Objects.requireNonNull(value); | ||
throwIfFailed(new LastNameValidator().validate(value)); | ||
} | ||
|
||
value = HtmlUtils.htmlEscape(value, "UTF-8"); | ||
public static final class LastNameValidator implements Validator<String> { | ||
|
||
if (value.isEmpty() || value.length() > 50) { | ||
throw new IllegalArgumentException("Last name length must be between 1 and 50"); | ||
@Override | ||
public ValidationResult validate(String value) { | ||
return withValidators(IS_NOT_EMPTY, SANITIZED_HTML, MAX_LENGTH.apply(50)).validate(value); | ||
} | ||
} | ||
} |
15 changes: 9 additions & 6 deletions
15
app/src/main/java/it/chalmers/gamma/app/user/domain/Nick.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 |
---|---|---|
@@ -1,18 +1,21 @@ | ||
package it.chalmers.gamma.app.user.domain; | ||
|
||
import static it.chalmers.gamma.app.validation.ValidationHelper.*; | ||
|
||
import it.chalmers.gamma.app.validation.*; | ||
import java.io.Serializable; | ||
import java.util.Objects; | ||
import org.springframework.web.util.HtmlUtils; | ||
|
||
public record Nick(String value) implements Serializable { | ||
|
||
public Nick { | ||
Objects.requireNonNull(value); | ||
throwIfFailed(new NickValidator().validate(value)); | ||
} | ||
|
||
value = HtmlUtils.htmlEscape(value, "UTF-8"); | ||
public static final class NickValidator implements Validator<String> { | ||
|
||
if (value.isEmpty() || value.length() > 30) { | ||
throw new IllegalArgumentException("Nick length must be between 1 and 30"); | ||
@Override | ||
public ValidationResult validate(String value) { | ||
return withValidators(IS_NOT_EMPTY, SANITIZED_HTML, MAX_LENGTH.apply(50)).validate(value); | ||
} | ||
} | ||
} |
Oops, something went wrong.