Explicitly add json schema validator for schema validation
JSON Schema use validator (draft3 or draft4) to validate the given schema against given instance. By default it use draft4 validator which is current one. If new validator will be released in future and that become default then, there might be some compatibility issues between draft4 and new version. For example from draft3 to draft4, 'ip-version' format has been changed to 'ipv4'. This commits explicitly pass the validator while doing the schema validation. Also adds unit tests to check the current version of validator. If any new validator will be available in future, then we can use that by doing proper modification in schema files if needed. Change-Id: Ie11e3ba60926f247a82670b7da37d7890d16280c
This commit is contained in:
@@ -34,7 +34,8 @@ MAX_RECURSION_DEPTH = 2
|
||||
# All the successful HTTP status codes from RFC 7231 & 4918
|
||||
HTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206, 207)
|
||||
|
||||
# JSON Schema format checker used for JSON Schema validation
|
||||
# JSON Schema validator and format checker used for JSON Schema validation
|
||||
JSONSCHEMA_VALIDATOR = jsonschema.Draft4Validator
|
||||
FORMAT_CHECKER = jsonschema.draft4_format_checker
|
||||
|
||||
|
||||
@@ -809,6 +810,7 @@ class RestClient(object):
|
||||
if body_schema:
|
||||
try:
|
||||
jsonschema.validate(body, body_schema,
|
||||
cls=JSONSCHEMA_VALIDATOR,
|
||||
format_checker=FORMAT_CHECKER)
|
||||
except jsonschema.ValidationError as ex:
|
||||
msg = ("HTTP response body is invalid (%s)") % ex
|
||||
@@ -823,6 +825,7 @@ class RestClient(object):
|
||||
if header_schema:
|
||||
try:
|
||||
jsonschema.validate(resp, header_schema,
|
||||
cls=JSONSCHEMA_VALIDATOR,
|
||||
format_checker=FORMAT_CHECKER)
|
||||
except jsonschema.ValidationError as ex:
|
||||
msg = ("HTTP response header is invalid (%s)") % ex
|
||||
|
@@ -15,6 +15,7 @@
|
||||
import json
|
||||
|
||||
import httplib2
|
||||
import jsonschema
|
||||
from oslotest import mockpatch
|
||||
import six
|
||||
|
||||
@@ -1027,3 +1028,24 @@ class TestRestClientJSONSchemaFormatValidation(TestJSONSchemaValidationBase):
|
||||
}
|
||||
body = {'foo': 'example@example.com'}
|
||||
self._test_validate_pass(schema, body)
|
||||
|
||||
|
||||
class TestRestClientJSONSchemaValidatorVersion(TestJSONSchemaValidationBase):
|
||||
|
||||
schema = {
|
||||
'status_code': [200],
|
||||
'response_body': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'foo': {'type': 'string'}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def test_current_json_schema_validator_version(self):
|
||||
with mockpatch.PatchObject(jsonschema.Draft4Validator,
|
||||
"check_schema") as chk_schema:
|
||||
body = {'foo': 'test'}
|
||||
self._test_validate_pass(self.schema, body)
|
||||
chk_schema.mock.assert_called_once_with(
|
||||
self.schema['response_body'])
|
||||
|
Reference in New Issue
Block a user