diff --git a/src/main/java/com/networknt/schema/format/AbstractRFC3986Format.java b/src/main/java/com/networknt/schema/format/AbstractRFC3986Format.java index 0b0748b5..2500d88e 100644 --- a/src/main/java/com/networknt/schema/format/AbstractRFC3986Format.java +++ b/src/main/java/com/networknt/schema/format/AbstractRFC3986Format.java @@ -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; @@ -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); } } @@ -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; + } } diff --git a/src/test/java/com/networknt/schema/format/IriFormatTest.java b/src/test/java/com/networknt/schema/format/IriFormatTest.java index b371b1b4..17dc5e51 100644 --- a/src/test/java/com/networknt/schema/format/IriFormatTest.java +++ b/src/test/java/com/networknt/schema/format/IriFormatTest.java @@ -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 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 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 messages = schema.validate("\"about:\"", InputFormat.JSON); + assertTrue(messages.isEmpty()); + } } diff --git a/src/test/java/com/networknt/schema/format/IriReferenceFormatTest.java b/src/test/java/com/networknt/schema/format/IriReferenceFormatTest.java index 445937b0..0b5f1d4e 100644 --- a/src/test/java/com/networknt/schema/format/IriReferenceFormatTest.java +++ b/src/test/java/com/networknt/schema/format/IriReferenceFormatTest.java @@ -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 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 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 messages = schema.validate("\"about:\"", InputFormat.JSON); + assertTrue(messages.isEmpty()); + } + } diff --git a/src/test/java/com/networknt/schema/format/UriFormatTest.java b/src/test/java/com/networknt/schema/format/UriFormatTest.java index 6584ef4f..690980e9 100644 --- a/src/test/java/com/networknt/schema/format/UriFormatTest.java +++ b/src/test/java/com/networknt/schema/format/UriFormatTest.java @@ -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 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 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 messages = schema.validate("\"about:\"", InputFormat.JSON); + assertTrue(messages.isEmpty()); + } } diff --git a/src/test/java/com/networknt/schema/format/UriReferenceFormatTest.java b/src/test/java/com/networknt/schema/format/UriReferenceFormatTest.java index 4522d037..bb8ced21 100644 --- a/src/test/java/com/networknt/schema/format/UriReferenceFormatTest.java +++ b/src/test/java/com/networknt/schema/format/UriReferenceFormatTest.java @@ -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 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 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 messages = schema.validate("\"about:\"", InputFormat.JSON); + assertTrue(messages.isEmpty()); + } }