Skip to content

Commit

Permalink
Merge branch 'master' of github.com:networknt/json-schema-validator
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehu committed Jun 21, 2024
2 parents b97ab80 + b93a66e commit d07f80f
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 1 deletion.
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());
}
}

0 comments on commit d07f80f

Please sign in to comment.