Comments (7)
Does the library supports local references? Like if I have
common.json
and thenschema1.json
andschema2.json
, where I would like to use something defined incommon.json
. All files in the same directory. I noticed that it tried to download the referenced schema but I would prefer to use the local file - just giving relative path as"$ref": "common.json#/definitions/xyz"
.
Have you find a way to solve this problem ?
from python-fastjsonschema.
@EMAN6 Have a look here: https://github.com/robofit/arcor2/blob/6b57dc4a7431fab77a72d5c8547157a8607cea94/helpers.py#L60
from python-fastjsonschema.
hey @horejsek is this something we could contribute to fastjsonschema? In case you're interested in supporting it.
from python-fastjsonschema.
@heitorlessa Hi, it is already supported. You can pass handlers
parameter where you can define how to retrieve your local references.
from python-fastjsonschema.
Personnally, I have a hard time using the handlers
parameter. It is never ran and I get a urllib.error.URLError: <urlopen error [Errno 2] No such file or directory: '/soil_sample.schema.json'>
. It makes me think my handler did not have the chance to change the ref to the absolute path because it was never ran.
from python-fastjsonschema.
@WilliamHarvey97 could you provide smallest runnable code to showcase the problem? Ideally with opening new issue.
from python-fastjsonschema.
I've made an example to demonstrate the incorrect behavior I'm seeing with local references. It might be that I'm using handlers incorrectly or my schema is invalid, but I don't think so.
I tested this with (Python 3.8.10) :
jsonschema==4.21.1
fastjsonschema==2.19.1
I've inlined the schemas into the python file to make this easier to debug, but it wouldnt have worked correctly if it was in an external file either.
import fastjsonschema
from jsonschema import Draft7Validator, RefResolver
from referencing import Registry, Resource
from referencing.jsonschema import DRAFT7
def remotes_handler(uri):
print(f"remotes_handler: {uri}")
raise Exception("remotes_handler: {uri}")
# req = urlopen(uri)
# encoding = req.info().get_content_charset() or "utf-8"
# return json.loads(
# req.read().decode(encoding),
# )
handlers = {"": remotes_handler}
shared_schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "shared_schema.template.json",
"type": "object",
"definitions": {"metadata": {"type": "object"}},
}
schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "id_path/example.template.json",
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"metadata": {"$ref": "shared_schema.template.json#/definitions/metadata"},
},
},
}
json_data = {"tacos": {"metadata": {"version": "1.0.0"}}}
# resolver = RefResolver(base_uri="file:///", referrer=handlers)
schema_resource = Resource.from_contents(shared_schema)
registry = Registry().with_resource(
uri="id_path/shared_schema.template.json",
resource=schema_resource,
)
validator = Draft7Validator(schema, registry=registry)
errors = sorted(validator.iter_errors(json_data), key=lambda e: e.path)
print(errors)
print(f"is_valid: {validator.is_valid(json_data)}")
validator = fastjsonschema.compile(
schema,
handlers=handlers,
)
# this doesnt get reached, because resolver is incorrect.
validator(json_data)
What seem to be happening is that inside RefResolver's resolving, its incorrectly concatenating paths with the $id's path:
https://github.com/horejsek/python-fastjsonschema/blob/master/fastjsonschema/ref_resolver.py#L126
When doing so it also gets into a recursive look of resolving references until my handler gets called, at which point the url sent to it is:
'id_path/id_path/id_path/id_path/id_path/shared_schema.template.json'
I'm not sure exactly the right general solution, but one idea is if this behavior is generally wanted, then for my case I could pass in my own RefResolver, then I could correct for the behavior on my side without strange hacks).
Since I also wanted to make sure my schemas arent somehow invalid, I validated them using python jsonschema which is also in this example.
from python-fastjsonschema.
Related Issues (20)
- Uniqueness enforced if uniqueItems explicitly set to False HOT 1
- 2.16.1: test_compile_to_code_custom_format fails HOT 3
- 2.16.2 not available on pypi.org HOT 1
- RFC 3339 date month and mday should be two digit HOT 2
- Getting UnboundLocalError HOT 3
- are the compiled validation functions cached? HOT 2
- Datadog's Guarddog detects exec in setup.py and complains HOT 4
- Python 3.10 venv build is failing HOT 1
- sdist is missing tests HOT 1
- Cannot parse int64 types HOT 1
- [Potential bug] not working validation for properties not defined in the schema object HOT 1
- Release request HOT 1
- Wrong tag name for 2.17.0 HOT 1
- 2.17.0 sdist contains "stray" tests/examples/lengths that breaks tests HOT 2
- errors for missing required properties
- Error occurs when fastjsonschema generates empty except block HOT 2
- Feature request: automatically add nested defaults HOT 7
- RFE: is it possible to start making github releases?🤔 HOT 1
- test_pattern_with_escape_no_warnings fails with python 3.12 HOT 2
- unspecified required properties yield unexpected behavior HOT 1
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 python-fastjsonschema.