Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iri, iri-reference, uri and uri-reference #1071

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.util.regex.Pattern;

import com.networknt.schema.ExecutionContext;
import com.networknt.schema.Format;
Expand All @@ -10,13 +11,15 @@
* {@link AbstractFormat} for RFC 3986.
*/
public abstract class AbstractRFC3986Format implements Format {
private static final Pattern VALID = Pattern.compile("([A-Za-z0-9+-\\.]*:)?//|[A-Za-z0-9+-\\.]+:");

@Override
public final boolean matches(ExecutionContext executionContext, String value) {
try {
URI uri = new URI(value);
return validate(uri);
} catch (URISyntaxException e) {
return false;
return handleException(e);
}
}

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

/**
* Determines if the uri matches the format.
*
* @param e the URISyntaxException
* @return false if it does not match
*/
protected boolean handleException(URISyntaxException e) {
if (VALID.matcher(e.getInput()).matches()) {
return true;
}
return false;
}
}
35 changes: 35 additions & 0 deletions src/test/java/com/networknt/schema/format/IriFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,39 @@ void iriShouldPass() {
assertTrue(messages.isEmpty());
}

@Test
void noAuthorityShouldPass() {
String schemaData = "{\r\n"
+ " \"format\": \"iri\"\r\n"
+ "}";

SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
Set<ValidationMessage> messages = schema.validate("\"http://\"", InputFormat.JSON);
assertTrue(messages.isEmpty());
}

@Test
void noSchemeNoAuthorityShouldPass() {
String schemaData = "{\r\n"
+ " \"format\": \"iri\"\r\n"
+ "}";

SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
Set<ValidationMessage> messages = schema.validate("\"//\"", InputFormat.JSON);
assertTrue(messages.isEmpty());
}

@Test
void noPathShouldPass() {
String schemaData = "{\r\n"
+ " \"format\": \"iri\"\r\n"
+ "}";

SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
Set<ValidationMessage> messages = schema.validate("\"about:\"", InputFormat.JSON);
assertTrue(messages.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,40 @@ void iriShouldPass() {
assertTrue(messages.isEmpty());
}

@Test
void noAuthorityShouldPass() {
String schemaData = "{\r\n"
+ " \"format\": \"iri-reference\"\r\n"
+ "}";

SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
Set<ValidationMessage> messages = schema.validate("\"http://\"", InputFormat.JSON);
assertTrue(messages.isEmpty());
}

@Test
void noSchemeNoAuthorityShouldPass() {
String schemaData = "{\r\n"
+ " \"format\": \"iri-reference\"\r\n"
+ "}";

SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
Set<ValidationMessage> messages = schema.validate("\"//\"", InputFormat.JSON);
assertTrue(messages.isEmpty());
}

@Test
void noPathShouldPass() {
String schemaData = "{\r\n"
+ " \"format\": \"iri-reference\"\r\n"
+ "}";

SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
Set<ValidationMessage> messages = schema.validate("\"about:\"", InputFormat.JSON);
assertTrue(messages.isEmpty());
}

}
36 changes: 36 additions & 0 deletions src/test/java/com/networknt/schema/format/UriFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,40 @@ void iriShouldFail() {
InputFormat.JSON);
assertFalse(messages.isEmpty());
}

@Test
void noAuthorityShouldPass() {
String schemaData = "{\r\n"
+ " \"format\": \"uri\"\r\n"
+ "}";

SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
Set<ValidationMessage> messages = schema.validate("\"http://\"", InputFormat.JSON);
assertTrue(messages.isEmpty());
}

@Test
void noSchemeNoAuthorityShouldPass() {
String schemaData = "{\r\n"
+ " \"format\": \"uri\"\r\n"
+ "}";

SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
Set<ValidationMessage> messages = schema.validate("\"//\"", InputFormat.JSON);
assertTrue(messages.isEmpty());
}

@Test
void noPathShouldPass() {
String schemaData = "{\r\n"
+ " \"format\": \"uri\"\r\n"
+ "}";

SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
Set<ValidationMessage> messages = schema.validate("\"about:\"", InputFormat.JSON);
assertTrue(messages.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,39 @@ void iriShouldFail() {
assertFalse(messages.isEmpty());
}

@Test
void noAuthorityShouldPass() {
String schemaData = "{\r\n"
+ " \"format\": \"uri-reference\"\r\n"
+ "}";

SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
Set<ValidationMessage> messages = schema.validate("\"http://\"", InputFormat.JSON);
assertTrue(messages.isEmpty());
}

@Test
void noSchemeNoAuthorityShouldPass() {
String schemaData = "{\r\n"
+ " \"format\": \"uri-reference\"\r\n"
+ "}";

SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
Set<ValidationMessage> messages = schema.validate("\"//\"", InputFormat.JSON);
assertTrue(messages.isEmpty());
}

@Test
void noPathShouldPass() {
String schemaData = "{\r\n"
+ " \"format\": \"uri-reference\"\r\n"
+ "}";

SchemaValidatorsConfig config = SchemaValidatorsConfig.builder().formatAssertionsEnabled(true).build();
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config);
Set<ValidationMessage> messages = schema.validate("\"about:\"", InputFormat.JSON);
assertTrue(messages.isEmpty());
}
}
Loading