Python jsonschema.RefResolver() Examples

The following are 30 code examples of jsonschema.RefResolver(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module jsonschema , or try the search function .
Example #1
Source File: verify_schema.py    From Spectrum-Access-System with Apache License 2.0 8 votes vote down vote up
def testJsonSchema(schema_file, test_file):
  schema = loadSchema(schema_file)
  data = loadJson(test_file)

  print 'Loaded test validation JSON value from %s:' % test_file

  dir = os.path.dirname(os.path.realpath(__file__))

  resolver = jsonschema.RefResolver(referrer=schema, base_uri='file://' + dir + '/')

  try:
    jsonschema.validate(data, schema, resolver=resolver)
  except jsonschema.exceptions.ValidationError as e:
    print e
    print 'FAILED VALIDATION for %s' % test_file
    pprint(data)
    return 1

  print 'Validated.'
  return 0 
Example #2
Source File: data_loaders.py    From cloudformation-cli with Apache License 2.0 6 votes vote down vote up
def make_validator(schema, base_uri=None):
    if not base_uri:
        base_uri = Draft7Validator.ID_OF(schema)

    def get_from_local(uri):  # pylint: disable=unused-argument
        meta_schema = Path(os.path.dirname(os.path.realpath(__file__))).joinpath(
            "data/schema/meta-schema.json"
        )
        return json.load(meta_schema.open())

    resolver = RefResolver(
        base_uri=base_uri,
        referrer=schema,
        handlers={"http": get_from_local, "https": get_from_local},
    )
    return Draft7Validator(schema, resolver=resolver) 
Example #3
Source File: util.py    From starfish with MIT License 6 votes vote down vote up
def _create_validator(schema: Dict) -> Draft4Validator:
        """resolve $ref links in a loaded json schema and return a validator

        Parameters
        ----------
        schema : Dict
            loaded json schema

        Returns
        -------
        Draft4Validator :
            json-schema validator specific to the supplied schema, with references resolved

        """

        # Note: we are using 5.0.0 here as the first known file. It does *not* need to
        # be upgraded with each version bump since only the dirname is used.
        experiment_schema_path = Path(resource_filename(
            package_name, "spacetx_format/schema/experiment_5.0.0.json"))

        package_root_path = experiment_schema_path.parent.parent
        base_uri = f"{package_root_path.as_uri()}/"
        resolver = RefResolver(base_uri, schema)
        return Draft4Validator(schema, resolver=resolver) 
Example #4
Source File: utils.py    From pyhf with Apache License 2.0 6 votes vote down vote up
def validate(spec, schema_name, version=None):
    schema = load_schema(schema_name, version=version)
    try:
        resolver = jsonschema.RefResolver(
            base_uri='file://{0:s}'.format(
                pkg_resources.resource_filename(__name__, 'schemas/')
            ),
            referrer=schema_name,
            store=SCHEMA_CACHE,
        )
        validator = jsonschema.Draft6Validator(
            schema, resolver=resolver, format_checker=None
        )
        return validator.validate(spec)
    except jsonschema.ValidationError as err:
        raise InvalidSpecification(err) 
Example #5
Source File: validator.py    From basis_set_exchange with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _get_schema(file_type):
    '''Get a schema that can validate BSE JSON files or dictionaries

       The schema_type represents the type of BSE JSON file to be validated,
       and can be 'component', 'element', 'table', 'metadata', or 'references'.

       Returns the schema and the reference resolver
    '''

    schema_file = "{}-schema.json".format(file_type)
    file_path = os.path.join(_default_schema_dir, schema_file)

    schema = fileio.read_schema(file_path)

    # Set up the resolver for links
    base_uri = 'file://{}/'.format(_default_schema_dir)
    resolver = jsonschema.RefResolver(base_uri=base_uri, referrer=schema)

    return schema, resolver 
Example #6
Source File: api.py    From quay with Apache License 2.0 6 votes vote down vote up
def is_valid_response(action, resp={}):
    assert action.name in actions.keys()

    schema_for = {
        "IndexState": "State",
        "Index": "IndexReport",
        "GetIndexReport": "IndexReport",
        "GetVulnerabilityReport": "VulnerabilityReport",
    }
    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "clair-v4.openapi.json")

    with open(filename) as openapi_file:
        openapi = json.load(openapi_file)
        resolver = RefResolver(base_uri="", referrer=openapi)
        schema = openapi["components"]["schemas"][schema_for[action.name]]

        try:
            validate(resp, schema, resolver=resolver)
            return True
        except Exception:
            logger.exception("Security scanner response failed OpenAPI validation")
            return False 
Example #7
Source File: protocol.py    From ofd with Apache License 2.0 6 votes vote down vote up
def __init__(self, versions, path, skip_unknown=False, min_date='2016.09.01', future_hours=24):
        """
        Класс для валидации документов от ККТ по json-схеме.
        :param versions: поддерживаемые версии протокола, например ['1.0', '1.05'].
        :param path: путь до директории, которая содержит все директории со схемами, разбитым по версиям,
        например, схемы для протокола 1.0 должны лежать в <path>/1.0/
        :param skip_unknown: если номер версии отличается от поддерживаемых пропускать валидацию
        """
        self._validators = {}
        self._skip_unknown = skip_unknown
        schema_dir = os.path.expanduser(path)
        schema_dir = os.path.abspath(schema_dir)

        self.min_date = datetime.datetime.strptime(min_date, '%Y.%m.%d') if min_date else None
        self.future_hours = future_hours

        for version in versions:
            full_path = os.path.join(schema_dir, version, 'document.schema.json')
            with open(full_path, encoding='utf-8') as fh:
                schema = json.loads(fh.read())
                resolver = jsonschema.RefResolver('file://' + full_path, None)
                validator = Draft4Validator(schema=schema, resolver=resolver)
                validator.check_schema(schema)  # проверяем, что сама схема - валидная
                self._validators[version] = validator 
Example #8
Source File: json_faker.py    From contrail-server-manager with Apache License 2.0 6 votes vote down vote up
def validate_JSON(data,combined_schema):
    for keys in combined_schema['properties']:
        if "$ref" in (combined_schema['properties'])[keys]:
            refUrl= ((combined_schema['properties'])[keys])['$ref']
            references_file, section = refUrl.split('#')

    if not os.path.isfile(references_file):
        print "References file does not exists"
        sys.exit()

    schema_dir = os.path.dirname(os.path.realpath(references_file))
    resolver = RefResolver("file://{}/".format(schema_dir), None)

    try:
        validate(data,combined_schema,format_checker=FormatChecker(), resolver=resolver)
        print "JSON is valid with the schema"
        return True
    except exceptions.ValidationError as error:
        print(error.message)
        return False

#Merging schemas and putting key translations 
Example #9
Source File: schemas_are_valid_json.py    From metadata-schema with Apache License 2.0 6 votes vote down vote up
def get_validator(filename, base_uri=''):
    """Load schema from JSON file;
    Check whether it's a valid schema;
    Return a Draft4Validator object.
    Optionally specify a base URI for relative path
    resolution of JSON pointers (This is especially useful
    for local resolution via base_uri of form file://{some_path}/)
    """

    schema = get_json_from_file(filename)
    try:
        # Check schema via class method call. Works, despite IDE complaining
        Draft4Validator.check_schema(schema)
        print("Schema %s is valid JSON" % filename)
    except SchemaError:
        raise
    if base_uri:
        resolver = RefResolver(base_uri=base_uri,
                               referrer=filename)
    else:
        resolver = None
    return Draft4Validator(schema=schema,
                           resolver=resolver) 
Example #10
Source File: test_list.py    From agents-aea with Apache License 2.0 6 votes vote down vote up
def setup_class(cls):
        """Set the test up."""
        cls.schema = json.load(open(AGENT_CONFIGURATION_SCHEMA))
        cls.resolver = jsonschema.RefResolver(
            "file://{}/".format(Path(CONFIGURATION_SCHEMA_DIR).absolute()), cls.schema
        )
        cls.validator = Draft4Validator(cls.schema, resolver=cls.resolver)
        cls.cwd = os.getcwd()
        cls.t = tempfile.mkdtemp()
        # copy the 'dummy_aea' directory in the parent of the agent folder.
        shutil.copytree(Path(CUR_PATH, "data", "dummy_aea"), Path(cls.t, "dummy_aea"))
        cls.runner = CliRunner()
        os.chdir(Path(cls.t, "dummy_aea"))

        with mock.patch(
            "aea.cli.list.format_items", return_value=FORMAT_ITEMS_SAMPLE_OUTPUT
        ):
            cls.result = cls.runner.invoke(
                cli, [*CLI_LOG_OPTION, "list", "protocols"], standalone_mode=False
            ) 
