Skip to content

Commit b933e9f

Browse files
authored
Fix unevaluatedPropeties with patternProperties and type union (#582) (#583)
1 parent 9592543 commit b933e9f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/main/java/com/networknt/schema/PatternPropertiesValidator.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
5858
for (Map.Entry<Pattern, JsonSchema> entry : schemas.entrySet()) {
5959
Matcher m = entry.getKey().matcher(name);
6060
if (m.find()) {
61+
addToEvaluatedProperties(at + "." + name);
6162
errors.addAll(entry.getValue().validate(n, rootNode, at + "." + name));
6263
}
6364
}
@@ -69,4 +70,16 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
6970
public void preloadJsonSchema() {
7071
preloadJsonSchemas(schemas.values());
7172
}
73+
74+
private void addToEvaluatedProperties(String propertyPath) {
75+
Object evaluatedProperties = CollectorContext.getInstance().get(UnEvaluatedPropertiesValidator.EVALUATED_PROPERTIES);
76+
List<String> evaluatedPropertiesList = null;
77+
if (evaluatedProperties == null) {
78+
evaluatedPropertiesList = new ArrayList<>();
79+
CollectorContext.getInstance().add(UnEvaluatedPropertiesValidator.EVALUATED_PROPERTIES, evaluatedPropertiesList);
80+
} else {
81+
evaluatedPropertiesList = (List<String>) evaluatedProperties;
82+
}
83+
evaluatedPropertiesList.add(propertyPath);
84+
}
7285
}

src/test/resources/schema/unevaluatedTests/unevaluated-tests.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,5 +2150,48 @@
21502150
"valid": false
21512151
}
21522152
]
2153+
},
2154+
{
2155+
"description": "unevaluatedProperties with patternProperties and type union",
2156+
"schema": {
2157+
"type": "object",
2158+
"patternProperties": {
2159+
"^valid_": {
2160+
"type": ["array", "string"],
2161+
"items": {
2162+
"type": "string"
2163+
}
2164+
}
2165+
},
2166+
"unevaluatedProperties": false
2167+
},
2168+
"tests": [
2169+
{
2170+
"description": "Not valid key against pattern",
2171+
"data": {
2172+
"valid_array": ["array1_value", "array2_value"],
2173+
"valid_string": "string_value",
2174+
"invalid_key": "this is an unevaluated properties due to key not matching the pattern"
2175+
},
2176+
"valid": false
2177+
},
2178+
{
2179+
"description": "Not valid type",
2180+
"data": {
2181+
"valid_array": ["array1_value", "array2_value"],
2182+
"valid_string": "string_value",
2183+
"valid_key": 5
2184+
},
2185+
"valid": false
2186+
},
2187+
{
2188+
"description": "Valid",
2189+
"data": {
2190+
"valid_array": ["array1_value", "array2_value"],
2191+
"valid_string": "string_value"
2192+
},
2193+
"valid": true
2194+
}
2195+
]
21532196
}
21542197
]

0 commit comments

Comments
 (0)