|
15 | 15 | */ |
16 | 16 | package com.networknt.schema; |
17 | 17 |
|
18 | | -import com.fasterxml.jackson.databind.JsonNode; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.Iterator; |
| 20 | +import java.util.LinkedHashSet; |
| 21 | +import java.util.Set; |
| 22 | + |
19 | 23 | import org.slf4j.Logger; |
20 | 24 | import org.slf4j.LoggerFactory; |
21 | 25 |
|
22 | | -import java.util.*; |
23 | | -import java.util.regex.Pattern; |
| 26 | +import com.fasterxml.jackson.databind.JsonNode; |
| 27 | +import com.fasterxml.jackson.databind.node.TextNode; |
24 | 28 |
|
25 | 29 | public class PropertyNamesValidator extends BaseJsonValidator implements JsonValidator { |
26 | 30 | private static final Logger logger = LoggerFactory.getLogger(PropertyNamesValidator.class); |
27 | | - private Map<String, JsonSchema> schemas; |
28 | | - private boolean schemaValue = false; |
| 31 | + private final JsonSchema innerSchema; |
29 | 32 | public PropertyNamesValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { |
30 | 33 | super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.PROPERTYNAMES, validationContext); |
31 | | - if(schemaNode.isBoolean()) { |
32 | | - schemaValue = schemaNode.booleanValue(); |
33 | | - } else { |
34 | | - schemas = new HashMap<String, JsonSchema>(); |
35 | | - for (Iterator<String> it = schemaNode.fieldNames(); it.hasNext(); ) { |
36 | | - String pname = it.next(); |
37 | | - schemas.put(pname, new JsonSchema(validationContext, schemaPath + "/" + pname, parentSchema.getCurrentUri(), schemaNode.get(pname), parentSchema)); |
38 | | - } |
39 | | - } |
| 34 | + innerSchema = new JsonSchema(validationContext, schemaPath, parentSchema.getCurrentUri(), schemaNode, parentSchema); |
40 | 35 | } |
41 | 36 |
|
42 | 37 | public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { |
43 | 38 | debug(logger, node, rootNode, at); |
44 | 39 |
|
45 | 40 | 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 | | - int maxLength = entry.getValue().getSchemaNode().intValue(); |
56 | | - if("maxLength".equals(entry.getKey()) && pname.length() > maxLength) { |
57 | | - errors.add(buildValidationMessage(at + "." + pname, "maxLength " + maxLength)); |
58 | | - } |
59 | | - int minLength = entry.getValue().getSchemaNode().intValue(); |
60 | | - if("minLength".equals(entry.getKey()) && pname.length() < minLength) { |
61 | | - errors.add(buildValidationMessage(at + "." + pname, "minLength " + minLength)); |
62 | | - } |
63 | | - String pattern = entry.getValue().getSchemaNode().textValue(); |
64 | | - if("pattern".equals(entry.getKey()) && !Pattern.matches(pattern,pname)) { |
65 | | - errors.add(buildValidationMessage(at + "." + pname, "pattern " + pattern)); |
66 | | - } |
67 | | - } |
68 | | - } |
69 | | - } else { |
70 | | - if(!schemaValue && node.isObject() && node.size() != 0) { |
71 | | - errors.add(buildValidationMessage(at + "." + node, "false")); |
| 41 | + for (Iterator<String> it = node.fieldNames(); it.hasNext(); ) { |
| 42 | + final String pname = it.next(); |
| 43 | + final TextNode pnameText = TextNode.valueOf(pname); |
| 44 | + final Set<ValidationMessage> schemaErrors = innerSchema.validate(pnameText, node, at + "." + pname); |
| 45 | + for (final ValidationMessage schemaError : schemaErrors) { |
| 46 | + final String path = schemaError.getPath(); |
| 47 | + String msg = schemaError.getMessage(); |
| 48 | + if (msg.startsWith(path)) |
| 49 | + msg = msg.substring(path.length()).replaceFirst("^:\\s*", ""); |
| 50 | + |
| 51 | + errors.add(buildValidationMessage(schemaError.getPath(), msg)); |
72 | 52 | } |
73 | 53 | } |
74 | 54 | return Collections.unmodifiableSet(errors); |
|
0 commit comments