2020import org .apache .commons .lang .StringUtils ;
2121import org .slf4j .Logger ;
2222
23+ import java .net .MalformedURLException ;
24+ import java .net .URL ;
2325import java .util .Set ;
2426
2527public abstract class BaseJsonValidator implements JsonValidator {
2628 private String schemaPath ;
2729 private JsonNode schemaNode ;
2830 private JsonSchema parentSchema ;
31+ private JsonSchema subSchema ;
2932 private ValidatorTypeCode validatorType ;
3033 private String errorCode ;
3134
@@ -35,6 +38,7 @@ public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema pare
3538 this .schemaNode = schemaNode ;
3639 this .parentSchema = parentSchema ;
3740 this .validatorType = validatorType ;
41+ this .subSchema = obainSubSchemaNode (schemaNode );
3842 }
3943
4044 protected String getSchemaPath () {
@@ -48,6 +52,28 @@ protected JsonNode getSchemaNode() {
4852 protected JsonSchema getParentSchema () {
4953 return parentSchema ;
5054 }
55+
56+ protected JsonSchema getSubSchema () {
57+ return subSchema ;
58+ }
59+
60+ protected boolean hasSubSchema () {
61+ return subSchema != null ;
62+ }
63+
64+ protected JsonSchema obainSubSchemaNode (JsonNode schemaNode ){
65+ JsonNode node = schemaNode .get ("id" );
66+ if (node == null ) return null ;
67+ if (node .equals (schemaNode .get ("$schema" ))) 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+ }
5177
5278 public Set <ValidationMessage > validate (JsonNode node ) {
5379 return validate (node , node , AT_ROOT );
0 commit comments