Example #11
Source File: test_list.py    From agents-aea with Apache License 2.0 6 votes vote down vote up
def setup_class(cls):
        """Set the test up."""
        cls.schema = json.load(open(AGENT_CONFIGURATION_SCHEMA))
        cls.resolver = jsonschema.RefResolver(
            "file://{}/".format(Path(CONFIGURATION_SCHEMA_DIR).absolute()), cls.schema
        )
        cls.validator = Draft4Validator(cls.schema, resolver=cls.resolver)
        cls.cwd = os.getcwd()
        cls.t = tempfile.mkdtemp()
        # copy the 'dummy_aea' directory in the parent of the agent folder.
        shutil.copytree(Path(CUR_PATH, "data", "dummy_aea"), Path(cls.t, "dummy_aea"))
        cls.runner = CliRunner()
        os.chdir(Path(cls.t, "dummy_aea"))

        with mock.patch(
            "aea.cli.list.format_items", return_value=FORMAT_ITEMS_SAMPLE_OUTPUT
        ):
            cls.result = cls.runner.invoke(
                cli, [*CLI_LOG_OPTION, "list", "skills"], standalone_mode=False
            ) 
Example #12
Source File: transform.py    From singer-python with Apache License 2.0 6 votes vote down vote up
def resolve_schema_references(schema, refs=None):
    '''Resolves and replaces json-schema $refs with the appropriate dict.

    Recursively walks the given schema dict, converting every instance
    of $ref in a 'properties' structure with a resolved dict.

    This modifies the input schema and also returns it.

    Arguments:
        schema:
            the schema dict
        refs:
            a dict of <string, dict> which forms a store of referenced schemata

    Returns:
        schema
    '''
    refs = refs or {}
    return _resolve_schema_references(schema, RefResolver("", schema, store=refs)) 
Example #13
Source File: test_create.py    From agents-aea with Apache License 2.0 6 votes vote down vote up
def setup_class(cls):
        """Set the test up."""
        cls.schema = json.load(open(AGENT_CONFIGURATION_SCHEMA))
        cls.resolver = jsonschema.RefResolver(
            make_jsonschema_base_uri(Path(CONFIGURATION_SCHEMA_DIR).absolute()),
            cls.schema,
        )
        cls.validator = Draft4Validator(cls.schema, resolver=cls.resolver)

        cls.runner = CliRunner()
        cls.agent_name = "myagent"
        cls.cwd = os.getcwd()
        cls.t = tempfile.mkdtemp()
        os.chdir(cls.t)
        result = cls.runner.invoke(
            cli, [*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR]
        )
        assert result.exit_code == 0

        cls.result = cls.runner.invoke(
            cli,
            [*CLI_LOG_OPTION, "create", "--local", cls.agent_name],
            standalone_mode=False,
        )
        cls.agent_config = cls._load_config_file(cls.agent_name) 
Example #14
Source File: test_freeze.py    From agents-aea with Apache License 2.0 6 votes vote down vote up
def setup_class(cls):
        """Set the test up."""
        cls.schema = json.load(open(AGENT_CONFIGURATION_SCHEMA))
        cls.resolver = jsonschema.RefResolver(
            make_jsonschema_base_uri(Path(CONFIGURATION_SCHEMA_DIR)), cls.schema
        )
        cls.validator = Draft4Validator(cls.schema, resolver=cls.resolver)

        cls.cwd = os.getcwd()
        cls.t = tempfile.mkdtemp()
        # copy the 'dummy_aea' directory in the parent of the agent folder.
        shutil.copytree(Path(CUR_PATH, "data", "dummy_aea"), Path(cls.t, "dummy_aea"))
        cls.runner = CliRunner()
        os.chdir(Path(cls.t, "dummy_aea"))
        cls.result = cls.runner.invoke(
            cli, [*CLI_LOG_OPTION, "freeze"], standalone_mode=False
        ) 
Example #15
Source File: cmd.py    From contrail-docker with Apache License 2.0 6 votes vote down vote up
def validate(self, data=None):
        if not data:
            data = self.config_dict
        schema_dir = "/usr/share/contrailctl/schema/"
        schema_path="{}/{}.json".format(schema_dir, self.component)
        resolver = RefResolver("file://{}/".format(schema_dir), None)
        try:
            schema=open(schema_path,'r').read()
        except IOError as error:
            print("Schema file is missing - {}".format(schema_path))
            return True
        try:
            validate(data, json.loads(schema), format_checker=FormatChecker(), resolver=resolver)
            return True
        except exceptions.ValidationError as error:
            print(error.message)
            return False 
