N1kk1
N1kk1

Reputation: 13

Everit schema dereferencing

I have the following schema:

String schema = "{\n" +
  "  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n" +
  "  \"$id\": \"https://test.com/someSchema.json",\n" +
  "  \"title\": \"test\",\n" +
  "  \"description\": \"test\",\n" +
  "  \"type\": \"object\",\n" +
  "  \"properties\": {\n" +
  "    \"message\": {\n" +
  .............
  "    },\n" +
  "    \"reference\": {\n" +
  "      \"$ref\": \"someRefSchema.json\"\n" +
  "    }\n" +
  "  },\n" +
  "}\n";

Also I have separated schema:

String referenceSchema = "{\n" +
  "  \"$id\": \"https://test.com/someRefSchema.json\",\n" +
  "  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n" +
  "  \"title\": \"test\",\n" +
  "  \"description\": \"test\",\n" +
  "  \"type\": \"object\",\n" +
  "  \"properties\": {\n" +
  "    \"message\": {\n" +
        .....
  "    },\n" +
  "}";

When I'm trying to validate some object with this schema, using everit I get the following exception: Exception in thread "main" java.io.UncheckedIOException: java.io.FileNotFoundException: https://test.com/someRefSchema.json

JSONObject schemaObject = new JSONObject(schema);
JSONObject referenceSchemaObject = new JSONObject(referenceSchema);
Schema schemaTarget = SchemaLoader.builder()
        .draftV7Support()
        .schemaJson(referenceSchema)
        .schemaJson(schemaObject)
        .build()
        .load()
        .build();
try {
  schemaTarget.validate(new JSONObject(subject));
} catch (ValidationException e) {
  throw new ValidationException(schema, "Error occurred: " + e.getMessage(), e.getKeyword(), e.getSchemaLocation());
}

I've tried to add .resolutionScope("https://test.com/") but the result is the same. How should I dereference inner schema correctly, which is located in separate JSON?

Upvotes: 0

Views: 359

Answers (1)

N1kk1
N1kk1

Reputation: 13

I've resolved the problem. Instead of .schemaJson(referenceSchema) there should be

.registerSchemaByURI(new URI("https://test.com/someRefSchema.json"), referenceSchemaObject)
.resolutionScope("https://test.com")

Upvotes: 1

Related Questions