Json schema ref other file

3.6k views Asked by At

With this schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "b": {
    "type": "object",
    "properties": {
      "c": {
        "type": "string"
      }
    }
  },
  "type": "object",
  "properties": {
    "a": {
      "$ref": "#/b"
    }
  }
}

I can validate this example:

{
  "a": {
    "c": "test"
  }
}

Now I want to create a new schema file for the "b" element and refer it in my 1st schema. How can I do this ? I try a lot of things but I always obtain jsonspec.reference.exceptions.NotFound: u'b.json' not registered.

1

There are 1 answers

0
AudioBubble On

After a couple of hours of googling and various doc readings, I've finally managed to be able to compose two json-schema's using "$ref" : "file:...".

My example data looks like this:

john-doe.json:

{
  "first_name": "john",
  "last_name": "doe",
  "age": 42,
  "address": {
    "street_address": "foo street 42",
    "city": "baklavastan",
    "state": "foobar"
    "foo": 42
  }
}

And then I have two json schema files, which lives in the same directory on my file system.

customer.json

{
  "type": "object",
  "properties": {
    "first_name": { "type": "string" },
    "last_name":  { "type": "string" },
    "age" :       { "type" : "integer" },
    "address" :   { "$ref" : "file:address.json#" }
  },
  "required": ["first_name", "last_name", "age", "address"],
  "additionalProperties": false
}

address.json

{
  "type": "object",
  "properties": {
    "street_address": { "type": "string" },
    "city":           { "type": "string" },
    "state":          { "type": "string" }
  },
  "required": ["street_address", "city", "state"],
  "additionalProperties": false
}

I am able to validate against the schema in Clojure using the library scjsv (which is a wrapper around the Java json-schema-validation library)

It gives me following validation error (in clojure edn) for the john-doe.json example:

{ :level "error",
  :schema {:loadingURI "file:address.json#", :pointer ""},
  :instance {:pointer "/address"},
  :domain "validation",
  :keyword "additionalProperties",
  :message
  "object instance has properties which are not allowed by the schema: [\"foo\"]",
  :unwanted ["foo"] }