20
20
import org .apache .commons .lang .StringUtils ;
21
21
import org .slf4j .Logger ;
22
22
23
+ import java .net .MalformedURLException ;
24
+ import java .net .URL ;
23
25
import java .util .Set ;
24
26
25
27
public abstract class BaseJsonValidator implements JsonValidator {
26
28
private String schemaPath ;
27
29
private JsonNode schemaNode ;
28
30
private JsonSchema parentSchema ;
31
+ private JsonSchema subSchema ;
29
32
private ValidatorTypeCode validatorType ;
30
33
private String errorCode ;
31
34
@@ -35,6 +38,7 @@ public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema pare
35
38
this .schemaNode = schemaNode ;
36
39
this .parentSchema = parentSchema ;
37
40
this .validatorType = validatorType ;
41
+ this .subSchema = obainSubSchemaNode (schemaNode );
38
42
}
39
43
40
44
protected String getSchemaPath () {
@@ -48,6 +52,28 @@ protected JsonNode getSchemaNode() {
48
52
protected JsonSchema getParentSchema () {
49
53
return parentSchema ;
50
54
}
55
+
56
+ protected JsonSchema getSubSchema () {
57
+ return subSchema ;
58
+ }
59
+
60
+ protected boolean hasSubSchema () {
61
+ return subSchema != null ;
62
+ }
63
+
64
+
65
+ protected JsonSchema obainSubSchemaNode (JsonNode schemaNode ){
66
+ JsonNode node = schemaNode .get ("id" );
67
+ if (node == null ) return null ;
68
+
69
+ try {
70
+ JsonSchemaFactory factory = new JsonSchemaFactory ();
71
+ URL url = new URL (node .textValue ());
72
+ return factory .getSchema (url );
73
+ } catch (MalformedURLException e ) {
74
+ return null ;
75
+ }
76
+ }
51
77
52
78
public Set <ValidationMessage > validate (JsonNode node ) {
53
79
return validate (node , node , AT_ROOT );
0 commit comments