You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dear team,
I've been trying my best to migrate my schema validation from RefResolver to the newer referencing library, but with no avail.
I would really appreciate some help or tips to point me in the right direction.
What is working for me, with RefResolver, is the following code:
defvalidate_schema(
instance: dict[str, Any],
schema: dict[str, Any],
*,
path: str,
method: str,
response: str,
content: str="application/json",
) ->None:
try:
_methods=schema["paths"][path]
exceptKeyError: # pragma: no cover (should never happen)msg=f"Path {path} not found in schema"raiseValueError(msg) fromNonetry:
_responses=_methods[method]
exceptKeyError: # pragma: no cover (should never happen)msg=f"Method {method} not found in schema"raiseValueError(msg) fromNonetry:
_content=_responses["responses"][response]["content"]
exceptKeyError: # pragma: no cover (should never happen)msg=f"Response {response} not found in schema"raiseValueError(msg) fromNonetry:
_schema=_content[content]["schema"]
exceptKeyError: # pragma: no cover (should never happen)msg=f"Content {content} not found in schema"raiseValueError(msg) fromNoneopenapi_schema_validator.validate(
instance,
_schema,
cls=OAS30Validator,
resolver=RefResolver.from_schema(schema), # TODO: migrate to referencing lib
)
I then tried to migrate to the referencing lib as mentioned here:
But with no success, as all I get are errors like: jsonschema.exceptions._WrappedReferencingError: PointerToNowhere: '/components/schemas/Foo' does not exist within {'$ref': '#/components/schemas/Foo'}
What am I missing? Thank you for the help.
The text was updated successfully, but these errors were encountered:
Instead of passing a registry, you can flatten the schema dict before handing it to the validate function.
To correctly handle $ref pointers directly when parsing your YAML files, you can use a library like PyYAML combined with jsonschema's RefResolver or openapi-spec-validator to properly resolve $ref references. A more elegant solution for loading and resolving references in OpenAPI or YAML schemas is using the yaml loader from jsonschema or openapi-spec-validator libraries.
However, if your project requires loading complex OpenAPI files with nested $ref pointers, the prance library might be the most straightforward choice:
Dear team,
I've been trying my best to migrate my schema validation from RefResolver to the newer referencing library, but with no avail.
I would really appreciate some help or tips to point me in the right direction.
What is working for me, with RefResolver, is the following code:
I then tried to migrate to the referencing lib as mentioned here:
But with no success, as all I get are errors like:
jsonschema.exceptions._WrappedReferencingError: PointerToNowhere: '/components/schemas/Foo' does not exist within {'$ref': '#/components/schemas/Foo'}
What am I missing? Thank you for the help.
The text was updated successfully, but these errors were encountered: