Skip to content

How to migrate from RefResolver to referencing  #1079

Closed

Description

@cscetbon

Trying to follow https://python-jsonschema.readthedocs.io/en/latest/referencing/#migrating-from-refresolver using the latest alpha version but I can't get it to work. I currently have

def get_schema():
  return {
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "edm": {...},
        "eem": {...}
    },
    "anyOf": [
        {
            "$ref": "#/definitions/eem/events/SISAddress"
        },
        {
            "$ref": "#/definitions/eem/events/SISEnrollment"
        },
        {
            "$ref": "#/definitions/eem/events/SISPerson"
        },
        {
            "$ref": "#/definitions/eem/events/SISRestriction"
        },
        {
            "$ref": "#/definitions/eem/events/SISSection"
        }
    ]
}
  }
my_schema = get_schema()
schemastore = {
  'https://my_url/EDM.json': my_schema['definitions']['edm'],
  'https://my_url/EEM.json': my_schema['definitions']['eem']
}

resolver = jsonschema.RefResolver(base_uri='', referrer=my_schema, store=schemastore)
jsonschema.validate(payload, my_schema, resolver=resolver)

I tried to do

my_schema = get_schema()
schema = Resource.from_contents(my_schema)
registry = Registry().with_resources([
  ('https://my_url/EDM.json', my_schema['definitions']['edm']),
  ('https://my_url/EEM.json', my_schema['definitions']['eem'])
])
validator = Draft4Validator(schema=my_schema, registry=registry)
validator.validate(payload)

but I get

...
File ~/.virtualenvs/sds/lib/python3.10/site-packages/referencing/_core.py:394, in Registry.crawl(self)
    391 while uncrawled:
    392     uri, resource = uncrawled.pop()
--> 394     id = resource.id()
    395     if id is not None:
    396         uri = urljoin(uri, id)

AttributeError: 'dict' object has no attribute 'id'

Activity

  1. Julian commented on Apr 5, 2023

    @Julian
    Member

    You're passing dicts (object schemas) to the second argument of with_resources, but it takes referencing.Resource objects, which are essentially your dict wrapped with the information that it is a Draft 4 schema.

    You could instead either call Resource.from_contents on your dicts if they contain $schema keys, or use referencing.jsonschema.DRAFT4.create_resource(each) on them to explicitly say they're draft 4 resources, or even better would be if they have id keys in them indicating they're subresources (so e.g. "id": "https://my_url/EDM.json" in your definitions/edm schema) in which case you need to do nothing extra at all, they'll be automatically found if your parent schema resource is in the registry.

    Hope that helps but if there's suggestions for how you'd have figured the above out in the doc page you linked let me know.

  2. locked and limited conversation to collaborators on Apr 5, 2023
  3. converted this issue into a discussion #1080 on Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions