Skip to content

Commit b93a66e

Browse files
authored
Fix iri, iri-reference, uri and uri-reference (#1071)
1 parent 622475b commit b93a66e

File tree

5 files changed

+158
-1
lines changed

5 files changed

+158
-1
lines changed

src/main/java/com/networknt/schema/format/AbstractRFC3986Format.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.net.URI;
44
import java.net.URISyntaxException;
5+
import java.util.regex.Pattern;
56

67
import com.networknt.schema.ExecutionContext;
78
import com.networknt.schema.Format;
@@ -10,13 +11,15 @@
1011
* {@link AbstractFormat} for RFC 3986.
1112
*/
1213
public abstract class AbstractRFC3986Format implements Format {
14+
private static final Pattern VALID = Pattern.compile("([A-Za-z0-9+-\\.]*:)?//|[A-Za-z0-9+-\\.]+:");
15+
1316
@Override
1417
public final boolean matches(ExecutionContext executionContext, String value) {
1518
try {
1619
URI uri = new URI(value);
1720
return validate(uri);
1821
} catch (URISyntaxException e) {
19-
return false;
22+
return handleException(e);
2023
}
2124
}
2225

@@ -28,4 +31,16 @@ public final boolean matches(ExecutionContext executionContext, String value) {
2831
*/
2932
protected abstract boolean validate(URI uri);
3033

34+
/**
35+
* Determines if the uri matches the format.
36+
*
37+
* @param e the URISyntaxException
38+
* @return false if it does not match
39+
*/
40+
protected boolean handleException(URISyntaxException e) {
41+
if (VALID.matcher(e.getInput()).matches()) {
42+
return true;
43+
}
44+
return false;
45+
}
3146
}

src/test/java/com/networknt/schema/format/IriFormatTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,39 @@ void iriShouldPass() {
8282
assertTrue(messages.isEmpty());
8383
}
8484

85+
@Test
86+
void noAuthorityShouldPass() {
87+
String schemaData = "{\r\n"
88+
+ " \"format\": \"iri\"\r\n"
89+
+ "}";
90+
91+
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
92+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
93+
Set<ValidationMessage> messages = schema.validate("\"http://\"", InputFormat.JSON);
94+
assertTrue(messages.isEmpty());
95+
}
96+
97+
@Test
98+
void noSchemeNoAuthorityShouldPass() {
99+
String schemaData = "{\r\n"
100+
+ " \"format\": \"iri\"\r\n"
101+
+ "}";
102+
103+
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
104+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
105+
Set<ValidationMessage> messages = schema.validate("\"//\"", InputFormat.JSON);
106+
assertTrue(messages.isEmpty());
107+
}
108+
109+
@Test
110+
void noPathShouldPass() {
111+
String schemaData = "{\r\n"
112+
+ " \"format\": \"iri\"\r\n"
113+
+ "}";
114+
115+
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
116+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
117+
Set<ValidationMessage> messages = schema.validate("\"about:\"", InputFormat.JSON);
118+
assertTrue(messages.isEmpty());
119+
}
85120
}

src/test/java/com/networknt/schema/format/IriReferenceFormatTest.java

+36
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,40 @@ void iriShouldPass() {
8282
assertTrue(messages.isEmpty());
8383
}
8484

85+
@Test
86+
void noAuthorityShouldPass() {
87+
String schemaData = "{\r\n"
88+
+ " \"format\": \"iri-reference\"\r\n"
89+
+ "}";
90+
91+
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
92+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
93+
Set<ValidationMessage> messages = schema.validate("\"http://\"", InputFormat.JSON);
94+
assertTrue(messages.isEmpty());
95+
}
96+
97+
@Test
98+
void noSchemeNoAuthorityShouldPass() {
99+
String schemaData = "{\r\n"
100+
+ " \"format\": \"iri-reference\"\r\n"
101+
+ "}";
102+
103+
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
104+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
105+
Set<ValidationMessage> messages = schema.validate("\"//\"", InputFormat.JSON);
106+
assertTrue(messages.isEmpty());
107+
}
108+
109+
@Test
110+
void noPathShouldPass() {
111+
String schemaData = "{\r\n"
112+
+ " \"format\": \"iri-reference\"\r\n"
113+
+ "}";
114+
115+
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
116+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
117+
Set<ValidationMessage> messages = schema.validate("\"about:\"", InputFormat.JSON);
118+
assertTrue(messages.isEmpty());
119+
}
120+
85121
}

src/test/java/com/networknt/schema/format/UriFormatTest.java

+36
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,40 @@ void iriShouldFail() {
8181
InputFormat.JSON);
8282
assertFalse(messages.isEmpty());
8383
}
84+
85+
@Test
86+
void noAuthorityShouldPass() {
87+
String schemaData = "{\r\n"
88+
+ " \"format\": \"uri\"\r\n"
89+
+ "}";
90+
91+
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
92+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
93+
Set<ValidationMessage> messages = schema.validate("\"http://\"", InputFormat.JSON);
94+
assertTrue(messages.isEmpty());
95+
}
96+
97+
@Test
98+
void noSchemeNoAuthorityShouldPass() {
99+
String schemaData = "{\r\n"
100+
+ " \"format\": \"uri\"\r\n"
101+
+ "}";
102+
103+
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
104+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
105+
Set<ValidationMessage> messages = schema.validate("\"//\"", InputFormat.JSON);
106+
assertTrue(messages.isEmpty());
107+
}
108+
109+
@Test
110+
void noPathShouldPass() {
111+
String schemaData = "{\r\n"
112+
+ " \"format\": \"uri\"\r\n"
113+
+ "}";
114+
115+
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
116+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
117+
Set<ValidationMessage> messages = schema.validate("\"about:\"", InputFormat.JSON);
118+
assertTrue(messages.isEmpty());
119+
}
84120
}

src/test/java/com/networknt/schema/format/UriReferenceFormatTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,39 @@ void iriShouldFail() {
8282
assertFalse(messages.isEmpty());
8383
}
8484

85+
@Test
86+
void noAuthorityShouldPass() {
87+
String schemaData = "{\r\n"
88+
+ " \"format\": \"uri-reference\"\r\n"
89+
+ "}";
90+
91+
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
92+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
93+
Set<ValidationMessage> messages = schema.validate("\"http://\"", InputFormat.JSON);
94+
assertTrue(messages.isEmpty());
95+
}
96+
97+
@Test
98+
void noSchemeNoAuthorityShouldPass() {
99+
String schemaData = "{\r\n"
100+
+ " \"format\": \"uri-reference\"\r\n"
101+
+ "}";
102+
103+
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
104+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
105+
Set<ValidationMessage> messages = schema.validate("\"//\"", InputFormat.JSON);
106+
assertTrue(messages.isEmpty());
107+
}
108+
109+
@Test
110+
void noPathShouldPass() {
111+
String schemaData = "{\r\n"
112+
+ " \"format\": \"uri-reference\"\r\n"
113+
+ "}";
114+
115+
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
116+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
117+
Set<ValidationMessage> messages = schema.validate("\"about:\"", InputFormat.JSON);
118+
assertTrue(messages.isEmpty());
119+
}
85120
}

0 commit comments

Comments
 (0)