Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Validate JSON from a file? #612

Open
papadeltasierra opened this issue Jun 29, 2023 · 2 comments
Open

[Feature]: Validate JSON from a file? #612

papadeltasierra opened this issue Jun 29, 2023 · 2 comments

Comments

@papadeltasierra
Copy link

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

@p1c2u
Copy link
Collaborator

p1c2u commented Jul 15, 2023

@papadeltasierra please check openapi-spec-validator project.

@pawel-slowik
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants