Skip to content

Support Location-Independent Identifier $refs #341

@hooke

Description

@hooke

With jsonschema v.2.6.0, I couldn't manage to use id's of subschemas in $ref in the same file.

test.json with schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Test Schema",
  "description": "A test for subschemes' relative URIs",
  "type": "object",
"additionalProperties": false,
  "properties": {
    "prop1": { "$ref": "#smth"}
  },
  "definitions": {
    "smth": {
      "$id": "#smth",
      "type": "object",
      "properties": {
        "some_field": {
          "type": "string"
        }
      }
    }
  }
}

test_sample.json with JSON to test:

{
  "prop1": {
    "some_field": "a text"
  }
}

The python code:

import os
import jsonschema, json
from pprint import pprint

schema_absolute_path = "/home/hooke/prj/MK/python-test/example/"
schema_file_name = "test.json"

with open(schema_absolute_path + schema_file_name) as schema_file:    
    schema = json.load(schema_file)

try:
    jsonschema.Draft4Validator.check_schema(schema)
    print "Valid draft 4 schema"
except jsonschema.exceptions.SchemaError: 
    print "Invalid schema"

schema_absolute_uri = 'file://' + schema_absolute_path + schema_file_name

resolver = jsonschema.RefResolver(base_uri = schema_absolute_uri, referrer = schema)



validator = jsonschema.Draft4Validator(
        schema, resolver=resolver)

instance_file_name = "test_sample.json"
with open(schema_absolute_path + instance_file_name) as data_file:    
    data = json.load(data_file)

try:
    validator.validate(data, schema)
    print "Properly accepted object"
except jsonschema.exceptions.RefResolutionError as e: 
    print e
except jsonschema.exceptions.ValidationError: 
    print "Failed to accept object"

Result:

$ python example_test.py 
Valid draft 4 schema
Unresolvable JSON pointer: u'smth'

The aforementioned schema was validated successfully on jsonschemalint.com with draft-04, and with https://github.com/epoberezkin/ajv validator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething doesn't work the way it should.Needs SimplificationAn issue which is in need of simplifying the example or issue being demonstrated for diagnosis.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions