Skip to content

Commit

Permalink
Merge pull request #195 from sebastian-toepfer/format_assertion_uri-t…
Browse files Browse the repository at this point in the history
…emplate

add support for uri-template format
  • Loading branch information
sebastian-toepfer authored Dec 9, 2024
2 parents 2c42f28 + a2a9275 commit 56a6028
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ final class Formats {
Map.entry("duration", Map.entry(3339, "duration")),
Map.entry("uri", Map.entry(3986, "URI")),
Map.entry("uri-reference", Map.entry(3986, "URI-reference")),
Map.entry("uri-template", Map.entry(6570, "URI-Template")),
Map.entry("email", Map.entry(5321, "mailbox")),
Map.entry("ipv4", Map.entry(2673, "dotted-quad")),
Map.entry("uuid", Map.entry(4122, "UUID")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public final class Rfcs {
private static final String TIMEZONE = "([Zz]|([+-]\\d{2}\\:\\d{2}))";
private static final String TIME = "\\d{2}\\:\\d{2}\\:\\d{2}(\\.\\d+)?" + TIMEZONE;

//create regex with: https://abnf.msweet.org/index.php
private static final Map<Integer, Map<String, Rule>> RULES = Map.ofEntries(
Map.entry(
1123,
Expand Down Expand Up @@ -259,6 +258,20 @@ public final class Rfcs {
)
)
),
Map.entry(
6570,
Map.of(
"URI-Template",
new RegExRule(
Pattern.compile(
"(([!#-$&-;=?-\\[\\]_a-z~\\xa0-\\ud7ff\\uf900-\\ufdcf\\ufdf0-\\uffef\\ue000-\\uf8ff]|%[0-9A-F" +
"a-f][0-9A-Fa-f])|\\{[+#./;?&=,!@|]?(\\w|%[0-9A-Fa-f][0-9A-Fa-f])((\\.)?([a-zA-Z0-9_]|%[0-9A-" +
"Fa-f][0-9A-Fa-f]))*(:[1-9]\\d{0,3}|\\*)?(,(\\w|%[0-9A-Fa-f][0-9A-Fa-f])((\\.)?(\\w|%[0-9A-Fa" +
"-f][0-9A-Fa-f]))*(:[1-9]\\d{0,3}|\\*)?)*\\})*"
)
)
)
),
Map.entry(
6901,
Map.of("json-pointer", new RegExRule(Pattern.compile("^(/([\\x00-.0-}\\x7f-\\u010ffff]|\\~[01])*)*$")))
Expand Down
36 changes: 36 additions & 0 deletions vocabulary/format-assertion/src/main/resources/rfc/rfc6570
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
URI-Template = *( literals / expression )

literals = %x21 / %x23-24 / %x26-3B / %x3D / %x3F-5B
/ %x5D / %x5F / %x61-7A / %x7E / ucschar / iprivate
/ pct-encoded
; any Unicode character except: CTL, SP,
; DQUOTE, "%" (aside from pct-encoded),
; "<", ">", "\", "^", "`", "{", "|", "}"

expression = "{" [ operator ] variable-list "}"
operator = op-level2 / op-level3 / op-reserve
op-level2 = "+" / "#"
op-level3 = "." / "/" / ";" / "?" / "&"
op-reserve = "=" / "," / "!" / "@" / "|"

variable-list = varspec *( "," varspec )
varspec = varname [ modifier-level4 ]
varname = varchar *( ["."] varchar )
varchar = ALPHA / DIGIT / "_" / pct-encoded

modifier-level4 = prefix / explode

prefix = ":" max-length
max-length = %x31-39 0*3DIGIT ; positive integer < 10000

explode = "*"

pct-encoded = "%" HEXDIG HEXDIG
ucschar = %xA0-D7FF / %xF900-FDCF / %xFDF0-FFEF
/ %x10000-1FFFD / %x20000-2FFFD / %x30000-3FFFD
/ %x40000-4FFFD / %x50000-5FFFD / %x60000-6FFFD
/ %x70000-7FFFD / %x80000-8FFFD / %x90000-9FFFD
/ %xA0000-AFFFD / %xB0000-BFFFD / %xC0000-CFFFD
/ %xD0000-DFFFD / %xE1000-EFFFD

iprivate = %xE000-F8FF / %xF0000-FFFFD / %x100000-10FFFD
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ void should_found_urireferenceformat() {
assertThat(new Formats().findByName("uri-reference").applyTo("1://noUri"), is(false));
}

@Test
void should_found_uri_templateformat() {
assertThat(
new Formats().findByName("uri-template").applyTo("http://www.example.com/foo{?query,number}"),
is(true)
);
}

@Test
void should_found_uuidformat() {
assertThat(new Formats().findByName("uuid").applyTo("b7128c5b-eafc-4b6b-9b35-1c9e3dbaabb1"), is(true));
Expand Down

0 comments on commit 56a6028

Please sign in to comment.