mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-28 03:11:40 -05:00
We were using the Draft4Validator in one place, so this updates it to the Draft7Validator instead. The schemas were mostly using draft 4 of the JSON schema, though there were a couple of schemas that were already of version 7. This updates them all to version 7, which is the latest version fully supported by the jsonschema Python package. There are some breaking changes in the newer version of the schema, but I could not see anywhere would these affect us. Some of these schemas were not valid in version 4, but are now valid in version 7 because `"required": []` was not valid in earlier versions.
27 lines
772 B
Python
27 lines
772 B
Python
import os
|
|
|
|
import jsonschema
|
|
from flask import json
|
|
from jsonschema import Draft7Validator
|
|
|
|
|
|
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 = Draft7Validator(schema)
|
|
validator.validate(json_to_validate, schema)
|