|
23 | 23 | import org.slf4j.LoggerFactory;
|
24 | 24 |
|
25 | 25 | import java.util.Collections;
|
26 |
| -import java.util.LinkedHashSet; |
27 | 26 | import java.util.Set;
|
28 | 27 | import java.util.regex.PatternSyntaxException;
|
29 | 28 |
|
| 29 | +/** |
| 30 | + * Validator for Format. |
| 31 | + */ |
30 | 32 | public class FormatValidator extends BaseFormatJsonValidator implements JsonValidator {
|
31 | 33 | private static final Logger logger = LoggerFactory.getLogger(FormatValidator.class);
|
32 | 34 |
|
33 | 35 | private final Format format;
|
34 |
| - |
35 |
| - public FormatValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext, Format format, ValidatorTypeCode type) { |
36 |
| - super(schemaLocation, evaluationPath, schemaNode, parentSchema, type, validationContext); |
| 36 | + |
| 37 | + public FormatValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, |
| 38 | + JsonSchema parentSchema, ValidationContext validationContext, Format format, |
| 39 | + ErrorMessageType errorMessageType, Keyword keyword) { |
| 40 | + super(schemaLocation, evaluationPath, schemaNode, parentSchema, errorMessageType, keyword, validationContext); |
37 | 41 | this.format = format;
|
38 | 42 | }
|
39 | 43 |
|
| 44 | + public FormatValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, |
| 45 | + JsonSchema parentSchema, ValidationContext validationContext, Format format, ValidatorTypeCode type) { |
| 46 | + this(schemaLocation, evaluationPath, schemaNode, parentSchema, validationContext, format, type, type); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Gets the annotation value. |
| 51 | + * |
| 52 | + * @return the annotation value |
| 53 | + */ |
| 54 | + protected Object getAnnotationValue() { |
| 55 | + if (this.format != null) { |
| 56 | + return this.format.getName(); |
| 57 | + } |
| 58 | + return this.schemaNode.isTextual() ? schemaNode.textValue() : null; |
| 59 | + } |
| 60 | + |
40 | 61 | public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
|
41 | 62 | debug(logger, node, rootNode, instanceLocation);
|
42 |
| - |
43 |
| - if (format != null) { |
44 |
| - if (collectAnnotations(executionContext)) { |
| 63 | + /* |
| 64 | + * Annotations must be collected even if the format is unknown according to the specification. |
| 65 | + */ |
| 66 | + if (collectAnnotations(executionContext)) { |
| 67 | + Object annotationValue = getAnnotationValue(); |
| 68 | + if (annotationValue != null) { |
45 | 69 | putAnnotation(executionContext,
|
46 |
| - annotation -> annotation.instanceLocation(instanceLocation).value(this.format.getName())); |
| 70 | + annotation -> annotation.instanceLocation(instanceLocation).value(annotationValue)); |
47 | 71 | }
|
48 | 72 | }
|
49 | 73 |
|
50 |
| - JsonType nodeType = TypeFactory.getValueNodeType(node, this.validationContext.getConfig()); |
51 |
| - if (nodeType != JsonType.STRING) { |
52 |
| - return Collections.emptySet(); |
53 |
| - } |
54 |
| - |
55 | 74 | boolean assertionsEnabled = isAssertionsEnabled(executionContext);
|
56 |
| - Set<ValidationMessage> errors = new LinkedHashSet<>(); |
57 |
| - if (format != null) { |
58 |
| - if(format.getName().equals("ipv6")) { |
59 |
| - if(!node.textValue().trim().equals(node.textValue())) { |
60 |
| - if (assertionsEnabled) { |
61 |
| - // leading and trailing spaces |
62 |
| - errors.add(message().instanceNode(node).instanceLocation(instanceLocation) |
63 |
| - .locale(executionContext.getExecutionConfig().getLocale()) |
64 |
| - .failFast(executionContext.isFailFast()) |
65 |
| - .arguments(format.getName(), format.getErrorMessageDescription()).build()); |
66 |
| - } |
67 |
| - } else if(node.textValue().contains("%")) { |
68 |
| - if (assertionsEnabled) { |
69 |
| - // zone id is not part of the ipv6 |
70 |
| - errors.add(message().instanceNode(node).instanceLocation(instanceLocation) |
71 |
| - .locale(executionContext.getExecutionConfig().getLocale()) |
72 |
| - .arguments(format.getName(), format.getErrorMessageDescription()).build()); |
73 |
| - } |
74 |
| - } |
75 |
| - } |
| 75 | + if (this.format != null) { |
76 | 76 | try {
|
77 |
| - if (!format.matches(executionContext, node.textValue())) { |
78 |
| - if (assertionsEnabled) { |
79 |
| - errors.add(message().instanceNode(node).instanceLocation(instanceLocation) |
80 |
| - .locale(executionContext.getExecutionConfig().getLocale()) |
81 |
| - .arguments(format.getName(), format.getErrorMessageDescription()).build()); |
82 |
| - } |
83 |
| - } |
| 77 | + return format.validate(executionContext, validationContext, node, rootNode, instanceLocation, |
| 78 | + assertionsEnabled, |
| 79 | + () -> this.message().instanceNode(node).instanceLocation(instanceLocation) |
| 80 | + .messageKey(format.getMessageKey()) |
| 81 | + .locale(executionContext.getExecutionConfig().getLocale()) |
| 82 | + .failFast(executionContext.isFailFast()), |
| 83 | + this); |
84 | 84 | } catch (PatternSyntaxException pse) {
|
85 | 85 | // String is considered valid if pattern is invalid
|
86 |
| - logger.error("Failed to apply pattern on {}: Invalid RE syntax [{}]", instanceLocation, format.getName(), pse); |
| 86 | + logger.error("Failed to apply pattern on {}: Invalid RE syntax [{}]", instanceLocation, |
| 87 | + format.getName(), pse); |
| 88 | + return Collections.emptySet(); |
87 | 89 | }
|
| 90 | + } else { |
| 91 | + return validateUnknownFormat(executionContext, node, rootNode, instanceLocation); |
88 | 92 | }
|
| 93 | + } |
89 | 94 |
|
90 |
| - return Collections.unmodifiableSet(errors); |
| 95 | + /** |
| 96 | + * When the Format-Assertion vocabulary is specified, implementations MUST fail upon encountering unknown formats. |
| 97 | + * |
| 98 | + * @param executionContext the execution context |
| 99 | + * @param node the node |
| 100 | + * @param rootNode the root node |
| 101 | + * @param instanceLocation the instance location |
| 102 | + * @return the messages |
| 103 | + */ |
| 104 | + protected Set<ValidationMessage> validateUnknownFormat(ExecutionContext executionContext, |
| 105 | + JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) { |
| 106 | + /* |
| 107 | + * Unknown formats should create an assertion if the vocab is specified |
| 108 | + * according to the specification. |
| 109 | + */ |
| 110 | + if (createUnknownFormatAssertions(executionContext) && this.schemaNode.isTextual()) { |
| 111 | + return Collections.singleton(message().instanceLocation(instanceLocation).instanceNode(node) |
| 112 | + .messageKey("format.unknown").arguments(schemaNode.textValue()).build()); |
| 113 | + } |
| 114 | + return Collections.emptySet(); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * When the Format-Assertion vocabulary is specified, implementations MUST fail |
| 119 | + * upon encountering unknown formats. |
| 120 | + * <p> |
| 121 | + * Note that this is different from setting the setFormatAssertionsEnabled |
| 122 | + * configuration option. |
| 123 | + * <p> |
| 124 | + * The following logic will return true if the format assertions option is |
| 125 | + * turned on and strict is enabled (default false) or the format assertion |
| 126 | + * vocabulary is enabled. |
| 127 | + * |
| 128 | + * @param executionContext the execution context |
| 129 | + * @return true if format assertions should be generated |
| 130 | + */ |
| 131 | + protected boolean createUnknownFormatAssertions(ExecutionContext executionContext) { |
| 132 | + return (isAssertionsEnabled(executionContext) && isStrict(executionContext)) || (isFormatAssertionVocabularyEnabled()); |
91 | 133 | }
|
92 | 134 |
|
| 135 | + /** |
| 136 | + * Determines if strict handling. |
| 137 | + * <p> |
| 138 | + * Note that this defaults to false. |
| 139 | + * |
| 140 | + * @param executionContext the execution context |
| 141 | + * @return whether to perform strict handling |
| 142 | + */ |
| 143 | + protected boolean isStrict(ExecutionContext executionContext) { |
| 144 | + return this.validationContext.getConfig().isStrict(getKeyword(), Boolean.FALSE); |
| 145 | + } |
93 | 146 | }
|
0 commit comments