Example #16
Source File: test_schema.py    From agents-aea with Apache License 2.0 5 votes vote down vote up
def setup_class(cls):
        """Set up the test class."""
        cls.schema = json.load(open(AGENT_CONFIGURATION_SCHEMA))
        cls.resolver = jsonschema.RefResolver(
            make_jsonschema_base_uri(Path(CONFIGURATION_SCHEMA_DIR).absolute()),
            cls.schema,
        )
        cls.validator = Draft4Validator(cls.schema, resolver=cls.resolver) 
Example #17
Source File: redfish_json_validator.py    From oneview-redfish-toolkit with Apache License 2.0 5 votes vote down vote up
def validate(dict_to_validate, schema_name):
        """Validates a dict against a schema corresponding to the schema_name

            Returns:
                None

            Exception:
                ValidationError: Raises this exception on validation failure.
        """
        stored_schemas = config.get_stored_schemas()
        schema_obj = RedfishJsonValidator.get_schema_obj(schema_name)

        resolver = jsonschema.RefResolver('', schema_obj, store=stored_schemas)
        jsonschema.validate(dict_to_validate, schema_obj, resolver=resolver) 
Example #18
Source File: test_schema.py    From agents-aea with Apache License 2.0 5 votes vote down vote up
def setup_class(cls):
        """Set up the test class."""
        cls.schema = json.load(open(CONNECTION_CONFIGURATION_SCHEMA))
        cls.resolver = jsonschema.RefResolver(
            make_jsonschema_base_uri(Path(CONFIGURATION_SCHEMA_DIR).absolute()),
            cls.schema,
        )
        cls.validator = Draft4Validator(cls.schema, resolver=cls.resolver) 
Example #19
Source File: test_schema.py    From agents-aea with Apache License 2.0 5 votes vote down vote up
def setup_class(cls):
        """Set up the test class."""
        cls.schema = json.load(open(PROTOCOL_CONFIGURATION_SCHEMA))
        cls.resolver = jsonschema.RefResolver(
            make_jsonschema_base_uri(Path(CONFIGURATION_SCHEMA_DIR).absolute()),
            cls.schema,
        )
        cls.validator = Draft4Validator(cls.schema, resolver=cls.resolver) 
Example #20
Source File: security.py    From schemathesis with MIT License 5 votes vote down vote up
def process_definitions(self, schema: Dict[str, Any], endpoint: Endpoint, resolver: RefResolver) -> None:
        """Add relevant security parameters to data generation."""
        definitions = self.get_security_definitions(schema, resolver)
        requirements = get_security_requirements(schema, endpoint)
        for name, definition in definitions.items():
            if name in requirements:
                if definition["type"] == "apiKey":
                    self.process_api_key_security_definition(definition, endpoint)
                self.process_http_security_definition(definition, endpoint) 
Example #21
Source File: openlibrary.py    From openlibrary-client with GNU Affero General Public License v3.0 5 votes vote down vote up
def validate(self, doc, schema_name):
        """Validates a doc's json representation against
        its JSON Schema using jsonschema.validate().
        Returns:
          None
        Raises:
          jsonschema.exceptions.ValidationError if validation fails.
        """
        path = os.path.dirname(os.path.realpath(__file__))
        schemata_path = "%s/schemata/%s" % (path, schema_name)
        with open(schemata_path) as schema_data:
            schema = json.load(schema_data)
            resolver = jsonschema.RefResolver('file://' + schemata_path, schema)
            return jsonschema.Draft4Validator(schema, resolver=resolver).validate(doc.json()) 
Example #22
Source File: test_import_schema.py    From openlibrary-client with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_import_examples(example):
    with open(IMPORT_SCHEMA) as schema_data:
        schema = json.load(schema_data)
        resolver = jsonschema.RefResolver('file://' + IMPORT_SCHEMA, schema)
        result = jsonschema.Draft4Validator(schema, resolver=resolver).validate(example)
        assert result is None 
Example #23
Source File: router.py    From st2 with Apache License 2.0 5 votes vote down vote up
def add_spec(self, spec, transforms):
        info = spec.get('info', {})
        LOG.debug('Adding API: %s %s', info.get('title', 'untitled'), info.get('version', '0.0.0'))

        self.spec = spec
        self.spec_resolver = jsonschema.RefResolver('', self.spec)

        validate(copy.deepcopy(self.spec))

        for filter in transforms:
            for (path, methods) in six.iteritems(spec['paths']):
                if not re.search(filter, path):
                    continue

                for (method, endpoint) in six.iteritems(methods):
                    conditions = {
                        'method': [method.upper()]
                    }

                    connect_kw = {}
                    if 'x-requirements' in endpoint:
                        connect_kw['requirements'] = endpoint['x-requirements']

                    m = self.routes.submapper(_api_path=path, _api_method=method,
                                              conditions=conditions)
                    for transform in transforms[filter]:
                        m.connect(None, re.sub(filter, transform, path), **connect_kw)

                    module_name = endpoint['operationId'].split(':', 1)[0]
                    __import__(module_name)

        for route in sorted(self.routes.matchlist, key=lambda r: r.routepath):
            LOG.debug('Route registered: %+6s %s', route.conditions['method'][0], route.routepath) 
