Skip to content

Commit

Permalink
Override default uri format validator
Browse files Browse the repository at this point in the history
  • Loading branch information
testower committed Apr 21, 2022
1 parent 37df69b commit ae1cae2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ protected static Schema loadSchema(String version, String feedName) {
try {
InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("schema/v"+version+"/"+feedName+".json");
JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
return SchemaLoader.load(rawSchema);
SchemaLoader schemaLoader = SchemaLoader.builder()
.enableOverrideOfBuiltInFormatValidators()
.addFormatValidator(new URIFormatValidator())
.schemaJson(rawSchema)
.build();

return schemaLoader.load().build();
} catch (Exception e) {
System.out.println("Caught exception loading schema for " + feedName + " and version " + version);
throw e;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.entur.gbfs.validation.files;

import org.everit.json.schema.FormatValidator;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;

public class URIFormatValidator implements FormatValidator {
@Override
public Optional<String> validate(String subject) {
try {
new URI(subject);
return Optional.empty();
} catch (URISyntaxException e) {
if (e.getReason().equalsIgnoreCase("expected authority")) {
return Optional.empty();
}
}
return Optional.of("Invalid URI");
}

@Override
public String formatName() {
return "uri";
}
}

0 comments on commit ae1cae2

Please sign in to comment.