def validate_deferred_references(schema, context, **kwargs): try: deferred_references = context['deferred_references'] except: raise KeyError("`deferred_references` not found in context") with ErrorDict() as errors: for reference in deferred_references: parts = urlparse.urlparse(reference) if any((parts.scheme, parts.netloc, parts.params, parts.query)): errors.add_error( reference, MESSAGES['reference']['unsupported'].format(reference), ) continue if parts.path: from flex.core import load_source if parts.path.startswith('/'): schema = load_source(parts.path) elif 'base_path' in kwargs: schema = load_source( os.path.join(kwargs['base_path'], parts.path)) try: jsonpointer.resolve_pointer(schema, parts.fragment) except jsonpointer.JsonPointerException: errors.add_error( reference, MESSAGES['reference']['undefined'].format(reference), )
def __init__(self, reference, context, **kwargs): if self.validators_constructor is None: raise NotImplementedError( "Subclasses of LazyReferenceValidator must specify a " "`validators_constructor` function" ) self._kwargs = kwargs parsed_ref = urlparse.urlparse(reference) self.reference_path = parsed_ref.path self.reference_fragment = parsed_ref.fragment if self.reference_path: from flex.core import load_source if self.reference_path.startswith('/'): context = load_source(self.reference_path) elif 'base_path' in kwargs: context = load_source(os.path.join(kwargs['base_path'], self.reference_path)) # TODO: something better than this which potentiall raises a # JsonPointerException jsonpointer.resolve_pointer(context, self.reference_fragment) self.reference = reference self.context = context
def validate_reference(reference, context, **kwargs): try: parts = urlparse.urlparse(reference) if parts.path: from flex.core import load_source if parts.path.startswith('/'): context = load_source(parts.path) elif 'base_path' in kwargs: context = load_source( os.path.join(kwargs['base_path'], parts.path)) jsonpointer.resolve_pointer(context, parts.fragment) except jsonpointer.JsonPointerException: raise ValidationError( MESSAGES['reference']['undefined'].format(reference))
def dereference_reference(reference, context, **kwargs): parts = urlparse.urlparse(reference) if any((parts.scheme, parts.netloc, parts.params, parts.query)): raise ValueError( MESSAGES['reference']['unsupported'].format(reference), ) if parts.path: from flex.core import load_source if parts.path.startswith('/'): context = load_source(parts.path) elif 'base_path' in kwargs: context = load_source(os.path.join(kwargs['base_path'], parts.path)) return jsonpointer.resolve_pointer(context, parts.fragment)
def swagger(): """Load the swagger specification in a JSON schema object""" # Need to clear the basePath to run tests locally raw_schema = flex.load_source('crime_data/static/swagger.json') raw_schema.pop('basePath', None) schema = flex.parse(raw_schema) yield schema
def test_json_file_path(): native = {'foo': 'bar'} source = json.dumps(native) tmp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.json') tmp_file.write(source) tmp_file.flush() result = load_source(tmp_file.name) assert result == native
def test_yaml_file_object(): native = {'foo': 'bar'} source = yaml.dump(native) tmp_file = tempfile.NamedTemporaryFile(mode='r+w') tmp_file.write(source) tmp_file.file.seek(0) result = load_source(tmp_file.file) assert result == native
def test_yaml_file_path(): native = {b'foo': b'bar'} source = yaml.dump(native) tmp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.yaml') tmp_file.write(source) tmp_file.flush() result = load_source(tmp_file.name) assert result == native
def test_yaml_file_object(): native = {b'foo': b'bar'} source = yaml.dump(native) tmp_file = tempfile.NamedTemporaryFile(mode='w') tmp_file.write(source) tmp_file.flush() with open(tmp_file.name) as yaml_file: result = load_source(yaml_file) assert result == native
def test_json_file_object(): native = {'foo': 'bar'} source = json.dumps(native) tmp_file = tempfile.NamedTemporaryFile(mode='w') tmp_file.write(source) tmp_file.file.seek(0) with open(tmp_file.name) as json_file: result = load_source(json_file) assert result == native
def test_url(httpbin): native = { 'origin': '127.0.0.1', #'headers': { # 'Content-Length': '', # 'Accept-Encoding': 'gzip, deflate', # 'Host': '127.0.0.1:54634', # 'Accept': '*/*', # 'User-Agent': 'python-requests/2.4.3 CPython/2.7.8 Darwin/14.0.0', # 'Connection': 'keep-alive', #}, 'args': {}, #'url': 'http://127.0.0.1:54634/get', } source = httpbin.url + '/get' result = load_source(source) assert isinstance(result, collections.Mapping) result.pop('headers') result.pop('url') assert result == native
def test_json_string(): native = {'foo': 'bar'} source = json.dumps(native) result = load_source(source) assert result == native
def test_yaml_string(): native = {b'foo': b'bar'} source = yaml.dump(native) result = load_source(source) assert result == native
def test_native_mapping_is_passthrough(): source = {'foo': 'bar'} result = load_source(source) assert result == source assert result is not source
def test_native_mapping_is_passthrough(): source = {'foo': 'bar'} result = load_source(source) assert result == source