Use jsonschema validation + remove 'v2' schema

Emualated the validation methods that exist [in the python-client](620e5f7014/integration_test/__init__.py).

The `validate_v0` function loads json schemas from a local
`/schemas` directory, whereas the new `validate` function (which
we're going to use for our v2 API calls) uses the common
`get_notification_response` python schema defined in
"app/v2/notifications/notification_schemas.py".

Removed the new `v2` schema from the last commit as it's no longer
being used.

Also, refactored common code in the GET and POST contract files
so that making requests and converting responses to JSON are
pulled out into common functions.
This commit is contained in:
Paul Craig
2016-11-22 11:17:28 +00:00
parent fb50bb6325
commit 7ae427c4ef
12 changed files with 64 additions and 113 deletions

View File

@@ -2,15 +2,25 @@ import os
from flask import json
import jsonschema
from jsonschema import Draft4Validator
def validate(json_string, schema_filename):
schema_dir = os.path.join(os.path.dirname(__file__), 'schemas')
def return_json_from_response(response):
return json.loads(response.get_data(as_text=True))
def validate_v0(json_to_validate, schema_filename):
schema_dir = os.path.join(os.path.dirname(__file__), 'schemas/v0')
resolver = jsonschema.RefResolver('file://' + schema_dir + '/', None)
with open(os.path.join(schema_dir, schema_filename)) as schema:
jsonschema.validate(
json.loads(json_string),
json_to_validate,
json.load(schema),
format_checker=jsonschema.FormatChecker(),
resolver=resolver
)
def validate(json_to_validate, schema):
validator = Draft4Validator(schema)
validator.validate(json_to_validate, schema)