@@ -69,57 +69,61 @@ public BaseJsonValidator(String schemaPath,
69
69
}
70
70
71
71
public String getSchemaPath () {
72
- return schemaPath ;
72
+ return this . schemaPath ;
73
73
}
74
74
75
75
public JsonNode getSchemaNode () {
76
- return schemaNode ;
76
+ return this . schemaNode ;
77
77
}
78
78
79
79
public JsonSchema getParentSchema () {
80
- return parentSchema ;
80
+ return this . parentSchema ;
81
81
}
82
82
83
83
protected JsonSchema fetchSubSchemaNode (ValidationContext validationContext ) {
84
- return suppressSubSchemaRetrieval ? null : obtainSubSchemaNode (schemaNode , validationContext );
84
+ return this . suppressSubSchemaRetrieval ? null : obtainSubSchemaNode (this . schemaNode , validationContext );
85
85
}
86
86
87
87
private static JsonSchema obtainSubSchemaNode (final JsonNode schemaNode , final ValidationContext validationContext ) {
88
88
final JsonNode node = schemaNode .get ("id" );
89
+
89
90
if (node == null ) {
90
91
return null ;
91
92
}
93
+
92
94
if (node .equals (schemaNode .get ("$schema" ))) {
93
95
return null ;
94
96
}
95
97
96
98
final String text = node .textValue ();
97
99
if (text == null ) {
98
100
return null ;
99
- } else {
100
- final URI uri ;
101
- try {
102
- uri = validationContext .getURIFactory ().create (node .textValue ());
103
- } catch (IllegalArgumentException e ) {
104
- return null ;
105
- }
106
- return validationContext .getJsonSchemaFactory ().getSchema (uri , validationContext .getConfig ());
107
101
}
102
+
103
+ final URI uri ;
104
+ try {
105
+ uri = validationContext .getURIFactory ().create (node .textValue ());
106
+ } catch (IllegalArgumentException e ) {
107
+ return null ;
108
+ }
109
+
110
+ return validationContext .getJsonSchemaFactory ().getSchema (uri , validationContext .getConfig ());
108
111
}
109
112
113
+ @ Override
110
114
public Set <ValidationMessage > validate (JsonNode node ) {
111
115
return validate (node , node , atRoot ());
112
116
}
113
117
114
- protected boolean equals (double n1 , double n2 ) {
118
+ protected static boolean equals (double n1 , double n2 ) {
115
119
return Math .abs (n1 - n2 ) < 1e-12 ;
116
120
}
117
121
118
- protected boolean greaterThan (double n1 , double n2 ) {
122
+ protected static boolean greaterThan (double n1 , double n2 ) {
119
123
return n1 - n2 > 1e-12 ;
120
124
}
121
125
122
- protected boolean lessThan (double n1 , double n2 ) {
126
+ protected static boolean lessThan (double n1 , double n2 ) {
123
127
return n1 - n2 < -1e-12 ;
124
128
}
125
129
@@ -128,52 +132,52 @@ protected void parseErrorCode(String errorCodeKey) {
128
132
if (errorCodeNode != null && errorCodeNode .isTextual ()) {
129
133
String errorCodeText = errorCodeNode .asText ();
130
134
if (StringUtils .isNotBlank (errorCodeText )) {
131
- errorMessageType = CustomErrorMessageType .of (errorCodeText );
135
+ this . errorMessageType = CustomErrorMessageType .of (errorCodeText );
132
136
}
133
137
}
134
138
}
135
139
136
140
protected ValidationMessage buildValidationMessage (String at , String ... arguments ) {
137
- MessageFormat messageFormat = new MessageFormat (resourceBundle .getString (getErrorMessageType ().getErrorCodeValue ()));
138
- final ValidationMessage message = ValidationMessage .ofWithCustom (getValidatorType ().getValue (), getErrorMessageType (), messageFormat , customMessage , at , schemaPath , arguments );
139
- if (failFast && !isPartOfOneOfMultipleType ()) {
141
+ MessageFormat messageFormat = new MessageFormat (this . resourceBundle .getString (getErrorMessageType ().getErrorCodeValue ()));
142
+ final ValidationMessage message = ValidationMessage .ofWithCustom (getValidatorType ().getValue (), getErrorMessageType (), messageFormat , this . customMessage , at , this . schemaPath , arguments );
143
+ if (this . failFast && !isApplicator ()) {
140
144
throw new JsonSchemaException (message );
141
145
}
142
146
return message ;
143
147
}
144
148
145
149
protected ValidationMessage constructValidationMessage (String messageKey , String at , String ... arguments ) {
146
- MessageFormat messageFormat = new MessageFormat (resourceBundle .getString (messageKey ));
150
+ MessageFormat messageFormat = new MessageFormat (this . resourceBundle .getString (messageKey ));
147
151
final ValidationMessage message = new ValidationMessage .Builder ()
148
152
.code (getErrorMessageType ().getErrorCode ())
149
153
.path (at )
150
- .schemaPath (schemaPath )
154
+ .schemaPath (this . schemaPath )
151
155
.arguments (arguments )
152
156
.format (messageFormat )
153
157
.type (getValidatorType ().getValue ())
154
- .customMessage (customMessage )
158
+ .customMessage (this . customMessage )
155
159
.build ();
156
- if (failFast && !isPartOfOneOfMultipleType ()) {
160
+ if (this . failFast && !isApplicator ()) {
157
161
throw new JsonSchemaException (message );
158
162
}
159
163
return message ;
160
164
}
161
165
162
- protected void debug (Logger logger , JsonNode node , JsonNode rootNode , String at ) {
166
+ protected static void debug (Logger logger , JsonNode node , JsonNode rootNode , String at ) {
163
167
logger .debug ("validate( {}, {}, {})" , node , rootNode , at );
164
168
}
165
169
166
170
protected ValidatorTypeCode getValidatorType () {
167
- return validatorType ;
171
+ return this . validatorType ;
168
172
}
169
173
170
174
protected ErrorMessageType getErrorMessageType () {
171
- return errorMessageType ;
175
+ return this . errorMessageType ;
172
176
}
173
177
174
178
protected void updateValidatorType (ValidatorTypeCode validatorTypeCode ) {
175
- validatorType = validatorTypeCode ;
176
- errorMessageType = validatorTypeCode ;
179
+ this . validatorType = validatorTypeCode ;
180
+ this . errorMessageType = validatorTypeCode ;
177
181
parseErrorCode (validatorTypeCode .getErrorCodeKey ());
178
182
}
179
183
@@ -191,7 +195,7 @@ protected String getNodeFieldType() {
191
195
*/
192
196
@ Override
193
197
public Set <ValidationMessage > walk (JsonNode node , JsonNode rootNode , String at , boolean shouldValidateSchema ) {
194
- Set <ValidationMessage > validationMessages = new LinkedHashSet <ValidationMessage >();
198
+ Set <ValidationMessage > validationMessages = new LinkedHashSet <>();
195
199
if (shouldValidateSchema ) {
196
200
validationMessages = validate (node , rootNode , at );
197
201
}
@@ -204,12 +208,32 @@ protected void preloadJsonSchemas(final Collection<JsonSchema> schemas) {
204
208
}
205
209
}
206
210
211
+ private boolean isApplicator () {
212
+ return false
213
+ || isPartOfAnyOfMultipleType ()
214
+ || isPartOfIfMultipleType ()
215
+ || isPartOfNotMultipleType ()
216
+ || isPartOfOneOfMultipleType ();
217
+ }
218
+
219
+ private boolean isPartOfAnyOfMultipleType () {
220
+ return this .parentSchema .schemaPath .contains ("/" + ValidatorTypeCode .ANY_OF .getValue () + "/" );
221
+ }
222
+
223
+ private boolean isPartOfIfMultipleType () {
224
+ return this .parentSchema .schemaPath .contains ("/" + ValidatorTypeCode .IF_THEN_ELSE .getValue () + "/" );
225
+ }
226
+
227
+ private boolean isPartOfNotMultipleType () {
228
+ return this .parentSchema .schemaPath .contains ("/" + ValidatorTypeCode .NOT .getValue () + "/" );
229
+ }
230
+
207
231
protected boolean isPartOfOneOfMultipleType () {
208
- return parentSchema .schemaPath .contains ("/" + ValidatorTypeCode .ONE_OF .getValue () + "/" );
232
+ return this . parentSchema .schemaPath .contains ("/" + ValidatorTypeCode .ONE_OF .getValue () + "/" );
209
233
}
210
234
211
235
protected PathType getPathType () {
212
- return pathType ;
236
+ return this . pathType ;
213
237
}
214
238
215
239
/**
@@ -218,7 +242,7 @@ protected PathType getPathType() {
218
242
* @return The path.
219
243
*/
220
244
protected String atRoot () {
221
- return pathType .getRoot ();
245
+ return this . pathType .getRoot ();
222
246
}
223
247
224
248
/**
@@ -229,7 +253,7 @@ protected String atRoot() {
229
253
* @return The complete path.
230
254
*/
231
255
protected String atPath (String currentPath , String token ) {
232
- return pathType .append (currentPath , token );
256
+ return this . pathType .append (currentPath , token );
233
257
}
234
258
235
259
/**
@@ -240,7 +264,7 @@ protected String atPath(String currentPath, String token) {
240
264
* @return The complete path.
241
265
*/
242
266
protected String atPath (String currentPath , int index ) {
243
- return pathType .append (currentPath , index );
267
+ return this . pathType .append (currentPath , index );
244
268
}
245
269
246
270
/* ********************** START OF OpenAPI 3.0.x DISCRIMINATOR METHODS ********************************* */
0 commit comments