Comments (6)
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.
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.
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.
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.
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.
Great, glad to hear that!
from jschon.
Related Issues (20)
- Extensibility of [Relative]JSONPointer classes? HOT 8
- Meta-discussion: extensibility
- Immutable JSON? HOT 3
- Suggest type checking tool and configuration, and maybe add to tox?
- Down or upgrading JSON schema objects to different draft versions HOT 1
- Allow updating the default (URI prefix == None) Source
- 4 tests fail
- "$schema" access should load a Metaschema like "$ref" access loads a JSONSchema HOT 1
- "$id" is (slightly) non-compliant (hidden by errors in JSON Schema Test Suite)
- Proposal: opt-in opinionated mode HOT 2
- Proposal: support a default source for URIs with no usable base URI vs relative-reference split
- Support instantiating mutually referencing schema bundles
- Proposal: Subschema iteration/recursion HOT 1
- Nitpick: "integer" is not a type that can restrict evaluation
- "instancePointer" incorrect in new-style ("hierarchical", "list") output HOT 3
- Proposal: Support for "default" values, output document with defaults, accept "default" values for "required" fields
- Support behavioral dependencies
- support draft-next "contains", "minContains", and "maxContains"
- support draft-next "propertyDependencies"
- jschon and OpenAPI support (surprisingly simple... probably) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jschon.