Beispiel #1
0
 def do_expected(self):
     with open(jsfile) as f:
         js = json.load(f)
     try:
         assert '@type' in js
         schema_name = js['@type']
         with open(os.path.join(schema_folder,
                                schema_name + ".json")) as file_object:
             schema = json.load(file_object)
         resolver = RefResolver('file://' + schema_folder + '/', schema)
         validator = Draft4Validator(schema, resolver=resolver)
         validator.validate(js)
     except (AssertionError, ValidationError, KeyError) as ex:
         if success:
             raise
         return
     assert success
Beispiel #2
0
    def convert(self, work_dir):
        """Convert an ISA-Tab dataset (version 1) to JSON provided the ISA model v1.0 JSON Schemas
            :param work_dir: directory containing the ISA-tab dataset
        """
        logger.info("Converting ISAtab to ISAjson for {}".format(work_dir))

        isa_tab = parse(work_dir)
        #print(isa_tab)

        if isa_tab is None:
            logger.fatal("No ISAtab dataset found")
        else:
            isa_json = dict([])
            if isa_tab.metadata != {}:
                #print("isa_tab.metadata->",isa_tab.metadata)
                isa_json = dict([
                    ("identifier",
                     isa_tab.metadata['Investigation Identifier']),
                    ("title", isa_tab.metadata['Investigation Title']),
                    ("description",
                     isa_tab.metadata['Investigation Description']),
                    ("submissionDate",
                     isa_tab.metadata['Investigation Submission Date']),
                    ("publicReleaseDate",
                     isa_tab.metadata['Investigation Public Release Date']),
                    ("ontologySourceReferences",
                     self.createOntologySourceReferences(
                         isa_tab.ontology_refs)),
                    ("publications",
                     self.createPublications(isa_tab.publications,
                                             "Investigation")),
                    ("people",
                     self.createContacts(isa_tab.contacts, "Investigation")),
                    ("studies", self.createStudies(isa_tab.studies)),
                    ("comments", self.createComments(isa_tab.metadata))
                ])

            #validate json
            schema = json.load(open(join(SCHEMAS_PATH, INVESTIGATION_SCHEMA)))
            resolver = RefResolver(
                'file://' + join(SCHEMAS_PATH, INVESTIGATION_SCHEMA), schema)
            validator = Draft4Validator(schema, resolver=resolver)
            validator.validate(isa_json, schema)

            logger.info("... conversion finished.")
            return isa_json
Beispiel #3
0
    def load_images(self):
        logger.debug("loading images from: " + self.settings.db_path)
        with open(self.schema_db_path) as data:
            self.schema_db = json.load(data)

        if os.path.isfile(self.settings.db_path):
            with open(self.settings.db_path) as data:
                self.db = json.load(data)

        # change relative paths to a absolute paths
        schema_path = 'file:///{0}/'.format(
            os.path.dirname(os.path.abspath(self.schema_db_path)).replace(
                "\\", '/'))
        resolver = RefResolver(schema_path, self.schema_db)
        logger.debug("validating db...")
        validate(self.db, self.schema_db, resolver=resolver)
        logger.debug("db is valid!")
Beispiel #4
0
 def resolve(self, schema: dict, refs: Dict[str, dict] = None) -> dict:
     """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 {}
     refs = {**self._shared_refs, **refs}
     return self._resolve_schema_references(
         schema, RefResolver("", schema, store=refs))
def validate_dataset(path, filename, error_printing):
    try:
        dataset_schema_file = open(join(DATS_schemasPath,"dataset_schema.json"))
        datasetSchema = json.load(dataset_schema_file)
        resolver = RefResolver('file://'+DATS_schemasPath+'/'+"dataset_schema.json", datasetSchema) #, base_uri=schemasPath)
        validator = Draft4Validator(datasetSchema, resolver=resolver)
        logger.info("Validating %s", filename)

        try:
            dataset_file = open(join(path,filename))
            instance = json.load(dataset_file)

            if (error_printing == 0):
                errors = sorted(validator.iter_errors(instance), key=lambda e: e.path)
                for error in errors:
                     print(error.message)

                if (len(errors)==0):
                    return True
                else:
                    return False

            elif (error_printing == 1):
                errors = sorted(validator.iter_errors(instance), key=lambda e: e.path)
                for error in errors:
                    for suberror in sorted(error.context, key=lambda e: e.schema_path):
                        print(list(suberror.schema_path), suberror.message, sep=", ")

                if (len(errors)==0):
                    logger.info("...done")
                    return True
                else:
                    return False
            else:
                try:
                    validator.validate(instance, datasetSchema)
                    logger.info("...done")
                    return True
                except Exception as e:
                    logger.error(e)
                    return False
        finally:
            dataset_file.close()
    finally:
        dataset_schema_file.close()
Beispiel #6
0
def model_validator(additional_properties=False,
                    validate_wltc_data=False,
                    validate_schema=False) -> PandelVisitor:
    ## NOTE: Using non-copied (mode, wltc)-schemas,
    #  since we are certain they will not be modified here.
    #
    schema = _get_model_schema(additional_properties)
    wltc_schema = (_get_wltc_schema() if validate_wltc_data else {}
                   )  ## Do not supply wltc schema, for speedup.
    resolver = RefResolver(_model_url, schema, store={_wltc_url: wltc_schema})

    validator = PandelVisitor(
        schema,
        resolver=resolver,
        auto_default=True,
        auto_default_nulls=True,
        auto_remove_nulls=True,
    )
    if validate_schema:
        ## Patch jsonschema-schema for extra fields & forbid miss-spells.
        #  See https://github.com/Julian/jsonschema/issues/268
        #
        stricter_metaschema = {
            **validator.META_SCHEMA,
            "additionalProperties": False,
            "properties": {
                **validator.META_SCHEMA["properties"],
                "tags": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    },
                    "uniqueItems": True,
                },
                # For extension https://sphinx-jsonschema.readthedocs.io/en/latest/schemakeywords.html
                "$$target": {
                    "type": ["string", "array"]
                },
            },
        }
        strictValidator = jsonschema.validators.validator_for(
            stricter_metaschema)
        strictValidator(stricter_metaschema).validate(schema)

    return validator
Beispiel #7
0
    def __init__(self, schema_dir, span_type):
        """Initialize a JSON validator by loading all schemas and resolving references.

        Args:
        schema_dir (str) -- Where to look for subschemas.
        span_type (str) -- Span types to allow, eg. array or string.
        """
        # RefResolver initialization requires a base schema and URI
        base_uri = schema_dir + "grammar_spec.schema.json"
        try:
            with open(base_uri) as fd:
                base_schema = json.load(fd)
        except Exception as e:
            print(e)
            raise e
        # NOTE: Though not required, naming convention is that schemas end in .schema.json
        re_pattern = "(.*)\/(.*).schema.json$"
        base_schema_name = re.search(re_pattern, base_uri).group(2)
        resolver = RefResolver(base_schema_name + ".schema.json", base_schema)

        span_schemas = ["string_span", "array_span", "array_and_string_span"]
        # Which type of span to load
        if span_type == "string":
            span_schema_name = "string_span"
        elif span_type == "array":
            span_schema_name = "array_span"
        elif span_type == "all":
            span_schema_name = "array_and_string_span"
        else:
            print("I don't recognize span type {}.".format(span_type))
            raise Exception

        # Load all subschemas in schema directory
        for schema_path in glob.glob(schema_dir + "*.json"):
            schema_name = re.search(re_pattern, schema_path).group(2)
            with open(schema_path) as fd:
                json_schema = json.load(fd)
            if schema_name == span_schema_name:
                resolver.store["span" + ".schema.json"] = json_schema
            elif schema_name in span_schemas:
                continue
            else:
                resolver.store[schema_name + ".schema.json"] = json_schema
        self.base_schema = base_schema
        self.resolver = resolver
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"
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)
Beispiel #10
0
def validate(path):
    json_dir = os.path.join(path, 'event')
    json_files = os.listdir(json_dir)
    schema_dir = os.path.join(path, 'schema')
    schema_files = os.listdir(schema_dir)

    for schema_filename in schema_files:
        for json_filename in json_files:
            with open(os.path.join(schema_dir, schema_filename)) as json_schema, \
                    open(os.path.join(json_dir, json_filename)) as json_data:
                schema = json.load(json_schema)
                data = json.load(json_data)

                Draft7Validator.check_schema(schema)
                resolver = RefResolver(schema_dir, schema)
                validator = Draft7Validator(schema, resolver=resolver)

                yield validator.iter_errors(data)
Beispiel #11
0
def is_json_file_valid(validated_data: dict, schema_path):
    """
    Validates if dictionary suites provided schema.
    :validated_data: dict. Dictionary for validation.
    :schema_path: string. The template schema for validation.
    :return: boolean. Is dictionary is valid under provided schema.
    """
    schema = load_json_file(schema_path)

    resolver = RefResolver('file:///' + abspath('.').replace("\\", "/") + '/',
                           schema)

    try:
        return Draft4Validator(
            schema, resolver=resolver).validate(validated_data) is None
    except ValidationError as error:
        logging.getLogger(__name__).error(error)
        return False
Beispiel #12
0
    def init_app(self, app):
        path = os.path.split(os.path.realpath(__file__))
        app_dir = os.path.split(path[0])[0]
        for version in app.config['SCHEMA_VERSIONS']:
            schema_path = os.path.join(app_dir,
                                       app.config['SCHEMA_RELATIVE_DIRECTORY'],
                                       version)
            with open(os.path.join(schema_path,
                                   app.config['SCHEMA_FILENAME'])) as schema:
                self.schema[version] = json.load(schema)

            app.logger.info('file://' + schema_path + '/',
                            self.schema[version])
            self.resolver[version] = RefResolver('file://' + schema_path + '/',
                                                 self.schema[version])
            sem_mod = import_module('validation_api.schema.' + version +
                                    '.semantics')
            self.semantic_validators[version] = sem_mod.validation_rules
Beispiel #13
0
        def _validate(req, params, config=None):
            resolver = RefResolver("", "", store=schemas)

            if not query is None:
                _json_validate(req.params, query, resolver)

            if not request is None:
                _json_validate(req.json_body, request, resolver)

            res = func(req, params, config)

            if not response is None:
                if res.status_code >= 200 and res.status_code < 300:
                    _json_validate(res.json_body, response, resolver)
                elif res.status_code >= 400 and res.status_code < 600:
                    if not error_schema is None:
                        _json_validate(res.json_body, error_schema, resolver)

            return res
Beispiel #14
0
def main():
    # validate schema files
    meta_validator = Draft7Validator(json_shema_utils.load_schema("draft7"))
    process_schema_files(meta_validator, SCHEMA_DIR)
    check_errors("Invalid schema")

    # validate data files
    for schema_name in os.listdir(DATA_DIR):
        schema = get_schema(schema_name)
        resolver = RefResolver(base_uri=f'file://{SCHEMA_DIR}/',
                               referrer=schema)
        validator = Draft7Validator(schema, resolver=resolver)
        examples_dir = os.path.join(DATA_DIR, schema_name)
        process_schema_files(validator, examples_dir, count_name="data")
    check_errors("Invalid data")

    print(
        f"Successfully checked {counts['schema']} schema and {counts['data']} data files"
    )
Beispiel #15
0
    def __init__(
        self,
        residue_info: dict,
        schema: str = "lynx_residues",
        output_rules: dict = default_output_rules,
        nomenclature: str = "LipidLynxX",
        logger=app_logger,
    ):
        self.logger = logger
        self.export_rule = load_output_rule(output_rules, nomenclature)
        self.res_rule = self.export_rule.get("RESIDUES", None)
        self.res_rule_orders = self.res_rule.get("RESIDUE",
                                                 {}).get("ORDER", [])
        self.res_separators = self.export_rule.get("SEPARATORS", [])
        self.res_info = residue_info
        self.__replace_mdt__()

        self.schema = schema
        self.type = "FattyAcid"
        resolver = RefResolver(
            referrer=res_schema,
            base_uri=f"file://{os.path.dirname(res_schema_path)}/")
        self.validator = Draft7Validator(res_schema, resolver=resolver)

        mod_info = residue_info.get("MOD", {})

        self.mod_obj = Modifications(mod_info,
                                     num_o=residue_info.get("NUM_O", 0),
                                     nomenclature=nomenclature)
        self.sum_mod_info = self.mod_obj.sum_mod_info
        self.mod_level = self.mod_obj.mod_level
        self.res_level = self.mod_level
        if float(self.mod_level) > 0:
            self.is_modified = True
        else:
            self.is_modified = False

        if self.is_modified and self.sum_mod_info:
            self.linked_levels = self.sum_mod_info.get("linked_levels", ["0"])
        else:
            self.linked_levels = ["0"]

        self.linked_ids = self.__post_init__()
Beispiel #16
0
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))
Beispiel #17
0
    def __validate_json_schema(self, schema_filename):
        schema_dir = os.path.join(
            os.path.dirname(os.path.dirname(inspect.getmodule(self).__file__)),
            "config", "schemas")
        schema_file = os.path.join(schema_dir, schema_filename)
        resolver = RefResolver('file://' + schema_dir + "/", None)
        schema_obj = json.load(open(schema_file, "r"))
        instance_obj = json.load(open(self.filepath, "r"))

        result = {"status": Status.SUCCESS, "message": ""}
        try:
            jsonschema_validate(instance=instance_obj,
                                schema=schema_obj,
                                resolver=resolver)
        except ValidationError as e:
            result["status"] = Status.FAILURE,
            result["message"] = str(e)

        return result
Beispiel #18
0
def validate(json_data: json,
             schema_id: str,
             app_id: str = None,
             schema_store: dict = None,
             validate_schema: bool = False,
             schema_search_path: str = None
             ) -> Tuple[bool, iter]:
    """Load the json file and validate against loaded schema."""
    try:
        if not schema_search_path:
            schema_search_path = path.join(path.dirname(__file__), 'schemas')

        if not schema_store:
            schema_store = get_schema_store(validate_schema, schema_search_path)

        schema_uri = BASE_URI
        if app_id:
            schema_uri = schema_uri + '/' + app_id
        schema = schema_store.get(f'{schema_uri}/{schema_id}')
        if validate_schema:
            Draft7Validator.check_schema(schema)

        schema_file_path = path.join(schema_search_path, schema_id)
        resolver = RefResolver(f'file://{schema_file_path}.json', schema, schema_store)

        if Draft7Validator(schema,
                           format_checker=draft7_format_checker,
                           resolver=resolver
                           ) \
                .is_valid(json_data):
            return True, None

        errors = Draft7Validator(schema,
                                 format_checker=draft7_format_checker,
                                 resolver=resolver
                                 ) \
            .iter_errors(json_data)
        return False, errors

    except SchemaError as error:
        # handle schema error
        return False, error
Beispiel #19
0
    def validate_json(self, stac_content, stac_schema):
        """
        Validate STAC.
        :param stac_content: input STAC file content
        :param stac_schema of STAC (item, catalog, collection)
        :return: validation message
        """

        try:
            if "title" in stac_schema and "item" in stac_schema["title"].lower(
            ):
                logger.debug(
                    "Changing GeoJson definition to reference local file")
                # rewrite relative reference to use local geojson file
                stac_schema["definitions"]["core"]["allOf"][0]["oneOf"][0][
                    "$ref"] = ("file://" + self.dirpath +
                               "/geojson.json#definitions/feature")
            logging.info("Validating STAC")
            validate(stac_content, stac_schema)
            return True, None
        except RefResolutionError as error:
            # See https://github.com/Julian/jsonschema/issues/362
            # See https://github.com/Julian/jsonschema/issues/313
            # See https://github.com/Julian/jsonschema/issues/98
            try:
                self.fetch_spec("geojson")
                self.geojson_resolver = RefResolver(
                    base_uri=f"file://{self.dirpath}/geojson.json",
                    referrer="geojson.json")
                validate(stac_content,
                         stac_schema,
                         resolver=self.geojson_resolver)
                return True, None
            except Exception as error:
                logger.exception("A reference resolution error")
                return False, f"{error.args}"
        except ValidationError as error:
            logger.warning("STAC Validation Error")
            return False, f"{error.message} of {list(error.path)}"
        except Exception as error:
            logger.exception("STAC error")
            return False, f"{error}"
Beispiel #20
0
def validate_schema(schema_file, instance):
    """
    Validate json instance based on schema contained in schema_file. Need schema_file as opposed to loaded schema because we use the path of the schema file to resolve $ref's within it.

    :param schema_file: absolute path of schema file (including the file name)
    :param instance: loaded json instance
    :return: True if instance is valid, False
    """

    if not os.path.exists(schema_file):
        raise RuntimeError('Specified schema file does not exist: %s'%(schema_file))

    schema_path = os.path.dirname(schema_file)

    schema_uri = 'file:///{0}/'.format(schema_path)
    resolver = RefResolver(schema_uri, schema_file)

    with open(schema_file, 'r') as f:
        schema = json.load(f)

    try:
        # note returns None. Raises errors to indicate invalidity
        validate(instance, schema, resolver=resolver)
    except ve as e:
        # TODO: what should we actually do here. Pass an exception up?
        print('saw jsonschema.exceptions.ValidationError')
        print('e.message')
        print(e.message)

        if args.verbose:
            print('e.schema')
            print(e.schema)
            print('e.instance')
            print(e.instance)
            print('e.cause')
            print(e.cause)
            print('e.context')
            print(e.context)

        return False

    return True
Beispiel #21
0
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
Beispiel #22
0
 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
Beispiel #23
0
def add_lone_path(apifile, family, major_version, lone_bprint, version,
                  basedir, lone):
    """
    Add routes
    """
    with open(apifile) as infile:
        spec = json.load(infile)
        _LOG.info("Loading spec file - %s ", apifile)
        validpaths = list(spec['paths'].keys())
        _LOG.debug("validpaths in this lone is %r", validpaths)
        allowed_mime_types = get_mime_types(spec['components']['responses'])
        latest_version = lone_latest_version(basedir, family, lone)
        _LOG.debug("creating routes for %s", lone)
        base = "file://{0}/apischemas/openapi/".format(basedir)
        resolver = RefResolver(base_uri=base, referrer=spec)
        for path in validpaths:
            _LOG.debug("Path in validapath is %r", path)
            create_register_blueprint(lone_bprint, spec, version,
                                      latest_version, major_version, path,
                                      lone, allowed_mime_types, resolver)
    def init_app(self, app):
        path = os.path.split(os.path.realpath(__file__))
        app_dir = os.path.split(path[0])[0]
        for version in SCHEMA_VERSIONS:
            schema_path = os.path.join(app_dir, SCHEMA_RELATIVE_DIRECTORY,
                                       version)
            with open(os.path.join(schema_path, SCHEMA_FILENAME)) as simple:
                self.schema[version] = json.load(simple)

            # Get file prefix based on OS
            if os.name == 'nt':
                file_prefix = 'file:///'
            else:
                file_prefix = 'file:'

            self.resolver[version] = RefResolver(
                file_prefix + schema_path + '/', self.schema[version])
            sem_mod = import_module('local_land_charges_api_stub.schema.' +
                                    version + '.semantics')
            self.semantic_validators[version] = sem_mod.validation_rules
Beispiel #25
0
def validate(obj_descr, schema_name):
    """Check that the description is a valid data interface description.

    Args:
        obj_descr: (dict)
        schema_name: (str) name of object

    Returns:
        (bool) - true if description match node json schema
    """
    with open(os.path.join(here, "schema_%s.json" % schema_name), 'r') as fs:
        schema = json.load(fs)

    if here.startswith("/"):
        base_uri = "file:///%s/" % (here[1:])
    else:
        base_uri = "file:///%s/" % here
    refres = RefResolver(base_uri, schema)
    val = Draft4Validator(schema, resolver=refres)
    return val.is_valid(obj_descr)
Beispiel #26
0
    def __init__(self, **data: Any):
        """
        Initialize derived fields
        """
        super().__init__(**data)

        fid = self.schema_filename.split(".")[-2]
        json_schema_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            f"json_schema/{fid}-spec/json-schema",
        )
        self.schema_path: str = os.path.join(json_schema_path,
                                             self.schema_filename)
        self.resolver = RefResolver("file://" + json_schema_path + "/", None)
        with open(self.schema_path) as fp_schema:
            self.c_schema = json.load(fp_schema)

        # Check if stac_node_validator is available
        self.stac_node_validator_available = which(
            "stac-node-validator") is not None
Beispiel #27
0
    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

        """
        experiment_schema_path = resource_filename(package_name,
                                                   "schema/experiment.json")
        package_root = os.path.dirname(os.path.dirname(experiment_schema_path))
        base_uri = 'file://' + package_root + '/'
        resolver = RefResolver(base_uri, schema)
        return Draft4Validator(schema, resolver=resolver)
Beispiel #28
0
    def validate(self, loaded_user_configuration: Any) -> str:
        """Perform validation on the configuration against supported schemas.

    :param loaded_user_configuration: A Python object containing user config.
    :returns: The first configuration schema version that passes validation.
    :raises: :class:`jsonschema.exceptions.ValidationError`
    """

        validation_exception = exceptions.ValidationError("No schema found!")

        for version, schema in self.schemas.items():
            try:
                resolver = RefResolver("file://" + str(self._schema_folder),
                                       None)
                validate(loaded_user_configuration, schema, resolver=resolver)
                return version
            except exceptions.ValidationError as last_exception:
                validation_exception = last_exception

        raise validation_exception
Beispiel #29
0
def load_validator(schema_path, schema):
    """Create a JSON schema validator for the given schema.

    Args:
        schema_path: The filename of the JSON schema.
        schema: A Python object representation of the same schema.

    Returns:
        An instance of Draft4Validator.

    """
    # Get correct prefix based on OS
    if os.name == 'nt':
        file_prefix = 'file:///'
    else:
        file_prefix = 'file:'

    resolver = RefResolver(file_prefix + schema_path.replace("\\", "/"), schema)
    validator = Draft4Validator(schema, resolver=resolver)

    return validator
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" % filename)
    except SchemaError:
        raise
    if base_uri:
        resolver = RefResolver(base_uri=base_uri, referrer=filename)
    else:
        resolver = None
    return Draft4Validator(schema=schema, resolver=resolver)