Skip to content
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

Better documentation on extending RefResolver #225

Closed
foxx opened this issue May 10, 2015 · 4 comments
Closed

Better documentation on extending RefResolver #225

foxx opened this issue May 10, 2015 · 4 comments

Comments

@foxx
Copy link

foxx commented May 10, 2015

The documentation made it quite difficult to understand how RefResolver should be extended, and I had to trawl through code to make it work. This is useful when you want to reference other schemas, but you don't want to use file://

It would be nice to see better documentation on this, but in the mean time, here is how I got it working for anyone who runs into the same problem;

import jsonschema

class SchemaResolver(jsonschema.RefResolver):
    def resolve_remote(self, uri):
        if uri.startswith('/SOMETHING'):
            # your custom schema goes here
            return {}
        else:
            # fallback to original
            return super(SchemaResolver, self).resolve_remote(uri)

def validate(schema, doc):
    """Validate using custom resolver"""
    resolver = SchemaResolver.from_schema(schema)
    validator_cls = jsonschema.validators.validator_for(schema)
    validator_cls.check_schema(schema)
    validator = validator_cls(schema, resolver=resolver)
    validator.validate(doc)

schema = {"$ref": "/SOMETHING/v1/definitions.json"}
doc = {"field": "value"}

validate(schema, doc)
@Julian
Copy link
Member

Julian commented May 10, 2015

Hey!

This is a duplicate of #135, but yes you're certainly right, documentation would be great here, would be awesome if you could write up what you've found possibly?

For your specific solution, jsonschema doesn't encourage (or explicitly support) inheritance for RefResolver (or really any of its APIs) -- I suspect you'll find other "solutions" for relative file references in that issue ticket, but it's certainly something that comes up enough to warrant some more direction.

Thanks!

@Julian Julian closed this as completed May 10, 2015
@foxx
Copy link
Author

foxx commented May 10, 2015

Thanks for the quick reply, sorry I didn't see #135 previously. I'll have a read through and leave my thoughts, thanks again :)

@foxx
Copy link
Author

foxx commented May 10, 2015

I was able to find an alternative way to support this on 2.4.0 without needing to subclass. Either works fine, admittedly you have more customisation control if you subclass, but this is more readable and forces you to use correctly formed URIs

#!/usr/bin/env python

import jsonschema
import json

rootschema = json.load("""
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
      "link": {
        "type": "number"
      }
    }
}
""")

schema = json.load("""
{
  "type": "object",
  "properties": {
    "field": { "$ref": "/v1/rootschema.json#/definitions/link" }
  }
}
""")

doc = json.load("""
{
    "field": 0
}
""")

validator_cls = jsonschema.validators.validator_for(schema)
validator_cls.check_schema(schema)
validator = validator_cls(schema)

validator.resolver.store['/v1/rootschema.json'] = rootschema
validator.validate(doc)

@ksolie
Copy link

ksolie commented Sep 22, 2023

@foxx Were you able to find a solution using Registry?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants