mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 08:21:13 -05:00
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.
27 lines
772 B
Python
27 lines
772 B
Python
import os
|
|
|
|
from flask import json
|
|
import jsonschema
|
|
from jsonschema import Draft4Validator
|
|
|
|
|
|
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_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)
|