Closed
Description
I've created several custom formats but they don't seem to be evaluating:
Sample custom format:
public class IpHostPrefixFormat extends AbstractFormat {
public IpHostPrefixFormat() {
super("ip_host_prefix", "must be a valid host IP with prefix length");
}
public IpHostPrefixFormat(String name, String errorMessageDescription) {
super(name, errorMessageDescription);
}
protected IPAddress convertToAddress(String value) {
try {
IPAddressString addressString = new IPAddressString(value);
return addressString.toAddress();
} catch(AddressStringException e) {
return null;
}
}
protected boolean validateAddress(String value, IPAddress address) {
//Ensure full CIDR supplied
if (address.getNetworkPrefixLength() == null) {
return false;
}
//Ensure not a network address
if (address.isPrefixBlock()){
// Exceptions for /31-32 and /127-128
if (address.getIPVersion().isIPv4() && address.getNetworkPrefixLength() < 31) {
return false;
}
if (address.getIPVersion().isIPv6() && address.getNetworkPrefixLength() < 127) {
return false;
}
}
return address.toString().equals(value.toLowerCase());
}
@Override
public boolean matches(String value) {
IPAddress address = convertToAddress(value);
if (address == null) {
return false;
}
return validateAddress(value, address);
}
}
I create my JSONSchema as follows:
private JsonSchema buildSchema(JsonNode schemaJson) {
String schemaUri = schemaJson.get("$schema").asText();
JsonMetaSchema jsonMetaSchema =
new JsonMetaSchema.Builder(schemaUri).addFormats(formats).build();
JsonSchemaFactory jsonSchemaFactory =
new JsonSchemaFactory.Builder().defaultMetaSchemaURI(schemaUri).addMetaSchema(jsonMetaSchema).build();
return jsonSchemaFactory.getSchema(schemaJson);
}
Where formats
includes new IpHostPrefixFormat()
in the list.
If I then test the following schema against the json, it passes validation when it should fail.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"cidr": {
"type": "string",
"format": "ip_host_prefix"
}
},
"required": ["cidr"],
"title": "Sample ip schema",
"type": "object"
}
{
"cidr": "bogus"
}
I wrote test cases for the matches
method on every custom formatter and confirmed they work as expected, it just seems like the schema isn't using them. Am I constructing the Meta schema improperly?
Note that standard validators (e.g. string, number, etc) are all working fine in my tests, it's only the custom formats that do not work.
Metadata
Metadata
Assignees
Labels
No labels