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

Validate against a schema using $ref with relative filepath #262

Closed
seanraymentPP opened this issue Jul 30, 2019 · 3 comments
Closed

Validate against a schema using $ref with relative filepath #262

seanraymentPP opened this issue Jul 30, 2019 · 3 comments

Comments

@seanraymentPP
Copy link

I'm trying to come up with a way to validate against a json schema (7.0) that makes use of a $ref to a relative filepath. The schema is of the form:

...
"definitions": {
    "Success": {
        "allOf": [
            { "$ref": "events.json#/Event" }
            ...
        ]
    }
}
...

When I load this schema I get the reference is not canonical error. This seems to be because I'm not passing a full URI. Is it possible to validate against this schema?

@johandorland
Copy link
Collaborator

Can you paste the full schema?

@seanraymentPP
Copy link
Author

seanraymentPP commented Jul 30, 2019

The two schema files:

Event.json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Event": {
        "type": "object",
        "required": ["timestamp", "body"]
    }
  }
}

Success.json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "allOf": [
    { "$ref": "Event.json#/definitions/Event"},
    {
      "type": "object",
      "properties": {
        "body": {
          "id": {
            "type": "string",
          },
          "message": {
            "type": "string",
         }
        }
      }
    }
  ]
}

Since I am doing validation in a test, I was able to get this working by putting the schemas in testdata and by loading the root schema (Success.json) with gojsonschema.NewReferenceLoader instead of gojsonschema.NewStringLoader.

So I guess my question now is: is recursively resolving relative $ref's with jsonStringLoader not supported?

@johandorland
Copy link
Collaborator

johandorland commented Jul 30, 2019

{ "$ref": "Event.json"} on it's own is meaningless. There is no way of knowing that it is a file on the file system somewhere. When you loaded it using a NewReferenceLoader the library knew where the schema came from (as it does the loading of the schema by itself) and acts as a base $id against which the { "$ref": "Event.json"} is resolved. With the NewStringLoader you just provide the raw data, but it has no way of knowing what the base URI is. How is it supposed to know a bunch of raw bytes originated in a file somewhere?

So in short. When using NewReferenceLoader, the reference you give it also acts as an $id. You can more about $id and $ref work if you're not familiar with it here on the JSON schema site.

If you can keep things simple just use the NewReferenceLoader. In case that doesn't suit your needs and need more flexibility you have load them manually using gojsonschema.SchemaLoader. Documentation on that can be found here in the README.

To answer your question directly: It does, but a relative $ref is useless without an absolute URI somewhere to resolve against.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants