Skip to content

Instantly share code, notes, and snippets.

@jadeallenx
Last active January 4, 2018 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jadeallenx/c49eaa48dbe8fc3f69ae44defbc287b3 to your computer and use it in GitHub Desktop.
Save jadeallenx/c49eaa48dbe8fc3f69ae44defbc287b3 to your computer and use it in GitHub Desktop.
Using references in jesse for JSON schema validation

If you use the jesse application for JSON schema validation, you will no doubt be mystified by the byzantine way that references are handled in the application. I spent well over 30 hours trying to figure it out, since it is totally undocumented what the format should be.

I tried to make things as simple as possible. I had a file with the "base" schema for an item. I called it record.json It looked something like this:

{
   "id": "record",
   "description": "Base schema for a valid record",
   "type": "object",
   
   "properties": {
      "foo": { 
         "type": "string"
      },
      "bar": {
         "type": "number"
      }
   }
}

And then I had another file which took a collection of these items. It looked like this:

{
   "id": "records-bulk",
   "description": "A collection of records",
   "type": "object",
   
   "properties": {
       "records": {
          "type": "array",
          "items": { "$ref": "record" }
        }
   }
}

The intention here is clear - I want to reuse the schema for record as the elements in an array. For example, this is a valid input against the schema:

{
  "records": [
     { 
       "foo": "hoge",
       "bar": 42
     },
     { 
       "foo": "qux",
       "bar": 19.2
     }  
  ]
}

But how to communicate this to jesse? Well, it turns out that jesse canonicalizes your references - it insists on prefixing your ids with a URL - either "http://" or "file://". So to make this work in jesse, I needed to prefix both my ids with "file://" and then always reuse "file://record" anytime I wanted to use it elsewhere.

Apparently it is not possible to have "naked" references for reasons I can't possibly fathom. I might open a PR about this because it almost beggars imagination.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment