Skip to content

Support Location-Independent Identifier $refs #341

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

Closed
hooke opened this issue Jun 22, 2017 · 3 comments
Closed

Support Location-Independent Identifier $refs #341

hooke opened this issue Jun 22, 2017 · 3 comments
Labels
Bug Something doesn't work the way it should. Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis.

Comments

@hooke
Copy link

hooke commented Jun 22, 2017

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.

@Julian
Copy link
Member

Julian commented May 14, 2018

Hi -- still haven't gotten a chance to look at this fully, but as-is, that definitely is not a valid reference, so if it validates successfully elsewhere, it sounds like those places have bugs.

In draft4, id is spelled that way, not $id. It's only Draft 6 and later where it uses a $.

@Julian Julian added the Bug Something doesn't work the way it should. label Aug 13, 2018
@Julian Julian added the Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis. label Mar 29, 2020
@Julian Julian changed the title $ref doesn't work with id's in the same file Support Location-Independent Identifier $refs Jul 1, 2020
@willson-chen
Copy link
Contributor

@hooke Hi, I have tested #717 could solve this problem.

@Julian
Copy link
Member

Julian commented Jul 28, 2022

This appears to work at this point (the code you pasted correctly validates the instance). Closing, but feel free to follow up if there are still any issues.

@Julian Julian closed this as completed Jul 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something doesn't work the way it should. Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis.
Projects
None yet
Development

No branches or pull requests

3 participants