Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$ref not detecting the schema #93

Closed
asker123 opened this issue Apr 10, 2014 · 10 comments
Closed

$ref not detecting the schema #93

asker123 opened this issue Apr 10, 2014 · 10 comments

Comments

@asker123
Copy link

@asker123 asker123 commented Apr 10, 2014

Hi,

I have two schema files in the same folder:

NS.json and NB.json.

NS.json has a reference to NB.json like this:

$ref = "NB.json#/network"

Everytime I try to validate the schema, it fails with a message "cannot dereference URI"

I tried giving the whole file path, but that fails as well. Is there any other way to reference a schema in seperate file, to another schema to make the validator work?

Thanks.

@fge
Copy link
Collaborator

@fge fge commented Apr 10, 2014

Hello,

Can you provide the full code and error message please? Also, what version are you using?

@asker123
Copy link
Author

@asker123 asker123 commented Apr 11, 2014

I made this simple schema:

PersonSchema.json has a reference to AddressSchema.json

PersonSchema.Json

{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema"
"required":false,
"properties":{
"name": {
"type":"string",
"id": "http://jsonschema.net/name",
"required":false,
"default":"",
"editable": true,
"visible": true
},
"address": {
"type": "object",
"$ref": "AddressSchema.json#/address"
}
}
}

AdderssSchema.json

address{
"type":"object",
"$schema": "http://json-schema.org/draft-04/schema",
"required":false,
"properties":{
"street_address": {
"type":"string",
"id": "http://jsonschema.net/street_address",
"required":false,
"editable": true,
"visible": true
},
"house_number": {
"type":"number",
"id": "http://jsonschema.net/house_number",
"required":false,
"editable": false,
"visible": true
}
}

}

Both of them are in the same directory.

Now I register this schema, using the vertx-json-schema-validator as follows:

Register_Person.js

var vertx = require('vertx');
var container = require("vertx/container");
var console = require("vertx/console");

var eb = vertx.eventBus;
var address = "ns.jsonvalidator";
var y = {
"action": "addSchema",
"key":"person_schema",
"jsonSchema": {
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"required":false,
"properties":{
"name": {
"type":"string",
"id": "http://jsonschema.net/name",
"required":false,
"default":"",
"editable": true,
"visible": true
},
"address": {
"type": "object",
"$ref": "AddressSchema.json#/address"
}
}
}

}
eb.send(address,y,function(message) {
console.log("Received reply"+JSON.stringify(message));
});

Register_Address.js

var vertx = require('vertx');
var container = require("vertx/container");
var console = require("vertx/console");

var eb = vertx.eventBus;
var address = "ns.jsonvalidator";

var y = {
"action": "addSchema",
"key":"address_schema",
"jsonSchema": {
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"required":false,
"properties":{
"street_address": {
"type":"string",
"id": "http://jsonschema.net/street_address",
"required":false,
"editable": true,
"visible": true
},
"house_number": {
"type":"number",
"id": "http://jsonschema.net/house_number",
"required":false,
"editable": false,
"visible": true
}
}

}

}
eb.send(address,y,function(message) {
console.log("Received reply"+JSON.stringify(message));
});

And then, I send this for validation:

Validate_Person.js:

var vertx = require('vertx');
var container = require("vertx/container");
var console = require("vertx/console");

var eb = vertx.eventBus;
var address = "ns.jsonvalidator";

var x = {
"action": "validate",
"key":"person_schema",
"json":
{
"name": "Aka",
"address":
{
"street_address": "panch",
"house_number": 25
}
}
}
eb.send(address,x,function(message) {
console.log("Received reply"+JSON.stringify(message));
});

