Coder Social home page Coder Social logo

Develop JSON Schema validator about proxi-schemas HOT 3 OPEN

RalfG avatar RalfG commented on June 27, 2024
Develop JSON Schema validator

from proxi-schemas.

Comments (3)

bittremieux avatar bittremieux commented on June 27, 2024 2

Ah crap, I should've checked GitHub first, because I started on my own script.

With the flex library you can actually avoid having to convert the Swagger file to JSON schema, so that should be a bit more efficient.

The code is very simple:

import requests
from flex.core import load, validate_api_call

schema = load('https://raw.githubusercontent.com/HUPO-PSI/proxi-schemas/master/specs/swagger.yaml')

url = 'http://proteomecentral.proteomexchange.org/api/proxi/v0.1/spectra?resultType=full&usi=mzspec:PXL000006:02-14-2019:index:1250'
response = requests.get(url)

validate_api_call(schema, raw_request=response.request, raw_response=response)

See also the flex documentation on API call validation.

There seem to be several issues with the Swagger definition file and the API response preventing successful validation. So I guess it's good to have a validator. 😉

from proxi-schemas.

RalfG avatar RalfG commented on June 27, 2024

I guess some of these might come in handy: https://github.com/json-schema-org/JSON-Schema-Test-Suite#python

from proxi-schemas.

RalfG avatar RalfG commented on June 27, 2024

@edeutsch, @bittremieux,

Here's a simple Python script that does exactly what we want:

"""
Validate API response with OpenAPI YAML schema.

For example:

$ python proxi_validator.py --openapi-yaml "specs/swagger.yaml" --request-url "http://proteomecentral.proteomexchange.org/api/proxi/v0.1/spectra?resultType=full&usi=mzspec:PXL000006:02-14-2019:index:1250"

"""

import click
import requests
import yaml
from jsonschema import validate
from openapi_schema_to_json_schema import to_json_schema


def jsonschema_from_yaml_file(path_to_yaml):
    """Read OpenAPI schema yaml file and return as JSON Schema dictionary."""
    with open(path_to_yaml, 'r') as stream:
        open_api_schema = yaml.safe_load(stream)
    jsonschema = to_json_schema(open_api_schema)
    return jsonschema


def get_api_response(url):
    """Get API response JSON object."""
    response = requests.get(url)
    return response.json()


@click.command()
@click.argument("openapi-yaml", type=str)
@click.argument("request-url", type=str)
def main(**kwargs):
    """Validate API response with OpenAPI YAML schema."""
    validate(
        get_api_response(kwargs["request_url"]),
        schema=jsonschema_from_yaml_file(kwargs["openapi_yaml"])
    )


if __name__ == "__main__":
    main()

A ValidationError is thrown if the API response is invalid for the given swagger.yaml file.

Do I open a PR to add the script to the repository?

from proxi-schemas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.