-
Notifications
You must be signed in to change notification settings - Fork 3
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
Develop JSON Schema validator #70
Comments
I guess some of these might come in handy: https://github.com/json-schema-org/JSON-Schema-Test-Suite#python |
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? |
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:
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. 😉 |
As discussed during today's call, we need a validator for the JSON response that goes further than only checking whether there is a response or not.
The text was updated successfully, but these errors were encountered: