Skip to content

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

Open

Description

@papadeltasierra

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

Activity

  1. p1c2u commented on Jul 15, 2023

    @p1c2u
    Collaborator
  2. pawel-slowik commented on May 28, 2024

    @pawel-slowik

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions