feat) add json schema v6 uri-reference format #126
diff --git a/README.md b/README.md
index d0c2b8a..83ad31c 100644
--- a/README.md
+++ b/README.md
@@ -228,7 +228,7 @@
````json
{"type": "string", "format": "email"}
````
-Available formats: date-time, hostname, email, ipv4, ipv6, uri.
+Available formats: date-time, hostname, email, ipv4, ipv6, uri, uri-reference.
For repetitive or more complex formats, you can create custom format checkers and add them to gojsonschema like this:
diff --git a/format_checkers.go b/format_checkers.go
index c7214b0..94bd095 100644
--- a/format_checkers.go
+++ b/format_checkers.go
@@ -52,9 +52,12 @@
// http://tools.ietf.org/html/rfc3339#section-5.6
DateTimeFormatChecker struct{}
- // URIFormatCheckers validates a URI with a valid Scheme per RFC3986
+ // URIFormatChecker validates a URI with a valid Scheme per RFC3986
URIFormatChecker struct{}
+ // URIReferenceFormatChecker validates a URI or relative-reference per RFC3986
+ URIReferenceFormatChecker struct{}
+
// HostnameFormatChecker validates a hostname is in the correct format
HostnameFormatChecker struct{}
@@ -70,14 +73,15 @@
// so library users can add custom formatters
FormatCheckers = FormatCheckerChain{
formatters: map[string]FormatChecker{
- "date-time": DateTimeFormatChecker{},
- "hostname": HostnameFormatChecker{},
- "email": EmailFormatChecker{},
- "ipv4": IPV4FormatChecker{},
- "ipv6": IPV6FormatChecker{},
- "uri": URIFormatChecker{},
- "uuid": UUIDFormatChecker{},
- "regex": RegexFormatChecker{},
+ "date-time": DateTimeFormatChecker{},
+ "hostname": HostnameFormatChecker{},
+ "email": EmailFormatChecker{},
+ "ipv4": IPV4FormatChecker{},
+ "ipv6": IPV6FormatChecker{},
+ "uri": URIFormatChecker{},
+ "uri-reference": URIReferenceFormatChecker{},
+ "uuid": UUIDFormatChecker{},
+ "regex": RegexFormatChecker{},
},
}
@@ -173,6 +177,11 @@
return true
}
+func (f URIReferenceFormatChecker) IsFormat(input string) bool {
+ _, err := url.Parse(input)
+ return err == nil
+}
+
func (f HostnameFormatChecker) IsFormat(input string) bool {
return rxHostname.MatchString(input) && len(input) < 256
}
diff --git a/format_checkers_test.go b/format_checkers_test.go
index 04db0e7..ef8edaf 100644
--- a/format_checkers_test.go
+++ b/format_checkers_test.go
@@ -14,3 +14,10 @@
assert.False(t, checker.IsFormat("not-a-uuid"))
assert.False(t, checker.IsFormat("g1234567-89ab-cdef-0123-456789abcdef"))
}
+
+func TestURIReferenceFormatCheckerIsFormat(t *testing.T) {
+ checker := URIReferenceFormatChecker{}
+
+ assert.True(t, checker.IsFormat("relative"))
+ assert.True(t, checker.IsFormat("https://dummyhost.com/dummy-path?dummy-qp-name=dummy-qp-value"))
+}