Skip to content

Commit

Permalink
Merge pull request #192 from sebastian-toepfer/format_assertion_uri-r…
Browse files Browse the repository at this point in the history
…eference

Format assertion uri reference
  • Loading branch information
sebastian-toepfer authored Dec 7, 2024
2 parents 2c37f0a + d0a80ae commit 625ba56
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,41 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

final class Formats {

private static final Map<String, Map<Integer, String>> RFCS = Map.of(
private static final Map<String, Map.Entry<Integer, String>> RFCS = Map.of(
"hostname",
Map.of(1123, "hostname"),
Map.entry(1123, "hostname"),
"date-time",
Map.of(3339, "date-time"),
Map.entry(3339, "date-time"),
"date",
Map.of(3339, "full-date"),
Map.entry(3339, "full-date"),
"time",
Map.of(3339, "full-time"),
Map.entry(3339, "full-time"),
"duration",
Map.of(3339, "duration"),
Map.entry(3339, "duration"),
"uri",
Map.of(3986, "URI"),
Map.entry(3986, "URI"),
"uri-reference",
Map.entry(3986, "URI-reference"),
"email",
Map.of(5321, "mailbox"),
Map.entry(5321, "mailbox"),
"ipv4",
Map.of(2673, "dotted-quad"),
Map.entry(2673, "dotted-quad"),
"ipv6",
Map.of(4291, "IPv6address")
Map.entry(4291, "IPv6address")
);

Format findByName(final String name) {
return Optional.ofNullable(RFCS.get(name))
.stream()
.map(Map::entrySet)
.flatMap(Set::stream)
.findFirst()
.flatMap(entry ->
.map(entry ->
Rfcs.findRfcByNumber(entry.getKey()).map(rfc -> new RfcBasedFormat(name, rfc, entry.getValue()))
)
.flatMap(Optional::stream)
.map(Format.class::cast)
.findFirst()
.orElseGet(() -> new UnknownFormat(name));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,82 @@ public final class Rfcs {
"\\:|@)|/|\\?)*))?(#(((([-A-Z._a-z0-9]|~)|%[0-9A-Fa-f][0-9A-Fa-f]|(\\!|\\$|&|'|" +
"\\(|\\)|\\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\\+|,|;|\\=)|@)+)(/(([-A-Z._a-z0-9]|~)|%[0-9A-Fa-f][0-9A-Fa-f]|(\\!|\\$|&|'" +
"|\\(|\\)|\\*|\\+|,|;|\\=)|\\:|@)*)*|MISSING-0(([-A-Z._a-z0-9]|~)|%[0-9A-Fa-f][0-9A-Fa-f]|(" +
"\\!|\\$|&|'|\\(|\\)|\\*|\\+|,|;|\\=)|\\:|@))(\\?(((([-A-Z._a-z0-9]|~)|%[0-9A-Fa-f][0-9A-Fa-" +
"f]|(\\!|\\$|&|'|\\(|\\)|\\*|\\+|,|;|\\=)|\\:|@)|/|\\?)*))?(#(((([-A-Z._a-z0-9]|~)|%[0-9A-Fa" +
"-f][0-9A-Fa-f]|(\\!|\\$|&|'|\\(|\\)|\\*|\\+|,|;|\\=)|\\:|@)|/|\\?)*))?)$"
)
)
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,11 @@ void should_found_uriformat() {
assertThat(new Formats().findByName("uri").applyTo("http://www.example.com"), is(true));
assertThat(new Formats().findByName("uri").applyTo("1://noUri"), is(false));
}

@Test
void should_found_urireferenceformat() {
assertThat(new Formats().findByName("uri-reference").applyTo("http://www.example.com"), is(true));
assertThat(new Formats().findByName("uri-reference").applyTo("/json-schema"), is(true));
assertThat(new Formats().findByName("uri-reference").applyTo("1://noUri"), is(false));
}
}

0 comments on commit 625ba56

Please sign in to comment.