Example #24
Source File: test_nondefault_resolver_validator.py    From python-jsonschema-objects with MIT License 5 votes vote down vote up
def test_non_default_resolver_validator(markdown_examples):
    ms = URIDict()
    draft3 = load_schema("draft3")
    draft4 = load_schema("draft4")
    ms[draft3["id"]] = draft3
    ms[draft4["id"]] = draft4
    resolver_with_store = RefResolver(draft3["id"], draft3, ms)

    # 'Other' schema should be valid with draft3
    builder = pjo.ObjectBuilder(
        markdown_examples["Other"],
        resolver=resolver_with_store,
        validatorClass=Draft3Validator,
        resolved=markdown_examples,
    )
    klasses = builder.build_classes()
    a = klasses.Other(MyAddress="where I live")
    assert a.MyAddress == "where I live" 
Example #25
Source File: integration.py    From sagemaker-rl-container with Apache License 2.0 5 votes vote down vote up
def validate(self, config):
        """
        Validates that config contains the required list of parameter keys.
        :param config: the configuration dictionary to validate.
        :return:
        """
        # TODO: Implement validation
        pass
        # try:
        #     resolver = jsonschema.RefResolver(base_uri="file://" + BASE_SCHEMA_PATH + "/", referrer=self.schema)
        #     jsonschema.validate(config, self.schema, resolver=resolver)
        # except jsonschema.ValidationError as e:
        #     raise JsonSchemaValidationError(e) 
Example #26
Source File: osci_test.py    From koschei with GNU General Public License v2.0 5 votes vote down vote up
def validate_schema(obj, schema_ref):
    schema_path = f'{testdir}/schema/{schema_ref}.json'
    with open(schema_path) as f:
        schema = json.load(f)
    jsonschema.validate(obj, schema, resolver=jsonschema.RefResolver(f'file://{schema_path}', None)) 
Example #27
Source File: validators.py    From core with MIT License 5 votes vote down vote up
def _resolve_schema(schema_file_uri):
    with open(schema_file_uri) as schema_file:
        base_uri = os.path.dirname(schema_file_uri)
        schema = json.load(schema_file)
        resolver = jsonschema.RefResolver('file://'+base_uri+'/', schema)
        return (schema, resolver) 
Example #28
Source File: validate.py    From timeflux with MIT License 5 votes vote down vote up
def resolver():
    """Load the schema and returns a resolver."""
    if RESOLVER:
        return RESOLVER
    path = str(pathlib.Path(__file__).parents[1].joinpath("schema", "app.json"))
    with open(path) as stream:
        schema = json.load(stream)
    globals()["RESOLVER"] = RefResolver(
        "https://schema.timeflux.io/app.json", None
    ).from_schema(schema)
    return RESOLVER 
Example #29
Source File: configurator.py    From mikado with GNU Lesser General Public License v3.0 5 votes vote down vote up
def create_validator(simple=False):

    """Method to create a validator class (see extend_with_default).
    The simple keyword (boolean) is used to determine whether to keep
    only SimpleComment or full Comments from the schema.

    :type simple: bool

    :return validator
    :rtype: jsonschema.Draft7Validator
    """

    validator = extend_with_default(jsonschema.Draft7Validator,
                                    simple=simple)

    resolver = jsonschema.RefResolver("file:///{}".format(os.path.abspath(
        os.path.dirname(pkg_resources.resource_filename("Mikado.configuration", os.path.basename(__file__)))
    )), None)

    with io.TextIOWrapper(resource_stream("Mikado.configuration",
                                          "configuration_blueprint.json")) as blue:
        blue_print = json.loads(blue.read())

    validator = validator(blue_print, resolver=resolver)

    return validator 
Example #30
Source File: router.py    From ml-rest with The Unlicense 5 votes vote down vote up
def __init__(self, api):
        """
        Instantiate a new Lepo router.

        :param api: The OpenAPI definition object.
        :type api: dict
        """
        self.api = deepcopy(api)
        self.api.pop('host', None)
        self.handlers = {}
        self.resolver = RefResolver('', self.api)