Skip to content

Commit 51a365f

Browse files
authored
Merge pull request #101 from avaje/feature/example-custom-resource
Add examples with custom resource bundles
2 parents 292f8fa + 914c0fa commit 51a365f

File tree

9 files changed

+188
-0
lines changed

9 files changed

+188
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package example.avaje.composable;
2+
3+
import io.avaje.validation.constraints.NotBlank;
4+
import io.avaje.validation.constraints.Valid;
5+
6+
@Valid
7+
public record MyCustomPattern(
8+
@MyKey String key,
9+
@NotBlank String value) {
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package example.avaje.composable;
2+
3+
import io.avaje.validation.constraints.Constraint;
4+
import io.avaje.validation.constraints.Pattern;
5+
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
9+
import static java.lang.annotation.ElementType.*;
10+
import static java.lang.annotation.ElementType.TYPE_USE;
11+
import static java.lang.annotation.RetentionPolicy.SOURCE;
12+
13+
@Pattern(regexp = "[A-Z0-9_]{2,8}")
14+
@Constraint
15+
@Retention(SOURCE)
16+
@Target({METHOD, FIELD, ANNOTATION_TYPE, PARAMETER, TYPE_USE})
17+
public @interface MyKey {
18+
19+
String message() default "{example.avaje.MyKey.message}";
20+
21+
Class<?>[] groups() default {};
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package example.avaje.composable;
2+
3+
import io.avaje.validation.constraints.Constraint;
4+
import io.avaje.validation.constraints.Length;
5+
import io.avaje.validation.constraints.Pattern;
6+
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.Target;
9+
10+
import static java.lang.annotation.ElementType.*;
11+
import static java.lang.annotation.RetentionPolicy.SOURCE;
12+
13+
@Pattern(regexp = "[A-Z]+")
14+
@Length(max = 5)
15+
@Constraint
16+
@Retention(SOURCE)
17+
@Target({METHOD, FIELD, ANNOTATION_TYPE, PARAMETER, TYPE_USE})
18+
public @interface MySerial {
19+
20+
String message() default "{example.avaje.MySerial.message}";
21+
22+
Class<?>[] groups() default {};
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package example.avaje.composable;
2+
3+
import io.avaje.validation.constraints.NotBlank;
4+
import io.avaje.validation.constraints.Valid;
5+
6+
@Valid
7+
public record MySerialExample(
8+
@MySerial String key,
9+
@NotBlank String value) {
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
example.avaje.MyKey.message=Invalid MyKey
2+
example.avaje.MySerial.message=Invalid my serial
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example.avaje.MyKey.message=darf nicht MyKey
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# intentionally blank
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package example.avaje.composable;
2+
3+
import io.avaje.validation.ConstraintViolation;
4+
import io.avaje.validation.ConstraintViolationException;
5+
import io.avaje.validation.Validator;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.ArrayList;
9+
import java.util.Locale;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
import static org.assertj.core.api.Assertions.fail;
13+
14+
class MyCustomPatternTest {
15+
16+
final Validator validator = Validator.builder()
17+
.addResourceBundles("example.avaje.CustomMessages")
18+
.addLocales(Locale.GERMAN)
19+
.build();
20+
21+
@Test
22+
void valid() {
23+
validator.validate(new MyCustomPattern("SDFS", "Hi"));
24+
validator.validate(new MyCustomPattern("12", "Hi"));
25+
validator.validate(new MyCustomPattern("12345678", "Hi"));
26+
validator.validate(new MyCustomPattern("A234567Z", "Hi"));
27+
validator.validate(new MyCustomPattern("A2_456_Z", "Hi"));
28+
}
29+
30+
@Test
31+
void notValid() {
32+
var violation = one(new MyCustomPattern("SD*FS", "Hi"));
33+
assertThat(violation.message()).isEqualTo("Invalid MyKey");
34+
35+
assertThat(one(new MyCustomPattern("S", "Hi")).message()).isEqualTo("Invalid MyKey");
36+
assertThat(one(new MyCustomPattern("", "Hi")).message()).isEqualTo("Invalid MyKey");
37+
assertThat(one(new MyCustomPattern("123456789", "Hi")).message()).isEqualTo("Invalid MyKey");
38+
}
39+
40+
@Test
41+
void notValid_DE() {
42+
var violation = one(new MyCustomPattern("SD*FS", "Hi"), Locale.GERMAN);
43+
assertThat(violation.message()).isEqualTo("darf nicht MyKey");
44+
}
45+
46+
ConstraintViolation one(Object any) {
47+
return one(any, Locale.ENGLISH);
48+
}
49+
50+
ConstraintViolation one(Object any, Locale locale) {
51+
try {
52+
validator.validate(any, locale);
53+
fail("not expected");
54+
return null;
55+
} catch (ConstraintViolationException e) {
56+
var violations = new ArrayList<>(e.violations());
57+
assertThat(violations).hasSize(1);
58+
return violations.get(0);
59+
}
60+
}
61+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package example.avaje.composable;
2+
3+
import io.avaje.validation.ConstraintViolation;
4+
import io.avaje.validation.ConstraintViolationException;
5+
import io.avaje.validation.Validator;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.ArrayList;
9+
import java.util.Locale;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
import static org.assertj.core.api.Assertions.fail;
13+
14+
class MySerialTest {
15+
16+
final Validator validator = Validator.builder()
17+
.addResourceBundles("example.avaje.CustomMessages")
18+
.addLocales(Locale.GERMAN)
19+
.build();
20+
21+
@Test
22+
void valid() {
23+
validator.validate(new MySerialExample("A", "Hi"));
24+
validator.validate(new MySerialExample("ABCDE", "Hi"));
25+
}
26+
27+
@Test
28+
void notValid() {
29+
var violation = one(new MySerialExample("*", "Hi"));
30+
assertThat(violation.message()).isEqualTo("Invalid my serial");
31+
32+
assertThat(one(new MySerialExample("ABCDEF", "Hi")).message()).isEqualTo("Invalid my serial");
33+
assertThat(one(new MySerialExample("", "Hi")).message()).isEqualTo("Invalid my serial");
34+
assertThat(one(new MySerialExample("123456789", "Hi")).message()).isEqualTo("Invalid my serial");
35+
}
36+
37+
@Test
38+
void notValid_DE() {
39+
var violation = one(new MySerialExample("*", "Hi"), Locale.GERMAN);
40+
assertThat(violation.message()).isEqualTo("Invalid my serial"); // not translated for DE
41+
}
42+
43+
ConstraintViolation one(Object any) {
44+
return one(any, Locale.ENGLISH);
45+
}
46+
47+
ConstraintViolation one(Object any, Locale locale) {
48+
try {
49+
validator.validate(any, locale);
50+
fail("not expected");
51+
return null;
52+
} catch (ConstraintViolationException e) {
53+
var violations = new ArrayList<>(e.violations());
54+
assertThat(violations).hasSize(1);
55+
return violations.get(0);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)