This is the error I see:

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:129)
at com.github.fge.jsonschema.processors.ref.RefResolver.process(RefResolver.java:66)
at com.github.fge.jsonschema.processors.ref.RefResolver.process(RefResolver.java:51)
at com.github.fge.jsonschema.processing.ProcessorChain$ProcessorMerger.process(ProcessorChain.java:186)
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:3589)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2374)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2337)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2252)
at com.google.common.cache.LocalCache.get(LocalCache.java:3990)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3994)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4878)
at com.github.fge.jsonschema.processing.CachingProcessor.process(CachingProcessor.java:91)
at com.github.fge.jsonschema.processors.validation.ValidationChain.process(ValidationChain.java:99)
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:3589)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2374)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2337)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2252)
at com.google.common.cache.LocalCache.get(LocalCache.java:3990)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3994)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4878)
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.process(ValidationProcessor.java:46)
at com.github.fge.jsonschema.processing.ProcessingResult.of(ProcessingResult.java:79)
at com.github.fge.jsonschema.main.JsonSchema.doValidate(JsonSchema.java:69)
at com.github.fge.jsonschema.main.JsonSchema.validate(JsonSchema.java:93)
at com.campudus.jsonvalidator.SchemaValidatorBusMod$$anonfun$asyncReceive$1.applyOrElse(SchemaValidatorBusMod.scala:51)
at com.campudus.jsonvalidator.SchemaValidatorBusMod$$anonfun$asyncReceive$1.applyOrElse(SchemaValidatorBusMod.scala:42)
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:33)
at io.vertx.busmod.ScalaBusMod$class.apply(ScalaBusMod.scala:33)
at com.campudus.jsonvalidator.SchemaValidatorBusMod.apply(SchemaValidatorBusMod.scala:27)
at com.campudus.jsonvalidator.SchemaValidatorBusMod.apply(SchemaValidatorBusMod.scala:27)
at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
at org.vertx.scala.core.FunctionConverters$$anon$4.handle(FunctionConverters.scala:39)
at org.vertx.java.core.eventbus.impl.DefaultEventBus$11.run(DefaultEventBus.java:906)
at org.vertx.java.core.impl.DefaultContext$3.run(DefaultContext.java:176)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:354)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:353)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
at java.lang.Thread.run(Thread.java:744)
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:129)
at com.github.fge.jsonschema.processors.ref.RefResolver.process(RefResolver.java:66)
at com.github.fge.jsonschema.processors.ref.RefResolver.process(RefResolver.java:51)
at com.github.fge.jsonschema.processing.ProcessorChain$ProcessorMerger.process(ProcessorChain.java:186)
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:3589)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2374)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2337)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2252)
at com.google.common.cache.LocalCache.get(LocalCache.java:3990)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3994)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4878)
at com.github.fge.jsonschema.processing.CachingProcessor.process(CachingProcessor.java:91)
at com.github.fge.jsonschema.processors.validation.ValidationChain.process(ValidationChain.java:99)
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:3589)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2374)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2337)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2252)
at com.google.common.cache.LocalCache.get(LocalCache.java:3990)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3994)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4878)
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.process(ValidationProcessor.java:46)
at com.github.fge.jsonschema.processing.ProcessingResult.of(ProcessingResult.java:79)
at com.github.fge.jsonschema.main.JsonSchema.doValidate(JsonSchema.java:69)
at com.github.fge.jsonschema.main.JsonSchema.validate(JsonSchema.java:93)
at com.campudus.jsonvalidator.SchemaValidatorBusMod$$anonfun$asyncReceive$1.applyOrElse(SchemaValidatorBusMod.scala:51)
at com.campudus.jsonvalidator.SchemaValidatorBusMod$$anonfun$asyncReceive$1.applyOrElse(SchemaValidatorBusMod.scala:42)
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:33)
at io.vertx.busmod.ScalaBusMod$class.apply(ScalaBusMod.scala:33)
at com.campudus.jsonvalidator.SchemaValidatorBusMod.apply(SchemaValidatorBusMod.scala:27)
at com.campudus.jsonvalidator.SchemaValidatorBusMod.apply(SchemaValidatorBusMod.scala:27)
at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
at org.vertx.scala.core.FunctionConverters$$anon$4.handle(FunctionConverters.scala:39)
at org.vertx.java.core.eventbus.impl.DefaultEventBus$11.run(DefaultEventBus.java:906)
at org.vertx.java.core.impl.DefaultContext$3.run(DefaultContext.java:176)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:354)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:353)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
at java.lang.Thread.run(Thread.java:744)

Thanks for helping me out.

@asker123
Copy link
Author

@asker123 asker123 commented Apr 11, 2014

Also the vertx project use version 2.0.1.

Thanks

@fge
Copy link
Collaborator

@fge fge commented Apr 11, 2014

OK, first of all, you have a problem with the address schema (it is not valid).

The problem here is that you don't load the Person schema via a URI. As such, when resolving the JSON Reference, you only get Address.json#/address as a URI; and the loader cannot find it, hence the error message (URI is not absolute).

I don't know vertx but what is needed here is one of two things:

  • either preload both schemas at known URIs; or
  • have them somewhere where they can be fetched via URIs.

Do you have a link to vertx?

@asker123
Copy link
Author

@asker123 asker123 commented Apr 11, 2014

This is the vertx project: https://github.com/campudus/vertx-json-schema-validator

Is it not possible to access the schema, even if it lies in the same folder?

I have seen some examples like here:

http://spacetelescope.github.io/understanding-json-schema/structuring.html

The pound symbol (#) refers to the current document, and then the slash (/) separated keys thereafter just traverse the keys in the objects in the document. Therefore, in our example "#/definitions/address" means:
go to the root of the document
find the value of the key "definitions"
within that object, find the value of the key "address"
$ref can also be a relative or absolute URI, so if you prefer to include your definitions in separate files, you can also do that. For example:

{ "$ref": "definitions.json#/address" }

Is this not supported in your project? Only absolute URI?

Thanks.

@fge
Copy link
Collaborator

@fge fge commented Apr 11, 2014

Is this not supported in your project?

Well yes it is (I even have the best JSON Reference support out there, without boasting); but the URI has to point somewhere. And here it can't point anywhere, since to start with, Person does not have a URI.

I cannot point to Address out of thin air! I have had a look at the vertx project in question and it loads schemas anonymously; the loading process therefore does not have a base URI to base itself upon.

I can work with the module author so that a URI be produced and this way cross schema referencing will work; in the meantime you have to include all schemas into one I'm afraid.

@asker123
Copy link
Author

@asker123 asker123 commented Apr 12, 2014

I am able to reference the schema if I provide an absolute file path. For now, I can work with that, like you explained in the other issue. And yes this project is really good and comprises of everything I need. :)

After working on it, I realised the issues are with vertx project I am using and it only supports draft v3. :(

Thanks.

@fge
Copy link
Collaborator

@fge fge commented Apr 14, 2014

Hmwell, as I said, this vertx module supports everything this library supports, so v4 is supported.

Anyway, can I close this issue?

@asker123
Copy link
Author

@asker123 asker123 commented Apr 14, 2014

Yes please.

Also, I mentioned on the vertx project thread, that the "id" property in my schema was causing issues and not the project.
Sorry for the confusion.

Thanks.

@fge
Copy link
Collaborator

@fge fge commented Apr 14, 2014

OK! Yes, that is true that id can cause problems here as well, since it redefines the URI context

@fge fge closed this Apr 14, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.