Skip to content

Commit 3d52d67

Browse files
authored
Merge pull request #52 from joblift/unresolved-refs-are-errors
References that cannot be resolved should be treated as an error
2 parents a01f5d4 + 5a5a912 commit 3d52d67

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/main/java/com/networknt/schema/JsonSchemaException.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616

1717
package com.networknt.schema;
1818

19+
import java.util.Collections;
20+
import java.util.Set;
21+
1922
public class JsonSchemaException extends RuntimeException {
2023
private static final long serialVersionUID = -7805792737596582110L;
24+
private ValidationMessage validationMessage;
2125

2226
public JsonSchemaException(ValidationMessage validationMessage) {
2327
super(validationMessage.getMessage());
28+
this.validationMessage = validationMessage;
2429
}
2530

2631
public JsonSchemaException(String message) {
@@ -31,4 +36,10 @@ public JsonSchemaException(Throwable throwable) {
3136
super(throwable);
3237
}
3338

39+
public Set<ValidationMessage> getValidationMessages() {
40+
if (validationMessage == null) {
41+
return Collections.emptySet();
42+
}
43+
return Collections.singleton(validationMessage);
44+
}
3445
}

src/main/java/com/networknt/schema/RefValidator.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@
1616

1717
package com.networknt.schema;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.networknt.schema.url.URLFactory;
21-
import org.slf4j.Logger;
22-
import org.slf4j.LoggerFactory;
23-
2419
import java.io.InputStream;
2520
import java.net.MalformedURLException;
2621
import java.net.URL;
22+
import java.text.MessageFormat;
2723
import java.util.Collections;
2824
import java.util.Set;
2925

26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
import com.fasterxml.jackson.databind.JsonNode;
30+
import com.networknt.schema.url.URLFactory;
31+
3032
public class RefValidator extends BaseJsonValidator implements JsonValidator {
3133
private static final Logger logger = LoggerFactory.getLogger(RefValidator.class);
3234

@@ -72,6 +74,9 @@ public RefValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSch
7274
schema = new JsonSchema(validationContext, refValue, node, parentSchema);
7375
}
7476
}
77+
if (schema == null) {
78+
throw new JsonSchemaException(ValidationMessage.of(ValidatorTypeCode.REF.getValue(), CustomErrorMessageType.of("internal.unresolvedRef", new MessageFormat("{0}: Reference {1} cannot be resolved")), schemaPath, refValue));
79+
}
7580
}
7681

7782
private boolean isRelativePath(String schemaUrl) {

0 commit comments

Comments
 (0)