java-json-tools/json-schema-validator

How to refer a json schema from another schema?

dheepakji opened this issue · 1 comments

I am comparing a json against a json schema. I want to refer a json schema from a parent json schema. I am using "$ref" but it is throwing this exception:

com.github.fge.jsonschema.exceptions.ProcessingException: URI is not absolute

Help me how to refer a schema from the another schema.

Thanks a lot in advance!

My code is given below:
sample.json

{
  "206": {
    "76": [
      65535,
      255
    ]
  }
}

schema.json

{
	"$schema": "http://json-schema.org/draft-07/schema#",
	"type": "object",
	"additionalProperties": false,
	"properties": {
		"206": {
			"type": "object",
			"properties": {
				"76": {
					"$ref" : "definitions.json#/definitions/maxUsedCellMeta"
				}
			}
		}
	}
}

definitions.json (External reference)

{
	"$schema": "http://json-schema.org/draft-07/schema#",
	"type": "object",
	"additionalProperties": false,
	"definitions": {
		"maxUsedCellMeta": {
			"minItems": 2,
			"additionalItems": false,
			"items": [
				{
					"type": "integer"
				},
				{
					"type": "integer"
				}
			]
		}
	}
}

Validation.java

package validation;

import static org.testng.Assert.assertTrue;

import java.io.FileReader;
import java.io.IOException;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.testng.annotations.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.main.JsonValidator;
import com.github.fge.jsonschema.report.ProcessingReport;

public class Validation {

	@Test
	public void validateJsonSchema() throws IOException, ProcessingException, ParseException {

		JSONParser parser = new JSONParser();

		FileReader schemaFile = new FileReader(
				"/Users/deepak-8318/Downloads/SchemaValidation/src/main/java/validation/schema.json");
		
		FileReader inputFile = new FileReader(
				"/Users/deepak-8318/Downloads/SchemaValidation/src/main/java/validation/sample.json");

		JSONObject jsonSchema = (JSONObject) parser.parse(schemaFile);
		JSONObject jsonInput = (JSONObject) parser.parse(inputFile);
		
		System.out.println("schema "+jsonSchema);

		JsonNode schema = JsonLoader.fromString(jsonSchema.toString());
		JsonNode input = JsonLoader.fromString(jsonInput.toString());

		JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
		JsonValidator validator = factory.getValidator();

		ProcessingReport report = validator.validate(schema, input);
		
//		assertTrue(report.isSuccess(), "SchemaValidation");
		
		if(report.isSuccess()) {
			System.out.println("Passed :)");
		}
		else {
			System.out.println("Failed :(");
		}

	}
}

#Output:

FAILED: validateJsonSchema
com.github.fge.jsonschema.exceptions.ProcessingException: URI is not absolute
	at com.github.fge.jsonschema.load.SchemaLoader.get(SchemaLoader.java:146)
	at com.github.fge.jsonschema.processors.ref.RefResolver.loadRef(RefResolver.java:128)
	at com.github.fge.jsonschema.processors.ref.RefResolver.process(RefResolver.java:65)
	at com.github.fge.jsonschema.processors.ref.RefResolver.process(RefResolver.java:50)
	at com.github.fge.jsonschema.processing.ProcessorChain$ProcessorMerger.process(ProcessorChain.java:168)
	at com.github.fge.jsonschema.processing.ProcessingResult.of(ProcessingResult.java:79)
	at com.github.fge.jsonschema.processing.CachingProcessor$1.load(CachingProcessor.java:110)
	at com.github.fge.jsonschema.processing.CachingProcessor$1.load(CachingProcessor.java:102)
	at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3599)
	at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2379)
	at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2342)
	at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2257)
	at com.google.common.cache.LocalCache.get(LocalCache.java:4000)
	at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4004)
	at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4874)
	at com.github.fge.jsonschema.processing.CachingProcessor.process(CachingProcessor.java:91)
	at com.github.fge.jsonschema.processors.validation.ValidationChain.process(ValidationChain.java:98)
	at com.github.fge.jsonschema.processors.validation.ValidationChain.process(ValidationChain.java:52)
	at com.github.fge.jsonschema.processing.ProcessorMap$Mapper.process(ProcessorMap.java:169)
	at com.github.fge.jsonschema.processing.ProcessingResult.of(ProcessingResult.java:79)
	at com.github.fge.jsonschema.processing.CachingProcessor$1.load(CachingProcessor.java:110)
	at com.github.fge.jsonschema.processing.CachingProcessor$1.load(CachingProcessor.java:102)
	at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3599)
	at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2379)
	at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2342)
	at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2257)
	at com.google.common.cache.LocalCache.get(LocalCache.java:4000)
	at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:4004)
	at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4874)
	at com.github.fge.jsonschema.processing.CachingProcessor.process(CachingProcessor.java:91)
	at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:77)
	at com.github.fge.jsonschema.processors.validation.ValidationProcessor.processObject(ValidationProcessor.java:172)
	at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:114)
	at com.github.fge.jsonschema.processors.validation.ValidationProcessor.processObject(ValidationProcessor.java:172)
	at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:114)
	at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:46)
	at com.github.fge.jsonschema.processing.ProcessingResult.of(ProcessingResult.java:79)
	at com.github.fge.jsonschema.main.JsonValidator.validate(JsonValidator.java:93)
	at validation.Validation.validateJsonSchema(Validation.java:44)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.privateRun(TestRunner.java:648)
	at org.testng.TestRunner.run(TestRunner.java:505)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
	at org.testng.SuiteRunner.run(SuiteRunner.java:364)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
	at org.testng.TestNG.runSuites(TestNG.java:1049)
	at org.testng.TestNG.run(TestNG.java:1017)
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

Maven Dependencies

		<dependency>
			<groupId>com.github.fge</groupId>
			<artifactId>jackson-coreutils</artifactId>
			<version>1.0</version>
		</dependency> 
		<dependency>
			<groupId>com.github.fge</groupId>
			<artifactId>json-schema-validator</artifactId>
			<version>2.0.0</version>
		</dependency>

Yeah I found it.

Instead of referring like this
"$ref" : "definitions.json#/definitions/maxUsedCellMeta" in schema.json

I used the absolute path of my definitions.json file.
"$ref": "file:/Users/Downloads/schema/definition/definitions.json#/definitions/maxUsedCellMeta"