Example #1
0
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),
                )
Example #2
0
    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
Example #3
0
File: ref.py Project: Lookyan/flex
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))
Example #4
0
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)
Example #5
0
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
Example #6
0
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
Example #7
0
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
Example #8
0
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
Example #9
0
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
Example #10
0
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
Example #11
0
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
Example #12
0
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
Example #13
0
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
Example #14
0
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
Example #15
0
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
Example #16
0
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
Example #17
0
def test_json_string():
    native = {'foo': 'bar'}
    source = json.dumps(native)
    result = load_source(source)

    assert result == native
Example #18
0
def test_yaml_string():
    native = {b'foo': b'bar'}
    source = yaml.dump(native)
    result = load_source(source)

    assert result == native
Example #19
0
def test_yaml_string():
    native = {b'foo': b'bar'}
    source = yaml.dump(native)
    result = load_source(source)

    assert result == native
Example #20
0
def test_native_mapping_is_passthrough():
    source = {'foo': 'bar'}
    result = load_source(source)

    assert result == source
    assert result is not source
Example #21
0
def test_native_mapping_is_passthrough():
    source = {'foo': 'bar'}
    result = load_source(source)

    assert result == source
Example #22
0
def test_json_string():
    native = {'foo': 'bar'}
    source = json.dumps(native)
    result = load_source(source)

    assert result == native