Comments (12)
Indeed not even the example on https://openapi-schema-validator.readthedocs.io/en/latest/references.html works.
jsonschema.exceptions._WrappedReferencingError: PointerToNowhere: '/components/schemas/Name' does not exist within {'type': 'object', 'required': ['name'], 'properties': {'name': {'$ref': '#/components/schemas/Name'}, 'age': {'$ref': '#/components/schemas/Age'}, 'birth-date': {'$ref': '#/components/schemas/BirthDate'}}, 'additionalProperties': False}
How should the connection from "#/components/schemas/Name" to "urn:name-schema" be established?
from openapi-schema-validator.
@Cynocracy Thanks for the full code example! I think understand your approach better now, namely validating against the whole schema. My uneducated guess from my short debugging session is that your example should still work even if you omit the with_resources()
call.
Unfortunately, my use case is validating that the given data matches one part precisely, and not just "is valid under any possibilities given in the schema", e.g. in my simplified example I want to validate that the data is a list of uuid, and not a single uuid. So using the whole schema does not work there, I think.
from openapi-schema-validator.
Guys, if you need WA for the current issue I did the following:
- replaced
#/components/schemas/Name
withurn:Name
in the schema
import collections.abc
def update(out, intput):
for key, val in intput.items():
if isinstance(val, collections.abc.Mapping):
out,[key] = update(out.get(key, {}), val)
elif key == "$ref":
out[key] = val.replace("#/components/schemas/", "urn:")
else:
out,[key] = val
return out,
schema = {}
update(schema, schema_with_PointerToNowhere)
- populated
Registry
with these urns
resources = [(f"urn:{res_name}", Resource.from_contents(res, default_specification=DRAFT202012))
for res_name, res in schema['components']['schemas'].items()]
registry = Registry().with_resources(resources)
from openapi-schema-validator.
Maybe a little more context to what I wrote helps?
I was taking the full apidoc for an openAPI service (saved to schema.json) and doing
from openapi_schema_validator import validate
from referencing import Registry, Resource
from referencing.jsonschema import DRAFT202012
import json
f = open('schema.json')
schema = json.load(f)
resources = [(f"#/components/schemas/{res_name}", Resource.from_contents(res, default_specification=DRAFT202012))
for res_name, res in schema['components']['schemas'].items()]
registry = Registry().with_resources(resources)
create_schema = schema["components"]["requestBodies"]["MYBODY"]["content"]["application/json"]["schema"]
from jsonschema import Draft202012Validator
validator = Draft202012Validator(
create_schema,
registry=registry,
)
validator.validate({ "foo": "bar"})
I agree that the inclusion of the urn reference in the thing being validated in the docs confused me, and I don't fully understand that bit :)
I've just now had a brief debugging session. It seems to me that #/components... uris are looked up in two stages. With my code example in the issue description, there's firstly a lookup on base_uri='', and then afterwards the fragment is tried to resolve as a pointer /components/schema/uuid in the Resource that was found there, which fails
Huh, now I'm curious how my example worked... looking at it a bit now 🕵️♂️
from openapi-schema-validator.
resources = [(f"#/components/schemas/{res_name}", Resource.from_contents(res, default_specification=DRAFT202012))
for res_name, res in schema['components']['schemas'].items()]
registry = Registry().with_resources(resources)
seems to work for me, without the replacement of "#/components/schemas/
-> urn:
If this is supported by Registry (I haven't really read through their docs well enough to know :)), maybe the example just needs updating?
edit: incorrect! see below
from openapi-schema-validator.
I'm sorry to say, @AlexeyKatanaev the idea you propose is exactly what the docs instruct to do, and what I've already tried to do (see issue description), but unfortunately this simply does not work (and it's not just me, @lawilog also confirmed it).
@Cynocracy thanks for the suggestion, but I've now tried f'#/components/schemas/{name}'
and f'/components/schemas/{name}'
and also both of those with an additional urn:
prefix, but none of those work for me, unfortunately.
I've just now had a brief debugging session. It seems to me that #/components...
uris are looked up in two stages. With my code example in the issue description, there's firstly a lookup on base_uri=''
, and then afterwards the fragment is tried to resolve as a pointer /components/schema/uuid
in the Resource that was found there, which fails. Because it further seems to me that in the call of validate(['{urn:uuid:deadbeef}'], schema=schema['components']['schemas']['something'], registry=registry)
the base_uri=''
is already occupied by the value of the schema=
parameter. So there is no chance at all to resolve these references, because of course the something
definition does not also contain the referenced uuid
definition, as that's the whole point of the reference in the first place.
So I'm sorry to say, I'm still completely stuck on how to migrate local references from RefResolver
to referencing.Registry
, and would appreciate any help. Thanks!
from openapi-schema-validator.
@wosc Ah yeah, you're correct! It turns out the schema I was using already had $refs evaluated, so the registry was doing a whole lot of nothing.
edit: can also confirm that using the YAML with the $refs in it quickly becomes a headache, because my $refs also have $refs and that doesn't seem to be handled correctly, afaict, even with the find replace to urn:
from openapi-schema-validator.
Related Issues (20)
- Validate byte properly
- Inner any of type mismatch not raising error
- Validating schema using yaml file HOT 2
- Nullable Strings are not recognized, incorrect check for None if type is string
- Cannot import _legacy_validators HOT 5
- OAS30Validator validates readonly/writeonly incorectly
- Validate doesnt seem to match schema correctly? HOT 3
- Support for scientific notation HOT 3
- test_array_prefixitems_invalid[value0] failing on 0.6.2 HOT 2
- Incompatible with jsonschema 4.10.0 HOT 3
- Subclassing JSONSchema validator class with OAS30Validator HOT 1
- Redundant runtime dependency on `attrs`
- 0.4.2 test failures: tests/integration/test_validators.py::TestOAS{30,31}Validator::test_format_checkers HOT 1
- OpenAPI 3.0 Validator allows byte as string type
- OpenAPI 3.0 list of types raises TypeError not SchemaError HOT 1
- Formats raise error for other types
- AttributeError: module 'jsonschema._utils' has no attribute 'load_schema' HOT 2
- ERROR in validation: ... validation error: '2023-03-23 13:35:45' is not a 'date-time' - 'timestamp' HOT 2
- `readOnly` / `writeOnly` support for OpenAPI 3.1 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 openapi-schema-validator.