diff --git a/doc/compatibility.md b/doc/compatibility.md
index 60b0fbb3e..556038247 100644
--- a/doc/compatibility.md
+++ b/doc/compatibility.md
@@ -150,7 +150,12 @@ This behavior can be overridden to generate assertions by setting the `setFormat
| uri-template | 🚫 | 🟢 | 🟢 | 🟢 | 🟢 |
| uuid | 🚫 | 🚫 | 🟢 | 🟢 | 🟢 |
+##### Unknown Formats
+
+When the format assertion vocabularies are used in a meta schema, in accordance to the specification, unknown formats will result in assertions. If the format assertion vocabularies are not used, unknown formats will only result in assertions if the assertions are enabled and if `setStrict("format", true)`.
+
##### Footnotes
1. Note that the validation are only optional for some of the keywords/formats.
2. Refer to the corresponding JSON schema for more information on whether the keyword/format is optional or not.
+
diff --git a/src/main/java/com/networknt/schema/Format.java b/src/main/java/com/networknt/schema/Format.java
index 18f80b813..6f79b1021 100644
--- a/src/main/java/com/networknt/schema/Format.java
+++ b/src/main/java/com/networknt/schema/Format.java
@@ -16,23 +16,143 @@
package com.networknt.schema;
+import java.util.Collections;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
/**
* Used to implement the various formats for the format keyword.
+ *
+ * Simple implementations need only override {@link #matches(ExecutionContext, String)}.
*/
public interface Format {
/**
+ * Gets the format name.
+ *
* @return the format name as referred to in a json schema format node.
*/
String getName();
+
+ /**
+ * Gets the message key to use for the message.
+ *
+ * See jsv-messages.properties.
+ *
+ * The following are the arguments.
+ * {0} The instance location
+ * {1} The format name
+ * {2} The error message description
+ * {3} The input value
+ *
+ * @return the message key
+ */
+ default String getMessageKey() {
+ return "format";
+ }
+
+ /**
+ * Gets the error message description.
+ *
+ * Deprecated. Override getMessageKey() and set the localized message in the
+ * resource bundle or message source.
+ *
+ * @return the error message description.
+ */
+ @Deprecated
+ default String getErrorMessageDescription() {
+ return "";
+ }
+
/**
* Determines if the value matches the format.
- *
+ *
+ * This should be implemented for string node types.
+ *
* @param executionContext the execution context
* @param value to match
* @return true if matches
*/
- boolean matches(ExecutionContext executionContext, String value);
+ default boolean matches(ExecutionContext executionContext, String value) {
+ return true;
+ }
+
+ /**
+ * Determines if the value matches the format.
+ *
+ * @param executionContext the execution context
+ * @param validationContext the validation context
+ * @param value to match
+ * @return true if matches
+ */
+ default boolean matches(ExecutionContext executionContext, ValidationContext validationContext, String value) {
+ return matches(executionContext, value);
+ }
+
+ /**
+ * Determines if the value matches the format.
+ *
+ * @param executionContext the execution context
+ * @param validationContext the validation context
+ * @param value to match
+ * @return true if matches
+ */
+ default boolean matches(ExecutionContext executionContext, ValidationContext validationContext, JsonNode value) {
+ JsonType nodeType = TypeFactory.getValueNodeType(value, validationContext.getConfig());
+ if (nodeType != JsonType.STRING) {
+ return true;
+ }
+ return matches(executionContext, validationContext, value.textValue());
+ }
+
+ /**
+ * Determines if the value matches the format.
+ *
+ * This can be implemented for non-string node types.
+ *
+ * @param executionContext the execution context
+ * @param validationContext the validation context
+ * @param node the node
+ * @param rootNode the root node
+ * @param instanceLocation the instance location
+ * @param assertionsEnabled if assertions are enabled
+ * @param formatValidator the format validator
+ * @return true if matches
+ */
+ default boolean matches(ExecutionContext executionContext, ValidationContext validationContext, JsonNode node,
+ JsonNode rootNode, JsonNodePath instanceLocation, boolean assertionsEnabled, FormatValidator formatValidator) {
+ return matches(executionContext, validationContext, node);
+ }
- String getErrorMessageDescription();
+ /**
+ * Validates the format.
+ *
+ * This is the most flexible method to implement.
+ *
+ * @param executionContext the execution context
+ * @param validationContext the validation context
+ * @param node the node
+ * @param rootNode the root node
+ * @param instanceLocation the instance locaiton
+ * @param assertionsEnabled if assertions are enabled
+ * @param message the message builder
+ * @param formatValidator the format validator
+ * @return the messages
+ */
+ default Set validate(ExecutionContext executionContext, ValidationContext validationContext,
+ JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean assertionsEnabled,
+ Supplier message,
+ FormatValidator formatValidator) {
+ if (assertionsEnabled) {
+ if (!matches(executionContext, validationContext, node, rootNode, instanceLocation, assertionsEnabled,
+ formatValidator)) {
+ return Collections
+ .singleton(message.get()
+ .arguments(this.getName(), this.getErrorMessageDescription(), node.asText()).build());
+ }
+ }
+ return Collections.emptySet();
+ }
}
diff --git a/src/main/java/com/networknt/schema/FormatKeyword.java b/src/main/java/com/networknt/schema/FormatKeyword.java
index 214a4f00a..cb2031ffb 100644
--- a/src/main/java/com/networknt/schema/FormatKeyword.java
+++ b/src/main/java/com/networknt/schema/FormatKeyword.java
@@ -17,23 +17,31 @@
package com.networknt.schema;
import com.fasterxml.jackson.databind.JsonNode;
-import com.networknt.schema.format.DateTimeValidator;
-import com.networknt.schema.format.DurationFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
+/**
+ * Format Keyword.
+ */
public class FormatKeyword implements Keyword {
- private static final String DATE_TIME = "date-time";
- private static final String DURATION = "duration";
-
- private final ValidatorTypeCode type;
+ private final String value;
+ private final ErrorMessageType errorMessageType;
private final Map formats;
+
+ public FormatKeyword(Map formats) {
+ this(ValidatorTypeCode.FORMAT, formats);
+ }
public FormatKeyword(ValidatorTypeCode type, Map formats) {
- this.type = type;
+ this(type.getValue(), type, formats);
+ }
+
+ public FormatKeyword(String value, ErrorMessageType errorMessageType, Map formats) {
+ this.value = value;
this.formats = formats;
+ this.errorMessageType = errorMessageType;
}
Collection getFormats() {
@@ -46,27 +54,13 @@ public JsonValidator newValidator(SchemaLocation schemaLocation, JsonNodePath ev
if (schemaNode != null && schemaNode.isTextual()) {
String formatName = schemaNode.textValue();
format = this.formats.get(formatName);
- if (format != null) {
- return new FormatValidator(schemaLocation, evaluationPath, schemaNode, parentSchema, validationContext, format, type);
- }
-
- switch (formatName) {
- case DURATION:
- format = new DurationFormat(validationContext.getConfig().isStrict(DURATION));
- break;
-
- case DATE_TIME: {
- ValidatorTypeCode typeCode = ValidatorTypeCode.DATETIME;
- return new DateTimeValidator(schemaLocation, evaluationPath, schemaNode, parentSchema, validationContext, typeCode);
- }
- }
}
-
- return new FormatValidator(schemaLocation, evaluationPath, schemaNode, parentSchema, validationContext, format, this.type);
+ return new FormatValidator(schemaLocation, evaluationPath, schemaNode, parentSchema, validationContext, format,
+ errorMessageType, this);
}
@Override
public String getValue() {
- return this.type.getValue();
+ return this.value;
}
}
diff --git a/src/main/java/com/networknt/schema/FormatValidator.java b/src/main/java/com/networknt/schema/FormatValidator.java
index 7e588d447..966576246 100644
--- a/src/main/java/com/networknt/schema/FormatValidator.java
+++ b/src/main/java/com/networknt/schema/FormatValidator.java
@@ -23,71 +23,124 @@
import org.slf4j.LoggerFactory;
import java.util.Collections;
-import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.PatternSyntaxException;
+/**
+ * Validator for Format.
+ */
public class FormatValidator extends BaseFormatJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(FormatValidator.class);
private final Format format;
-
- public FormatValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, Format format, ValidatorTypeCode type) {
- super(schemaLocation, evaluationPath, schemaNode, parentSchema, type, validationContext);
+
+ public FormatValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
+ JsonSchema parentSchema, ValidationContext validationContext, Format format,
+ ErrorMessageType errorMessageType, Keyword keyword) {
+ super(schemaLocation, evaluationPath, schemaNode, parentSchema, errorMessageType, keyword, validationContext);
this.format = format;
}
+ public FormatValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode,
+ JsonSchema parentSchema, ValidationContext validationContext, Format format, ValidatorTypeCode type) {
+ this(schemaLocation, evaluationPath, schemaNode, parentSchema, validationContext, format, type, type);
+ }
+
+ /**
+ * Gets the annotation value.
+ *
+ * @return the annotation value
+ */
+ protected Object getAnnotationValue() {
+ if (this.format != null) {
+ return this.format.getName();
+ }
+ return this.schemaNode.isTextual() ? schemaNode.textValue() : null;
+ }
+
public Set validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
debug(logger, node, rootNode, instanceLocation);
-
- if (format != null) {
- if (collectAnnotations(executionContext)) {
+ /*
+ * Annotations must be collected even if the format is unknown according to the specification.
+ */
+ if (collectAnnotations(executionContext)) {
+ Object annotationValue = getAnnotationValue();
+ if (annotationValue != null) {
putAnnotation(executionContext,
- annotation -> annotation.instanceLocation(instanceLocation).value(this.format.getName()));
+ annotation -> annotation.instanceLocation(instanceLocation).value(annotationValue));
}
}
- JsonType nodeType = TypeFactory.getValueNodeType(node, this.validationContext.getConfig());
- if (nodeType != JsonType.STRING) {
- return Collections.emptySet();
- }
-
boolean assertionsEnabled = isAssertionsEnabled(executionContext);
- Set errors = new LinkedHashSet<>();
- if (format != null) {
- if(format.getName().equals("ipv6")) {
- if(!node.textValue().trim().equals(node.textValue())) {
- if (assertionsEnabled) {
- // leading and trailing spaces
- errors.add(message().instanceNode(node).instanceLocation(instanceLocation)
- .locale(executionContext.getExecutionConfig().getLocale())
- .failFast(executionContext.isFailFast())
- .arguments(format.getName(), format.getErrorMessageDescription()).build());
- }
- } else if(node.textValue().contains("%")) {
- if (assertionsEnabled) {
- // zone id is not part of the ipv6
- errors.add(message().instanceNode(node).instanceLocation(instanceLocation)
- .locale(executionContext.getExecutionConfig().getLocale())
- .arguments(format.getName(), format.getErrorMessageDescription()).build());
- }
- }
- }
+ if (this.format != null) {
try {
- if (!format.matches(executionContext, node.textValue())) {
- if (assertionsEnabled) {
- errors.add(message().instanceNode(node).instanceLocation(instanceLocation)
- .locale(executionContext.getExecutionConfig().getLocale())
- .arguments(format.getName(), format.getErrorMessageDescription()).build());
- }
- }
+ return format.validate(executionContext, validationContext, node, rootNode, instanceLocation,
+ assertionsEnabled,
+ () -> this.message().instanceNode(node).instanceLocation(instanceLocation)
+ .messageKey(format.getMessageKey())
+ .locale(executionContext.getExecutionConfig().getLocale())
+ .failFast(executionContext.isFailFast()),
+ this);
} catch (PatternSyntaxException pse) {
// String is considered valid if pattern is invalid
- logger.error("Failed to apply pattern on {}: Invalid RE syntax [{}]", instanceLocation, format.getName(), pse);
+ logger.error("Failed to apply pattern on {}: Invalid RE syntax [{}]", instanceLocation,
+ format.getName(), pse);
+ return Collections.emptySet();
}
+ } else {
+ return validateUnknownFormat(executionContext, node, rootNode, instanceLocation);
}
+ }
- return Collections.unmodifiableSet(errors);
+ /**
+ * When the Format-Assertion vocabulary is specified, implementations MUST fail upon encountering unknown formats.
+ *
+ * @param executionContext the execution context
+ * @param node the node
+ * @param rootNode the root node
+ * @param instanceLocation the instance location
+ * @return the messages
+ */
+ protected Set validateUnknownFormat(ExecutionContext executionContext,
+ JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
+ /*
+ * Unknown formats should create an assertion if the vocab is specified
+ * according to the specification.
+ */
+ if (createUnknownFormatAssertions(executionContext) && this.schemaNode.isTextual()) {
+ return Collections.singleton(message().instanceLocation(instanceLocation).instanceNode(node)
+ .messageKey("format.unknown").arguments(schemaNode.textValue()).build());
+ }
+ return Collections.emptySet();
+ }
+
+ /**
+ * When the Format-Assertion vocabulary is specified, implementations MUST fail
+ * upon encountering unknown formats.
+ *
+ * Note that this is different from setting the setFormatAssertionsEnabled
+ * configuration option.
+ *
+ * The following logic will return true if the format assertions option is
+ * turned on and strict is enabled (default false) or the format assertion
+ * vocabulary is enabled.
+ *
+ * @param executionContext the execution context
+ * @return true if format assertions should be generated
+ */
+ protected boolean createUnknownFormatAssertions(ExecutionContext executionContext) {
+ return (isAssertionsEnabled(executionContext) && isStrict(executionContext)) || (isFormatAssertionVocabularyEnabled());
}
+ /**
+ * Determines if strict handling.
+ *
+ * Note that this defaults to false.
+ *
+ * @param executionContext the execution context
+ * @return whether to perform strict handling
+ */
+ protected boolean isStrict(ExecutionContext executionContext) {
+ return this.validationContext.getConfig().isStrict(getKeyword(), Boolean.FALSE);
+ }
}
diff --git a/src/main/java/com/networknt/schema/JsonMetaSchema.java b/src/main/java/com/networknt/schema/JsonMetaSchema.java
index 6ecb6255e..5fd0464ff 100644
--- a/src/main/java/com/networknt/schema/JsonMetaSchema.java
+++ b/src/main/java/com/networknt/schema/JsonMetaSchema.java
@@ -19,7 +19,10 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.SpecVersion.VersionFlag;
import com.networknt.schema.format.DateFormat;
+import com.networknt.schema.format.DateTimeFormat;
+import com.networknt.schema.format.DurationFormat;
import com.networknt.schema.format.EmailFormat;
+import com.networknt.schema.format.IPv6Format;
import com.networknt.schema.format.IdnEmailFormat;
import com.networknt.schema.format.IdnHostnameFormat;
import com.networknt.schema.format.IriFormat;
@@ -36,13 +39,14 @@
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Consumer;
public class JsonMetaSchema {
private static final Logger logger = LoggerFactory.getLogger(JsonMetaSchema.class);
private static Map UNKNOWN_KEYWORDS = new ConcurrentHashMap<>();
- static PatternFormat pattern(String name, String regex, String description) {
- return new PatternFormat(name, regex, description);
+ static PatternFormat pattern(String name, String regex, String messageKey) {
+ return PatternFormat.of(name, regex, messageKey);
}
static PatternFormat pattern(String name, String regex) {
@@ -51,18 +55,17 @@ static PatternFormat pattern(String name, String regex) {
public static final List COMMON_BUILTIN_FORMATS = new ArrayList<>();
- public static final String IPV6_PATTERN = "^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$";
-
// this section contains formats common to all dialects.
static {
- COMMON_BUILTIN_FORMATS.add(pattern("hostname", "^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*$", "must be a valid RFC 1123 host name"));
- COMMON_BUILTIN_FORMATS.add(pattern("ipv4", "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$", "must be a valid RFC 2673 IP address"));
- COMMON_BUILTIN_FORMATS.add(pattern("ipv6", IPV6_PATTERN, "must be a valid RFC 4291 IP address"));
- COMMON_BUILTIN_FORMATS.add(pattern("json-pointer", "^(/([^/#~]|[~](?=[01]))*)*$", "must be a valid RFC 6901 JSON Pointer"));
- COMMON_BUILTIN_FORMATS.add(pattern("relative-json-pointer", "^(0|([1-9]\\d*))(#|(/([^/#~]|[~](?=[01]))*)*)$", "must be a valid IETF Relative JSON Pointer"));
- COMMON_BUILTIN_FORMATS.add(pattern("uri-template", "^([^\\p{Cntrl}\"'%<>\\^`\\{|\\}]|%\\p{XDigit}{2}|\\{[+#./;?&=,!@|]?((\\w|%\\p{XDigit}{2})(\\.?(\\w|%\\p{XDigit}{2}))*(:[1-9]\\d{0,3}|\\*)?)(,((\\w|%\\p{XDigit}{2})(\\.?(\\w|%\\p{XDigit}{2}))*(:[1-9]\\d{0,3}|\\*)?))*\\})*$", "must be a valid RFC 6570 URI Template"));
- COMMON_BUILTIN_FORMATS.add(pattern("uuid", "^\\p{XDigit}{8}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{12}$", "must be a valid RFC 4122 UUID"));
+ COMMON_BUILTIN_FORMATS.add(pattern("hostname", "^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*$", "format.hostname"));
+ COMMON_BUILTIN_FORMATS.add(pattern("ipv4", "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$", "format.ipv4"));
+ COMMON_BUILTIN_FORMATS.add(new IPv6Format());
+ COMMON_BUILTIN_FORMATS.add(pattern("json-pointer", "^(/([^/#~]|[~](?=[01]))*)*$", "format.json-pointer"));
+ COMMON_BUILTIN_FORMATS.add(pattern("relative-json-pointer", "^(0|([1-9]\\d*))(#|(/([^/#~]|[~](?=[01]))*)*)$", "format.relative-json-pointer"));
+ COMMON_BUILTIN_FORMATS.add(pattern("uri-template", "^([^\\p{Cntrl}\"'%<>\\^`\\{|\\}]|%\\p{XDigit}{2}|\\{[+#./;?&=,!@|]?((\\w|%\\p{XDigit}{2})(\\.?(\\w|%\\p{XDigit}{2}))*(:[1-9]\\d{0,3}|\\*)?)(,((\\w|%\\p{XDigit}{2})(\\.?(\\w|%\\p{XDigit}{2}))*(:[1-9]\\d{0,3}|\\*)?))*\\})*$", "format.uri-template"));
+ COMMON_BUILTIN_FORMATS.add(pattern("uuid", "^\\p{XDigit}{8}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{12}$", "format.uuid"));
COMMON_BUILTIN_FORMATS.add(new DateFormat());
+ COMMON_BUILTIN_FORMATS.add(new DateTimeFormat());
COMMON_BUILTIN_FORMATS.add(new EmailFormat());
COMMON_BUILTIN_FORMATS.add(new IdnEmailFormat());
COMMON_BUILTIN_FORMATS.add(new IdnHostnameFormat());
@@ -72,7 +75,8 @@ static PatternFormat pattern(String name, String regex) {
COMMON_BUILTIN_FORMATS.add(new TimeFormat());
COMMON_BUILTIN_FORMATS.add(new UriFormat());
COMMON_BUILTIN_FORMATS.add(new UriReferenceFormat());
-
+ COMMON_BUILTIN_FORMATS.add(new DurationFormat());
+
// The following formats do not appear in any draft
COMMON_BUILTIN_FORMATS.add(pattern("alpha", "^[a-zA-Z]+$"));
COMMON_BUILTIN_FORMATS.add(pattern("alphanumeric", "^[a-zA-Z0-9]+$"));
@@ -82,6 +86,10 @@ static PatternFormat pattern(String name, String regex) {
COMMON_BUILTIN_FORMATS.add(pattern("style", "\\s*(.+?):\\s*([^;]+);?"));
COMMON_BUILTIN_FORMATS.add(pattern("utc-millisec", "^[0-9]+(\\.?[0-9]+)?$"));
}
+
+ public interface FormatKeywordFactory {
+ FormatKeyword newInstance(Map formats);
+ }
public static class Builder {
private VersionFlag specification = null;
@@ -90,30 +98,48 @@ public static class Builder {
private Map vocabularies = new HashMap<>();
private String uri;
private String idKeyword = "id";
+ private FormatKeywordFactory formatKeywordFactory = null;
public Builder(String uri) {
this.uri = uri;
}
- private static Map createKeywordsMap(Map kwords, Map formats) {
+ private Map createKeywordsMap(Map kwords, Map formats) {
Map map = new HashMap<>();
for (Map.Entry type : kwords.entrySet()) {
String keywordName = type.getKey();
Keyword keyword = type.getValue();
if (ValidatorTypeCode.FORMAT.getValue().equals(keywordName)) {
if (!(keyword instanceof FormatKeyword)) {
- throw new IllegalArgumentException("Overriding the keyword 'format' is not supported");
+ throw new IllegalArgumentException("Overriding the keyword 'format' is not supported. Use the formatKeywordFactory and extend the FormatKeyword.");
}
// ignore - format keyword will be created again below.
} else {
map.put(keyword.getValue(), keyword);
}
}
- final FormatKeyword formatKeyword = new FormatKeyword(ValidatorTypeCode.FORMAT, formats);
+ final FormatKeyword formatKeyword = formatKeywordFactory != null ? formatKeywordFactory.newInstance(formats)
+ : new FormatKeyword(formats);
map.put(formatKeyword.getValue(), formatKeyword);
return map;
}
+ public Builder formatKeywordFactory(FormatKeywordFactory formatKeywordFactory) {
+ this.formatKeywordFactory = formatKeywordFactory;
+ return this;
+ }
+
+ public Builder formats(Consumer