|
16 | 16 |
|
17 | 17 | package com.networknt.schema;
|
18 | 18 |
|
19 |
| -import com.fasterxml.jackson.databind.JsonNode; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.regex.Pattern; |
| 22 | +import java.util.regex.PatternSyntaxException; |
| 23 | + |
20 | 24 | import org.jcodings.specific.UTF8Encoding;
|
21 | 25 | import org.joni.Option;
|
22 | 26 | import org.joni.Regex;
|
|
25 | 29 | import org.slf4j.Logger;
|
26 | 30 | import org.slf4j.LoggerFactory;
|
27 | 31 |
|
28 |
| -import java.util.Collections; |
29 |
| -import java.util.Set; |
30 |
| -import java.util.regex.Matcher; |
31 |
| -import java.util.regex.Pattern; |
32 |
| -import java.util.regex.PatternSyntaxException; |
| 32 | +import com.fasterxml.jackson.databind.JsonNode; |
33 | 33 |
|
34 |
| -public class PatternValidator extends BaseJsonValidator implements JsonValidator { |
35 |
| - private static final Logger logger = LoggerFactory.getLogger(PatternValidator.class); |
| 34 | +public class PatternValidator implements JsonValidator { |
36 | 35 |
|
37 |
| - private String pattern; |
38 |
| - private Pattern compiledPattern; |
39 |
| - private Regex compiledRegex; |
| 36 | + private final JsonValidator delegate; |
40 | 37 |
|
41 | 38 | public PatternValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
|
| 39 | + if (validationContext.getConfig() != null && validationContext.getConfig().isEcma262Validator()) { |
| 40 | + delegate = new PatternValidatorEcma262(schemaPath, schemaNode, parentSchema, validationContext); |
| 41 | + } else { |
| 42 | + delegate = new PatternValidatorJava(schemaPath, schemaNode, parentSchema, validationContext); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Set<ValidationMessage> validate(JsonNode rootNode) { |
| 48 | + return delegate.validate(rootNode); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { |
| 53 | + return delegate.validate(node, rootNode, at); |
| 54 | + } |
| 55 | + |
| 56 | + private static class PatternValidatorJava extends BaseJsonValidator implements JsonValidator { |
| 57 | + private static final Logger logger = LoggerFactory.getLogger(PatternValidator.class); |
| 58 | + |
| 59 | + private String pattern; |
| 60 | + private Pattern compiledPattern; |
| 61 | + |
| 62 | + public PatternValidatorJava(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { |
| 63 | + |
| 64 | + super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.PATTERN, validationContext); |
| 65 | + pattern = ""; |
| 66 | + if (schemaNode != null && schemaNode.isTextual()) { |
| 67 | + pattern = schemaNode.textValue(); |
| 68 | + try { |
| 69 | + compiledPattern = Pattern.compile(pattern); |
| 70 | + } catch (PatternSyntaxException pse) { |
| 71 | + logger.error("Failed to compile pattern : Invalid syntax [" + pattern + "]", pse); |
| 72 | + throw pse; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + parseErrorCode(getValidatorType().getErrorCodeKey()); |
| 77 | + } |
| 78 | + |
| 79 | + private boolean matches(String value) { |
| 80 | + return compiledPattern == null || compiledPattern.matcher(value).find(); |
| 81 | + } |
| 82 | + |
| 83 | + public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { |
| 84 | + debug(logger, node, rootNode, at); |
| 85 | + |
| 86 | + JsonType nodeType = TypeFactory.getValueNodeType(node); |
| 87 | + if (nodeType != JsonType.STRING) { |
| 88 | + return Collections.emptySet(); |
| 89 | + } |
42 | 90 |
|
43 |
| - super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.PATTERN, validationContext); |
44 |
| - pattern = ""; |
45 |
| - if (schemaNode != null && schemaNode.isTextual()) { |
46 |
| - pattern = schemaNode.textValue(); |
47 | 91 | try {
|
48 |
| - compileRegexPattern(pattern, validationContext.getConfig() != null && validationContext.getConfig().isEcma262Validator()); |
| 92 | + if (!matches(node.asText())) { |
| 93 | + return Collections.singleton(buildValidationMessage(at, pattern)); |
| 94 | + } |
49 | 95 | } catch (PatternSyntaxException pse) {
|
50 |
| - logger.error("Failed to compile pattern : Invalid syntax [" + pattern + "]", pse); |
51 |
| - throw pse; |
52 |
| - } catch (SyntaxException se) { |
53 |
| - logger.error("Failed to compile pattern : Invalid syntax [" + pattern + "]", se); |
54 |
| - throw se; |
| 96 | + logger.error("Failed to apply pattern on " + at + ": Invalid syntax [" + pattern + "]", pse); |
55 | 97 | }
|
56 |
| - } |
57 | 98 |
|
58 |
| - parseErrorCode(getValidatorType().getErrorCodeKey()); |
| 99 | + return Collections.emptySet(); |
| 100 | + } |
59 | 101 | }
|
60 | 102 |
|
61 |
| - private void compileRegexPattern(String regex, boolean useEcma262Validator) { |
62 |
| - if (useEcma262Validator) { |
| 103 | + private static class PatternValidatorEcma262 extends BaseJsonValidator implements JsonValidator { |
| 104 | + private static final Logger logger = LoggerFactory.getLogger(PatternValidator.class); |
| 105 | + |
| 106 | + private String pattern; |
| 107 | + private Regex compiledRegex; |
| 108 | + |
| 109 | + public PatternValidatorEcma262(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { |
| 110 | + |
| 111 | + super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.PATTERN, validationContext); |
| 112 | + pattern = ""; |
| 113 | + if (schemaNode != null && schemaNode.isTextual()) { |
| 114 | + pattern = schemaNode.textValue(); |
| 115 | + try { |
| 116 | + compileRegexPattern(pattern, validationContext.getConfig() != null && validationContext.getConfig().isEcma262Validator()); |
| 117 | + } catch (SyntaxException se) { |
| 118 | + logger.error("Failed to compile pattern : Invalid syntax [" + pattern + "]", se); |
| 119 | + throw se; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + parseErrorCode(getValidatorType().getErrorCodeKey()); |
| 124 | + } |
| 125 | + |
| 126 | + private void compileRegexPattern(String regex, boolean useEcma262Validator) { |
63 | 127 | byte[] regexBytes = regex.getBytes();
|
64 | 128 | this.compiledRegex = new Regex(regexBytes, 0, regexBytes.length, Option.NONE, UTF8Encoding.INSTANCE, Syntax.ECMAScript);
|
65 |
| - } else { |
66 |
| - compiledPattern = Pattern.compile(pattern); |
67 | 129 | }
|
68 |
| - } |
69 | 130 |
|
70 |
| - private boolean matches(String value) { |
71 |
| - if (compiledRegex == null && compiledPattern == null) { |
72 |
| - return true; |
73 |
| - } |
| 131 | + private boolean matches(String value) { |
| 132 | + if (compiledRegex == null) { |
| 133 | + return true; |
| 134 | + } |
74 | 135 |
|
75 |
| - if (compiledPattern == null) { |
76 | 136 | byte[] bytes = value.getBytes();
|
77 | 137 | return compiledRegex.matcher(bytes).search(0, bytes.length, Option.NONE) >= 0;
|
78 |
| - } else { |
79 |
| - return compiledPattern.matcher(value).find(); |
80 | 138 | }
|
81 | 139 |
|
82 |
| - } |
| 140 | + public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { |
| 141 | + debug(logger, node, rootNode, at); |
83 | 142 |
|
84 |
| - public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { |
85 |
| - debug(logger, node, rootNode, at); |
86 |
| - |
87 |
| - JsonType nodeType = TypeFactory.getValueNodeType(node); |
88 |
| - if (nodeType != JsonType.STRING) { |
89 |
| - return Collections.emptySet(); |
90 |
| - } |
| 143 | + JsonType nodeType = TypeFactory.getValueNodeType(node); |
| 144 | + if (nodeType != JsonType.STRING) { |
| 145 | + return Collections.emptySet(); |
| 146 | + } |
91 | 147 |
|
92 |
| - try { |
93 |
| - if (!matches(node.asText())) { |
94 |
| - return Collections.singleton(buildValidationMessage(at, pattern)); |
| 148 | + try { |
| 149 | + if (!matches(node.asText())) { |
| 150 | + return Collections.singleton(buildValidationMessage(at, pattern)); |
| 151 | + } |
| 152 | + } catch (PatternSyntaxException pse) { |
| 153 | + logger.error("Failed to apply pattern on " + at + ": Invalid syntax [" + pattern + "]", pse); |
95 | 154 | }
|
96 |
| - } catch (PatternSyntaxException pse) { |
97 |
| - logger.error("Failed to apply pattern on " + at + ": Invalid syntax [" + pattern + "]", pse); |
98 |
| - } |
99 | 155 |
|
100 |
| - return Collections.emptySet(); |
| 156 | + return Collections.emptySet(); |
| 157 | + } |
101 | 158 | }
|
102 | 159 | }
|
0 commit comments