-
Notifications
You must be signed in to change notification settings - Fork 2
Handle Repeatable Constraints #267
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
blackbox-test/src/main/java/example/avaje/repeat/SignupRequest.java
This file contains hidden or 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,31 @@ | ||
package example.avaje.repeat; | ||
|
||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Pattern; | ||
import jakarta.validation.constraints.Size; | ||
|
||
@Valid | ||
public class SignupRequest { | ||
|
||
@NotBlank(message = "{signup.password.notblank1}") | ||
@Size(min = 5, max = 32, message = "{signup.password.size}") | ||
@Pattern(regexp = "^[a-zA-Z0-9!@#$^&*]*$", message = "{signup.password.invalid}") | ||
@Pattern(regexp = ".*[a-z].*", message = "{signup.password.lowercase}") | ||
@Pattern(regexp = ".*[A-Z].*", message = "{signup.password.uppercase}") | ||
@Pattern(regexp = ".*[0-9].*", message = "{signup.password.digit}") | ||
@Pattern(regexp = ".*[!@#$^&*].*", message = "{signup.password.special}") | ||
private String password; | ||
|
||
public SignupRequest(String password) { | ||
this.password = password; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
blackbox-test/src/main/resources/example/avaje/CustomMessages.properties
This file contains hidden or 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,3 +1,11 @@ | ||
example.avaje.MyKey.message=Invalid MyKey | ||
example.avaje.MySerial.message=Invalid my serial | ||
org.foo.MyCustomALong.message=Invalid special number | ||
|
||
signup.password.notblank1=Signup password must not be blank | ||
signup.password.size=Signup password size error | ||
signup.password.invalid=Signup password invalid | ||
signup.password.lowercase=Signup must have a lower case | ||
signup.password.uppercase=Signup must have at least 1 upper case | ||
signup.password.digit=Signup digit | ||
signup.password.special=Signup special character |
50 changes: 50 additions & 0 deletions
50
blackbox-test/src/test/java/example/avaje/repeat/SignupRequestTest.java
This file contains hidden or 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 example.avaje.repeat; | ||
|
||
import io.avaje.validation.ConstraintViolation; | ||
import io.avaje.validation.ConstraintViolationException; | ||
import io.avaje.validation.Validator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.fail; | ||
|
||
class SignupRequestTest { | ||
|
||
final Validator validator = Validator.builder() | ||
.addResourceBundles("example.avaje.CustomMessages") | ||
.build(); | ||
|
||
@Test | ||
void lowercaseNoSpecial() { | ||
SignupRequest req = new SignupRequest("foo"); | ||
|
||
var violations = all(req, Locale.ENGLISH); | ||
assertThat(violations).hasSize(3); | ||
assertThat(violations.get(0).message()).isEqualTo("Signup password size error"); | ||
assertThat(violations.get(1).message()).isEqualTo("Signup must have at least 1 upper case"); | ||
assertThat(violations.get(2).message()).isEqualTo("Signup special character"); | ||
} | ||
|
||
@Test | ||
void missingDigit() { | ||
SignupRequest req = new SignupRequest("fooBar!"); | ||
|
||
var violations = all(req, Locale.ENGLISH); | ||
assertThat(violations).hasSize(1); | ||
assertThat(violations.get(0).message()).isEqualTo("Signup digit"); | ||
} | ||
|
||
List<ConstraintViolation> all(Object any, Locale locale) { | ||
try { | ||
validator.validate(any, locale); | ||
fail("not expected"); | ||
return List.of(); | ||
} catch (ConstraintViolationException e) { | ||
return new ArrayList<>(e.violations()); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.