Skip to content

Commit 1abc99b

Browse files
committed
fixes #315 implement propertyNames validator for v6, v7 and v2019-09
1 parent d5a861a commit 1abc99b

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2020 Network New Technologies Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.networknt.schema;
17+
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
22+
import java.util.*;
23+
24+
public class PropertyNamesValidator extends BaseJsonValidator implements JsonValidator {
25+
private static final Logger logger = LoggerFactory.getLogger(PropertyNamesValidator.class);
26+
private Map<String, JsonSchema> schemas;
27+
private boolean schemaValue = false;
28+
public PropertyNamesValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
29+
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.PROPERTYNAMES, validationContext);
30+
if(schemaNode.isBoolean()) {
31+
schemaValue = schemaNode.booleanValue();
32+
} else {
33+
schemas = new HashMap<String, JsonSchema>();
34+
for (Iterator<String> it = schemaNode.fieldNames(); it.hasNext(); ) {
35+
String pname = it.next();
36+
schemas.put(pname, new JsonSchema(validationContext, schemaPath + "/" + pname, parentSchema.getCurrentUri(), schemaNode.get(pname), parentSchema)
37+
.initialize());
38+
}
39+
}
40+
}
41+
42+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
43+
debug(logger, node, rootNode, at);
44+
45+
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
46+
if(schemas != null) {
47+
for (Map.Entry<String, JsonSchema> entry : schemas.entrySet()) {
48+
JsonNode propertyNode = node.get(entry.getKey());
49+
// check propertyNames
50+
if (!node.isObject()) {
51+
continue;
52+
}
53+
for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) {
54+
String pname = it.next();
55+
if("maxLength".equals(entry.getKey()) && pname.length() > entry.getValue().getSchemaNode().intValue()) {
56+
errors.add(buildValidationMessage(at + "." + pname, "maxLength"));
57+
}
58+
if("minLength".equals(entry.getKey()) && pname.length() < entry.getValue().getSchemaNode().intValue()) {
59+
errors.add(buildValidationMessage(at + "." + pname, "minLength"));
60+
}
61+
}
62+
}
63+
} else {
64+
if(!schemaValue && node.isObject() && !node.isEmpty()) {
65+
errors.add(buildValidationMessage(at + "." + node, "false"));
66+
}
67+
}
68+
return Collections.unmodifiableSet(errors);
69+
}
70+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
7272
TRUE("true", "1040", null, TrueValidator.class, 14),
7373
FALSE("false", "1041", new MessageFormat("Boolean schema false is not valid"), FalseValidator.class, 14),
7474
CONST("const", "1042", new MessageFormat("{0}: must be a constant value {1}"), ConstValidator.class, 14),
75-
CONTAINS("contains", "1043", new MessageFormat("{0}: does not contain an element that passes these validations: {1}"), ContainsValidator.class, 14);
75+
CONTAINS("contains", "1043", new MessageFormat("{0}: does not contain an element that passes these validations: {1}"), ContainsValidator.class, 14),
76+
PROPERTYNAMES("propertyNames", "1044", new MessageFormat("Property name {0} is not valid for validation: {1}"), PropertyNamesValidator.class, 14);
7677

7778
private static Map<String, ValidatorTypeCode> constants = new HashMap<String, ValidatorTypeCode>();
7879
private static SpecVersion specVersion = new SpecVersion();

src/test/java/com/networknt/schema/V201909JsonSchemaTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,6 @@ public void testPropertiesValidator() throws Exception {
398398
}
399399

400400
@Test
401-
@Ignore
402401
public void testPropertyNamesValidator() throws Exception {
403402
runTestFile("draft2019-09/propertyNames.json");
404403
}

src/test/java/com/networknt/schema/V6JsonSchemaTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ public void testPropertiesValidator() throws Exception {
290290
}
291291

292292
@Test
293-
@Ignore
294293
public void testPropertyNamesValidator() throws Exception {
295294
runTestFile("draft6/propertyNames.json");
296295
}

src/test/java/com/networknt/schema/V7JsonSchemaTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ public void testPropertiesValidator() throws Exception {
391391
}
392392

393393
@Test
394-
@Ignore
395394
public void testPropertyNamesValidator() throws Exception {
396395
runTestFile("draft7/propertyNames.json");
397396
}

0 commit comments

Comments
 (0)