Skip to content

Commit c1c9068

Browse files
committed
feat: Validate string format 'ipv6'
1 parent 4f09f16 commit c1c9068

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/main/java/com/retailsvc/http/validate/DefaultValidator.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ private record FormatCheck(Predicate<String> isValid, String message) {}
5050
private static final Pattern IPV4 =
5151
Pattern.compile("^((25[0-5]|2[0-4]\\d|1?\\d?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1?\\d?\\d)$");
5252

53+
private static final Pattern IPV6 =
54+
Pattern.compile(
55+
"^("
56+
+ "([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}"
57+
+ "|([0-9a-fA-F]{1,4}:){1,7}:"
58+
+ "|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}"
59+
+ "|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}"
60+
+ "|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}"
61+
+ "|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}"
62+
+ "|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}"
63+
+ "|[0-9a-fA-F]{1,4}:(:[0-9a-fA-F]{1,4}){1,6}"
64+
+ "|:((:[0-9a-fA-F]{1,4}){1,7}|:)"
65+
+ ")$");
66+
5367
private static final Pattern HOSTNAME =
5468
Pattern.compile(
5569
"^(?=.{1,253}$)([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)"
@@ -69,7 +83,8 @@ private record FormatCheck(Predicate<String> isValid, String message) {}
6983
Map.entry(
7084
"hostname",
7185
new FormatCheck(s -> HOSTNAME.matcher(s).matches(), "not a valid hostname")),
72-
Map.entry("ipv4", new FormatCheck(s -> IPV4.matcher(s).matches(), "not a valid ipv4")));
86+
Map.entry("ipv4", new FormatCheck(s -> IPV4.matcher(s).matches(), "not a valid ipv4")),
87+
Map.entry("ipv6", new FormatCheck(s -> IPV6.matcher(s).matches(), "not a valid ipv6")));
7388

7489
private final Function<String, Schema> refResolver;
7590
private final ConcurrentMap<String, Pattern> compiledPatterns = new ConcurrentHashMap<>();

src/test/java/com/retailsvc/http/validate/StringIntegerNumberTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@ void stringFormatIpv4() {
130130
.isEqualTo("format");
131131
}
132132

133+
@Test
134+
void stringFormatIpv6() {
135+
StringSchema s = new StringSchema(Set.of(TypeName.STRING), null, null, null, "ipv6", null);
136+
assertThatCode(() -> v.validate("2001:0db8:85a3:0000:0000:8a2e:0370:7334", s, "/v"))
137+
.doesNotThrowAnyException();
138+
assertThatCode(() -> v.validate("2001:db8::1", s, "/v")).doesNotThrowAnyException();
139+
assertThatCode(() -> v.validate("::1", s, "/v")).doesNotThrowAnyException();
140+
assertThatThrownBy(() -> v.validate("not:an:ipv6", s, "/v"))
141+
.extracting(t -> ((ValidationException) t).error().keyword())
142+
.isEqualTo("format");
143+
assertThatThrownBy(() -> v.validate("12345::1", s, "/v"))
144+
.extracting(t -> ((ValidationException) t).error().keyword())
145+
.isEqualTo("format");
146+
}
147+
133148
@Test
134149
void integerWithMinMax() {
135150
IntegerSchema s =

0 commit comments

Comments
 (0)