Skip to content

Commit 60917a8

Browse files
LeifRilbeleif.rilbe
andauthored
#326 property names with pattern (#374)
* #326: Pattern validation for propertyNames * #326: Adjust test case descriptions. Co-authored-by: leif.rilbe <[email protected]>
1 parent a5f9f7f commit 60917a8

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.slf4j.LoggerFactory;
2121

2222
import java.util.*;
23+
import java.util.regex.Pattern;
2324

2425
public class PropertyNamesValidator extends BaseJsonValidator implements JsonValidator {
2526
private static final Logger logger = LoggerFactory.getLogger(PropertyNamesValidator.class);
@@ -58,6 +59,9 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
5859
if("minLength".equals(entry.getKey()) && pname.length() < entry.getValue().getSchemaNode().intValue()) {
5960
errors.add(buildValidationMessage(at + "." + pname, "minLength"));
6061
}
62+
if("pattern".equals(entry.getKey()) && !Pattern.matches(entry.getValue().getSchemaNode().textValue(),pname)) {
63+
errors.add(buildValidationMessage(at + "." + pname, "pattern"));
64+
}
6165
}
6266
}
6367
} else {

src/test/resources/draft2019-09/propertyNames.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,35 @@
5050
}
5151
]
5252
},
53+
{
54+
"description": "propertyNames validation with pattern",
55+
"schema": {
56+
"propertyNames": { "pattern": "^a+$" }
57+
},
58+
"tests": [
59+
{
60+
"description": "matching property names are valid",
61+
"data": {
62+
"a": {},
63+
"aa": {},
64+
"aaa": {}
65+
},
66+
"valid": true
67+
},
68+
{
69+
"description": "non-matching property name is invalid",
70+
"data": {
71+
"aaA": {}
72+
},
73+
"valid": false
74+
},
75+
{
76+
"description": "object without properties is valid",
77+
"data": {},
78+
"valid": true
79+
}
80+
]
81+
},
5382
{
5483
"description": "propertyNames with boolean schema true",
5584
"schema": {

src/test/resources/draft6/propertyNames.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,35 @@
5050
}
5151
]
5252
},
53+
{
54+
"description": "propertyNames validation with pattern",
55+
"schema": {
56+
"propertyNames": { "pattern": "^a+$" }
57+
},
58+
"tests": [
59+
{
60+
"description": "matching property names are valid",
61+
"data": {
62+
"a": {},
63+
"aa": {},
64+
"aaa": {}
65+
},
66+
"valid": true
67+
},
68+
{
69+
"description": "non-matching property name is invalid",
70+
"data": {
71+
"aaA": {}
72+
},
73+
"valid": false
74+
},
75+
{
76+
"description": "object without properties is valid",
77+
"data": {},
78+
"valid": true
79+
}
80+
]
81+
},
5382
{
5483
"description": "propertyNames with boolean schema true",
5584
"schema": {

src/test/resources/draft7/propertyNames.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,35 @@
5050
}
5151
]
5252
},
53+
{
54+
"description": "propertyNames validation with pattern",
55+
"schema": {
56+
"propertyNames": { "pattern": "^a+$" }
57+
},
58+
"tests": [
59+
{
60+
"description": "matching property names are valid",
61+
"data": {
62+
"a": {},
63+
"aa": {},
64+
"aaa": {}
65+
},
66+
"valid": true
67+
},
68+
{
69+
"description": "non-matching property name is invalid",
70+
"data": {
71+
"aaA": {}
72+
},
73+
"valid": false
74+
},
75+
{
76+
"description": "object without properties is valid",
77+
"data": {},
78+
"valid": true
79+
}
80+
]
81+
},
5382
{
5483
"description": "propertyNames with boolean schema true",
5584
"schema": {

0 commit comments

Comments
 (0)