Open papadeltasierra opened 1 year ago
@papadeltasierra please check openapi-spec-validator project.
Not sure if that's what being asked, but you definitely can validate JSON payloads from files (as opposed to validating the schema itself). All you need to do is to create simple implementations of the request / response protocols, openapi_core.protocols.Request
and openapi_core.protocols.Response
. Then you can pass your objects to the validate_request
and validate_response
methods.
Example:
from dataclasses import dataclass
from typing import Mapping, Optional, Any
from openapi_core import OpenAPI
from openapi_core.datatypes import RequestParameters
@dataclass
class Request():
host_url: str
path: str
full_url_pattern: str
method: str
parameters: RequestParameters
content_type: str
body: Optional[bytes]
@dataclass
class Response():
status_code: int
content_type: str
headers: Mapping[str, Any]
data: Optional[bytes]
openapi = OpenAPI.from_file_path("openapi.yaml")
request = Request(
host_url="https://localhost:8000",
path="/foo/bar",
full_url_pattern="",
method="post",
parameters=RequestParameters(
header={
"Authorization": "Bearer whatever",
}
),
content_type="application/json",
body=open("request.json", "rb").read(),
)
openapi.validate_request(request)
ok_response = Response(
status_code=200,
content_type="application/json",
headers={},
data=open("response_200.json", "rb").read(),
)
openapi.validate_response(request, ok_response)
error_response = Response(
status_code=400,
content_type="application/json",
headers={},
data=open("response_400.json", "rb").read(),
)
openapi.validate_response(request, error_response)
Suggested Behavior
I am using JSON files to provide JSON blobs that get passed to internal methods but I want to make sure that when people extend this process, the JSON they provide really is correct according to the Swager definition. Could there be, or is there already, some way to use this library to do this, possibly by somehow Mocking up something with perhaps a request/response, URL and JSON blob inputs to provide enough for openapi-core to do its stuff?
Why is this needed?
See above.
References
No response
Would you like to implement a feature?
None