26
26
27
27
import java .util .*;
28
28
29
- public class PropertiesValidator extends BaseJsonValidator implements JsonValidator {
29
+ public class PropertiesValidator extends BaseJsonValidator {
30
30
public static final String PROPERTY = "properties" ;
31
31
private static final Logger logger = LoggerFactory .getLogger (PropertiesValidator .class );
32
32
private final Map <String , JsonSchema > schemas = new LinkedHashMap <>();
@@ -36,22 +36,23 @@ public PropertiesValidator(String schemaPath, JsonNode schemaNode, JsonSchema pa
36
36
this .validationContext = validationContext ;
37
37
for (Iterator <String > it = schemaNode .fieldNames (); it .hasNext (); ) {
38
38
String pname = it .next ();
39
- schemas .put (pname , validationContext .newSchema (schemaPath + "/" + pname , schemaNode .get (pname ), parentSchema ));
39
+ this . schemas .put (pname , validationContext .newSchema (schemaPath + "/" + pname , schemaNode .get (pname ), parentSchema ));
40
40
}
41
41
}
42
42
43
+ @ Override
43
44
public Set <ValidationMessage > validate (JsonNode node , JsonNode rootNode , String at ) {
44
45
debug (logger , node , rootNode , at );
45
46
CollectorContext collectorContext = CollectorContext .getInstance ();
46
47
47
48
WalkListenerRunner propertyWalkListenerRunner = new DefaultPropertyWalkListenerRunner (this .validationContext .getConfig ().getPropertyWalkListeners ());
48
49
49
- Set <ValidationMessage > errors = new LinkedHashSet <ValidationMessage >();
50
+ Set <ValidationMessage > errors = new LinkedHashSet <>();
50
51
51
52
// get the Validator state object storing validation data
52
53
ValidatorState state = (ValidatorState ) collectorContext .get (ValidatorState .VALIDATOR_STATE_KEY );
53
54
54
- for (Map .Entry <String , JsonSchema > entry : schemas .entrySet ()) {
55
+ for (Map .Entry <String , JsonSchema > entry : this . schemas .entrySet ()) {
55
56
JsonSchema propertySchema = entry .getValue ();
56
57
JsonNode propertyNode = node .get (entry .getKey ());
57
58
if (propertyNode != null ) {
@@ -90,9 +91,8 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
90
91
// this was a complex validator (ex oneOf) and the node has not been matched
91
92
state .setMatchedNode (false );
92
93
return Collections .emptySet ();
93
- } else {
94
- errors .addAll (requiredErrors );
95
94
}
95
+ errors .addAll (requiredErrors );
96
96
}
97
97
}
98
98
}
@@ -103,38 +103,38 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
103
103
104
104
@ Override
105
105
public Set <ValidationMessage > walk (JsonNode node , JsonNode rootNode , String at , boolean shouldValidateSchema ) {
106
- HashSet <ValidationMessage > validationMessages = new LinkedHashSet <ValidationMessage >();
107
- if (applyDefaultsStrategy .shouldApplyPropertyDefaults () && node .getNodeType () == JsonNodeType .OBJECT ) {
106
+ HashSet <ValidationMessage > validationMessages = new LinkedHashSet <>();
107
+ if (this . applyDefaultsStrategy .shouldApplyPropertyDefaults () && null != node && node .getNodeType () == JsonNodeType .OBJECT ) {
108
108
applyPropertyDefaults ((ObjectNode ) node );
109
109
}
110
110
if (shouldValidateSchema ) {
111
111
validationMessages .addAll (validate (node , rootNode , at ));
112
112
} else {
113
113
WalkListenerRunner propertyWalkListenerRunner = new DefaultPropertyWalkListenerRunner (this .validationContext .getConfig ().getPropertyWalkListeners ());
114
- for (Map .Entry <String , JsonSchema > entry : schemas .entrySet ()) {
114
+ for (Map .Entry <String , JsonSchema > entry : this . schemas .entrySet ()) {
115
115
walkSchema (entry , node , rootNode , at , shouldValidateSchema , validationMessages , propertyWalkListenerRunner );
116
116
}
117
117
}
118
118
return validationMessages ;
119
119
}
120
120
121
121
private void applyPropertyDefaults (ObjectNode node ) {
122
- for (Map .Entry <String , JsonSchema > entry : schemas .entrySet ()) {
122
+ for (Map .Entry <String , JsonSchema > entry : this . schemas .entrySet ()) {
123
123
JsonNode propertyNode = node .get (entry .getKey ());
124
124
125
125
JsonNode defaultNode = getDefaultNode (entry );
126
126
if (defaultNode == null ) {
127
127
continue ;
128
128
}
129
129
boolean applyDefault = propertyNode == null
130
- || (propertyNode .isNull () && applyDefaultsStrategy .shouldApplyPropertyDefaultsIfNull ());
130
+ || (propertyNode .isNull () && this . applyDefaultsStrategy .shouldApplyPropertyDefaultsIfNull ());
131
131
if (applyDefault ) {
132
132
node .set (entry .getKey (), defaultNode );
133
133
}
134
134
}
135
135
}
136
136
137
- private JsonNode getDefaultNode (final Map .Entry <String , JsonSchema > entry ) {
137
+ private static JsonNode getDefaultNode (final Map .Entry <String , JsonSchema > entry ) {
138
138
JsonSchema propertySchema = entry .getValue ();
139
139
return propertySchema .getSchemaNode ().get ("default" );
140
140
}
@@ -145,25 +145,25 @@ private void walkSchema(Map.Entry<String, JsonSchema> entry, JsonNode node, Json
145
145
JsonNode propertyNode = (node == null ? null : node .get (entry .getKey ()));
146
146
boolean executeWalk = propertyWalkListenerRunner .runPreWalkListeners (ValidatorTypeCode .PROPERTIES .getValue (),
147
147
propertyNode , rootNode , atPath (at , entry .getKey ()), propertySchema .getSchemaPath (),
148
- propertySchema .getSchemaNode (), propertySchema .getParentSchema (), validationContext ,
149
- validationContext .getJsonSchemaFactory ());
148
+ propertySchema .getSchemaNode (), propertySchema .getParentSchema (), this . validationContext ,
149
+ this . validationContext .getJsonSchemaFactory ());
150
150
if (executeWalk ) {
151
151
validationMessages .addAll (
152
152
propertySchema .walk (propertyNode , rootNode , atPath (at , entry .getKey ()), shouldValidateSchema ));
153
153
}
154
154
propertyWalkListenerRunner .runPostWalkListeners (ValidatorTypeCode .PROPERTIES .getValue (), propertyNode , rootNode ,
155
155
atPath (at , entry .getKey ()), propertySchema .getSchemaPath (), propertySchema .getSchemaNode (),
156
- propertySchema .getParentSchema (), validationContext , validationContext .getJsonSchemaFactory (), validationMessages );
156
+ propertySchema .getParentSchema (), this . validationContext , this . validationContext .getJsonSchemaFactory (), validationMessages );
157
157
158
158
}
159
159
160
160
public Map <String , JsonSchema > getSchemas () {
161
- return schemas ;
161
+ return this . schemas ;
162
162
}
163
163
164
164
165
165
@ Override
166
166
public void preloadJsonSchema () {
167
- preloadJsonSchemas (schemas .values ());
167
+ preloadJsonSchemas (this . schemas .values ());
168
168
}
169
169
}
0 commit comments