Git Product home page Git Product logo

Comments (6)

marksparkza avatar marksparkza commented on July 28, 2024 1

Hi,

Thanks for raising this issue - I'm working on improving the docs which are pretty scant in this area and will definitely add this as an example.

The add_schema method is actually not meant for application use - it's called internally during JSONSchema construction.

Okay, so let's say you have the following two schemas in a schemas directory (relative to the current working dir):

schemas/schema-org.json:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://example.com/schema-org",
    "type": "object",
    "properties": {
        "people": {
            "type": "array",
            "items": {
                "$ref": "https://example.com/schema-person"
            }
        }
    }
}

schemas/schema-person.json:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://example.com/schema-person",
    "type": "object",
    "properties": {
        "name": {
            "type": "string"
        }
    }
}

There are actually two ways to do this. In both ways you have to explicitly load your primary schema (the org schema in this case), and use that to evaluate the JSON data.

Option 1: Set up a base URI-to-directory mapping on the catalogue. In this case when the "$ref" to the person schema is encountered during the JSONSchema.loadf, the catalogue knows where to find that schema on disk, and loads it on the fly.

from jschon import create_catalogue, JSON, JSONSchema, URI

catalogue = create_catalogue('2020-12', default=True)
catalogue.add_directory(URI("https://example.com/"), 'schemas')

schema = JSONSchema.loadf('schemas/schema-org.json')
result = schema.evaluate(JSON({
    "people": [
        {"name": "Alice"},
        {"name": "Bob"}
    ]
}))
print(result.output('flag'))

Option 2: Load all of the schemas up front. In this case when the "$ref" is encountered, the target schema is found already cached in the catalogue.

from jschon import create_catalogue, JSON, JSONSchema

catalogue = create_catalogue('2020-12', default=True)

schema_person = JSONSchema.loadf('schemas/schema-person.json')
schema_org = JSONSchema.loadf('schemas/schema-org.json')
result = schema_org.evaluate(JSON({
    "people": [
        {"name": "Alice"},
        {"name": "Bob"}
    ]
}))
print(result.output('flag'))

In general, option 1 is probably better, because option 2 requires you to load the schemas in "$ref" dependency order.

Hope that helps!

from jschon.

marksparkza avatar marksparkza commented on July 28, 2024

Oh btw, I'm going to push out a long overdue release pretty soon, but in the meantime if you're using v0.6.0, just replace create_catalogue with Catalogue.create_default_catalogue.

from jschon.

marksparkza avatar marksparkza commented on July 28, 2024

In fact, there's a third way, which might be preferable even to option 1 above, since it avoids having to put the same directory path in two different places:

from jschon import create_catalogue, JSON, URI

catalogue = create_catalogue('2020-12', default=True)
catalogue.add_directory(URI("https://example.com/"), 'schemas')

schema = catalogue.get_schema(URI("https://example.com/schema-org"))
result = schema.evaluate(JSON({
    "people": [
        {"name": "Alice"},
        {"name": "Bob"}
    ]
}))
print(result.output('flag'))

from jschon.

Cylindric avatar Cylindric commented on July 28, 2024

Oh wow, what a rapid response :) Thanks I'll have a look at this as soon as I get back to my code!

from jschon.

Cylindric avatar Cylindric commented on July 28, 2024

That worked perfectly, thanks!
I still have some quirks of json-schema to learn, but being able to integrate it into our existing python tooling will be great.

from jschon.

marksparkza avatar marksparkza commented on July 28, 2024

Great, glad to hear that!

from jschon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.