Esempio n. 1
0
    def test_roster_config_validate(self):
        try:
            jsonschema.validate(
                {"target-1": {"host": "localhost", "user": "******", "passwd": "foo"}},
                ssh_schemas.RosterItem.serialize(),
                format_checker=jsonschema.FormatChecker(),
            )
        except jsonschema.exceptions.ValidationError as exc:
            self.fail("ValidationError raised: {0}".format(exc))

        with self.assertRaises(jsonschema.exceptions.ValidationError) as excinfo:
            jsonschema.validate(
                {
                    salt.utils.stringutils.to_str("target-1:1"): {
                        "host": "localhost",
                        "user": "******",
                        "passwd": "foo",
                    }
                },
                ssh_schemas.RosterItem.serialize(),
                format_checker=jsonschema.FormatChecker(),
            )
        if JSONSCHEMA_VERSION < _LooseVersion("2.6.0"):
            self.assertIn(
                "Additional properties are not allowed ('target-1:1' was unexpected)",
                excinfo.exception.message,
            )
        else:
            self.assertIn(
                "'target-1:1' does not match any of the regexes",
                excinfo.exception.message,
            )
Esempio n. 2
0
 def validateLocalJSONSchema(self, list_error=False):
     """
 Validate contained JSON with the Schema defined in the Portal Type.
 """
     portal = self.getPortalObject()
     defined_schema = portal.portal_types[
         self.getPortalType()].getTextContent()
     text_content = self.asJSONText()
     if not defined_schema or text_content is None:
         # No errors if nothing is defined
         return True
     defined_schema = json.loads(defined_schema)
     current_schema = json.loads(text_content)
     try:
         jsonschema.validate(current_schema,
                             defined_schema,
                             format_checker=jsonschema.FormatChecker())
     except jsonschema.exceptions.ValidationError as err:
         if list_error:
             validator = jsonschema.validators.validator_for(
                 defined_schema)(defined_schema,
                                 format_checker=jsonschema.FormatChecker())
             return sorted(validator.iter_errors(current_schema),
                           key=lambda e: e.path)
         return err
     return True
Esempio n. 3
0
    def wrapper(self, req, resp, *args, **kwargs):
        if req_schema is not None:
            try:
                jsonschema.validate(req.media,
                                    req_schema,
                                    format_checker=jsonschema.FormatChecker())
            except jsonschema.ValidationError as e:
                raise falcon.HTTPBadRequest('Request data failed validation',
                                            description=e.message)

        result = func(self, req, resp, *args, **kwargs)

        if resp_schema is not None:
            try:
                jsonschema.validate(resp.media,
                                    resp_schema,
                                    format_checker=jsonschema.FormatChecker())
            except jsonschema.ValidationError:
                raise falcon.HTTPInternalServerError(
                    'Response data failed validation'
                    # Do not return 'e.message' in the response to
                    # prevent info about possible internal response
                    # formatting bugs from leaking out to users.
                )

        return result
Esempio n. 4
0
 def validate_draft7logging(self, jsonObj, details, schema_version):
     try:
         #logger.entry('#__errors__')
         jsonschema.Draft7Validator(
             self.schema,
             format_checker=jsonschema.FormatChecker()).validate(jsonObj)
         jsonschema.validate(jsonObj,
                             self.schema,
                             format_checker=jsonschema.FormatChecker())
         tag = self.obj_id(details)
         print('#__validates__', tag, schema_version)
         return True, {
             tag: {
                 'errors': [],
                 'ok': True,
                 'version': schema_version
             }
         }
     except jsonschema.ValidationError as err:
         tag = self.obj_id(details)
         errors = verbose_error(self.schema, jsonObj, tag)
         return False, {
             tag: {
                 'errors': errors,
                 'error_type': 'jsonschema',
                 'ok': False,
                 'version': schema_version
             }
         }
Esempio n. 5
0
    def test_roster_config_validate(self):
        try:
            jsonschema.validate(
                {
                    'target-1': {
                        'host': 'localhost',
                        'user': '******',
                        'passwd': 'foo'
                    }
                },
                ssh_schemas.RosterItem.serialize(),
                format_checker=jsonschema.FormatChecker())
        except jsonschema.exceptions.ValidationError as exc:
            self.fail('ValidationError raised: {0}'.format(exc))

        with self.assertRaises(
                jsonschema.exceptions.ValidationError) as excinfo:
            jsonschema.validate(
                {
                    'target-1:1': {
                        'host': 'localhost',
                        'user': '******',
                        'passwd': 'foo'
                    }
                },
                ssh_schemas.RosterItem.serialize(),
                format_checker=jsonschema.FormatChecker())
        if JSONSCHEMA_VERSION < _LooseVersion('2.6.0'):
            self.assertIn(
                'Additional properties are not allowed (\'target-1:1\' was unexpected)',
                excinfo.exception.message)
        else:
            self.assertIn('\'target-1:1\' does not match any of the regexes',
                          excinfo.exception.message)
Esempio n. 6
0
def validate_json(json_obj):
    with open(SCHEMA_PATH) as s:
        json_schema = json.load(s)
    # first validate schema file
    v = jsonschema.Draft4Validator(
        json_schema,
        format_checker=jsonschema.FormatChecker(),
    )
    # now validate json file
    try:
        jsonschema.validate(
            json_obj,
            json_schema,
            format_checker=jsonschema.FormatChecker(),
        )
        logger.info("JSON schema validation passed.")
        return True
    except jsonschema.exceptions.ValidationError:
        errors = [e for e in v.iter_errors(json_obj)]
        logger.info(f"The file is not valid. Total json schema errors: {len(errors)}")
        for i, error in enumerate(errors, 1):
            logger.error(
                f"{i} Validation error in {'.'.join(str(v) for v in error.path)}: {error.message}",
            )
        logger.info("JSON schema validation failed.")
        return False
Esempio n. 7
0
    def test_roster_config_validate(self):
        try:
            jsonschema.validate(
                {
                    'target-1': {
                        'host': 'localhost',
                        'user': '******',
                        'passwd': 'foo'
                    }
                },
                ssh_schemas.RosterItem.serialize(),
                format_checker=jsonschema.FormatChecker())
        except jsonschema.exceptions.ValidationError as exc:
            self.fail('ValidationError raised: {0}'.format(exc))

        with self.assertRaises(
                jsonschema.exceptions.ValidationError) as excinfo:
            jsonschema.validate(
                {
                    'target-1:1': {
                        'host': 'localhost',
                        'user': '******',
                        'passwd': 'foo'
                    }
                },
                ssh_schemas.RosterItem.serialize(),
                format_checker=jsonschema.FormatChecker())
        self.assertIn(
            'Additional properties are not allowed (\'target-1:1\' was unexpected)',
            excinfo.exception.message)
Esempio n. 8
0
        async def wrapper(endpoint, session, request, *args, **kwargs):
            if hasattr(request_cls, '__body__'):
                # Return a 400 if the request body does not meet the
                # required schema
                # Body values come as arrays of length 1 so turn
                # them into single values
                set_defaults(request_cls.__body__, request.json)
                try:
                    jsonschema.validate(
                        request.json or {},
                        request_cls.__body__,
                        format_checker=jsonschema.FormatChecker())
                except jsonschema.ValidationError as err:
                    logger.exception(
                        'request body does not fit schema for resource %s',
                        request_cls.__name__)
                    return response.json({'error': err}, status=400)

            if hasattr(request_cls, '__params__'):
                # Return a 400 if the request params do not meet the
                # required schema
                # Params values always come as arrays of length 1 so turn
                # them into single values
                for key in request.args:
                    request.args[key] = request.args.get(key)
                set_defaults(request_cls.__params__, request.args)
                try:
                    jsonschema.validate(
                        request.args,
                        request_cls.__params__,
                        format_checker=jsonschema.FormatChecker())
                except jsonschema.ValidationError as err:
                    logger.exception(
                        'request params do not fit schema for resource %s',
                        request_cls.__name__)
                    return response.json({'error': err}, status=400)

            # Call the request handler
            result = await coro(endpoint, session, request, *args, **kwargs)

            if hasattr(response_cls, '__body__'):
                # Return a 500 if the response does not meet the required
                # format and raise an error
                body_no_defaults = json.loads(result.body)
                body = set_defaults(response_cls.__body__, body_no_defaults)
                # TODO: is this the right type of encoding
                result.body = bytes(json.dumps(body), 'utf-8')
                try:
                    jsonschema.validate(
                        body,
                        response_cls.__body__,
                        format_checker=jsonschema.FormatChecker())
                except jsonschema.ValidationError as err:
                    logger.exception(
                        'response body does not fit schema for resource %s',
                        response_cls.__name__)
                    return response.json({'error': err}, status=500)

            return result
Esempio n. 9
0
def make_validator():
    global top_validator, client_validator
    top_schema, client_schema = schema_data["top_schema"], schema_data[
        "client_schema"]
    default_validating = extend_with_default(jsonschema.Draft4Validator)
    top_validator = default_validating(
        top_schema, format_checker=jsonschema.FormatChecker())
    client_validator = default_validating(
        client_schema, format_checker=jsonschema.FormatChecker())
Esempio n. 10
0
def make_validator():
    global server_validator, config_validator
    server_schema, config_schema = schema_data["servers"], schema_data[
        "config"]
    default_validating = extend_with_default(jsonschema.Draft4Validator)
    server_validator = default_validating(
        server_schema, format_checker=jsonschema.FormatChecker())
    config_validator = default_validating(
        config_schema, format_checker=jsonschema.FormatChecker())
Esempio n. 11
0
    def test_uuid_format(self):
        with self.assertRaises(jsonschema.ValidationError):
            jsonschema.validate("not a uuid", {
                "type": "string",
                "format": "uuid"
            },
                                format_checker=jsonschema.FormatChecker())

        jsonschema.validate(str(uuid.uuid4()), {
            "type": "string",
            "format": "uuid"
        },
                            format_checker=jsonschema.FormatChecker())
Esempio n. 12
0
        async def wrapper(endpoint, request, *args, **kwargs):
            if hasattr(request_cls, '__body__'):
                # Return a 400 if the request body does not meet the
                # required schema
                try:
                    jsonschema.validate(
                        request.json or {},
                        request_cls.__body__,
                        format_checker=jsonschema.FormatChecker())
                except jsonschema.ValidationError as err:
                    logger.exception(
                        'request body does not fit schema for resource %s',
                        request_cls.__name__)
                    return response.json({'error': err}, status=400)

            if hasattr(request_cls, '__params__'):
                # Return a 400 if the request params do not meet the
                # required schema
                # Params values always come as arrays of length 1 so turn
                # them into single values
                params = {key: request.args.get(key) for key in request.args}
                try:
                    jsonschema.validate(
                        params,
                        request_cls.__params__,
                        format_checker=jsonschema.FormatChecker())
                except jsonschema.ValidationError as err:
                    logger.exception(
                        'request params do not fit schema for resource %s',
                        request_cls.__name__)
                    return response.json({'error': err}, status=400)

            # Call the request handler
            result = await coro(endpoint, request, *args, **kwargs)

            if hasattr(response_cls, '__body__'):
                # Return a 500 if the response does not meet the required
                # format and raise an error
                try:
                    jsonschema.validate(
                        json.loads(result.body),
                        response_cls.__body__,
                        format_checker=jsonschema.FormatChecker())
                except jsonschema.ValidationError as err:
                    logger.exception(
                        'response body does not fit schema for resource %s',
                        response_cls.__name__)
                    return response.json({'error': err}, status=500)

            return result
Esempio n. 13
0
def parse_data(data):
    result = data[3]
    method = data[1]
    format_ = data[4]
    try:
        if method == 'input' and format_ == 'json':
            parsed_data = [
                json.loads(line) for line in result.strip().split('\n')
            ]
            # assert all([set(obj).issuperset({"user", "ts", "context", "ip"}) for obj in parsed_data]), "Unknown JSON format" # JSON objects validation
            for dat in parsed_data:
                jsonschema.validate(dat,
                                    schema,
                                    format_checker=jsonschema.FormatChecker())
            return parsed_data, method
        if method == 'reward' and format_ == 'csv':
            reader = csv.reader(result.strip().split('\n'))
            header = next(reader)
            reader = list(reader)
            assert ((header == ["user", "ts", "reward_id", "reward_money"])
                    & all([len(row) in (3, 4) for row in reader])
                    ), "Unknown CSV format"  # csv rows length validation
            parsed_data = [
                (int(r[0]), datetime.datetime.fromtimestamp(float(r[1])),
                 int(r[2])) if len(r) == 3 else
                (int(r[0]), datetime.datetime.fromtimestamp(float(r[1])),
                 int(r[2]), int(r[3])) for r in reader
            ]  # type convertion
            return parsed_data[1:], method
        return data[:4] + (
            'Unknown datatype', ), 'error'  # unknown type or format
    except Exception as e:  # parse error
        return tuple(
            data[:4]) + ("{}: {}".format(*sys.exc_info()[:2]), ), 'error'
def validateValueForComplexDataformat(value, dataFormat, isArray):
    """Validate the event value to match the specified complex data format

    Args:
        value (:obj:): The value of the event to be validated
        dataFormat (:obj:): The (complex) data format of the event
        isArray (bool): Specifies wether this event will be added to cache if MSB is currently not reachable
    """
    schema = {}
    if isArray:
        schema["items"] = {}
        schema["items"]["$ref"] = dataFormat["dataObject"]["items"]["$ref"]
        schema["type"] = "array"
    else:
        schema["$ref"] = {}
        schema["$ref"] = dataFormat["dataObject"]["$ref"]
        schema["type"] = "object"
    schema["definitions"] = dataFormat
    try:
        jsonschema.validate(
            value,
            schema,
            format_checker=jsonschema.FormatChecker(),
        )
        return True
    except Exception as e:
        logging.error("Error validating event: " + str(e))
        return False
Esempio n. 15
0
def validate_files(
    schemafile: PathLike,
    *datafiles: PathLike,
    encoding: str = "utf-8",
) -> None:
    r"""
	Validate the given datafiles against the given schema.

	:param schemafile: The ``json`` or ``yaml`` formatted schema to validate with.
	:param \*datafiles: The ``json`` or ``yaml`` files to validate.
	:param encoding: Encoding to open the files with.

	.. versionadded:: 0.4.0
	"""

    schemafile = pathlib.Path(schemafile)

    yaml = YAML(typ="safe", pure=True)
    schema = yaml.load(schemafile.read_text(encoding=encoding))

    for filename in datafiles:
        for document in yaml.load_all(
                pathlib.Path(filename).read_text(encoding=encoding)):
            try:
                jsonschema.validate(document,
                                    schema,
                                    format_checker=jsonschema.FormatChecker())
            except jsonschema.exceptions.ValidationError as e:
                e.filename = str(filename)
                raise e
Esempio n. 16
0
def _validate_message_request():
    """Validates message request data

    returns deserialized data if valid."""
    if flask.request.content_type != 'application/json':
        flask.abort(415)
    data = flask.request.get_json()
    if not data:
        raise BadRequestException("Missing request data")
    schema = {
        "type": "object",
        "properties": {
            "to": {
                "type": "string",
                "format": "email"
            },
            "subject": {
                "type": "string",
                "maxLength": app.config['MAX_SUBJECT_SIZE']
            },
            "text": {
                "type": "string",
                "maxLength": app.config['MAX_TEXT_SIZE']
            }
        },
        "required": ["to", "subject", "text"]
    }
    try:
        jsonschema.validate(data,
                            schema,
                            format_checker=jsonschema.FormatChecker())
    except jsonschema.ValidationError:
        raise BadRequestException("Invalid JSON.  Check required properties.")
    return data
Esempio n. 17
0
def require_schema(schema):
    """This decorator verifies that request JSON matches given JSONSchema.

    http://json-schema.org
    """

    validator = jsonschema.Draft4Validator(
        schema,
        format_checker=jsonschema.FormatChecker()
    )

    def outer_decorator(func):
        @functools.wraps(func)
        def inner_decorator(self, *args, **kwargs):
            errors = validator.iter_errors(self.request_json)
            errors = [err.message for err in errors]

            if errors:
                LOG.warning("Cannot validate request: %s", errors)
                raise exceptions.InvalidJSONError(errors)

            return func(self, *args, **kwargs)

        return inner_decorator
    return outer_decorator
Esempio n. 18
0
    def validate(self, descriptor):

        # Other profiles
        if self.name != 'table-schema':
            return jsonschema.validate(descriptor, self.jsonschema)

        # Collect errors
        errors = []
        validator = _TableSchemaValidator(
            self.jsonschema, format_checker=jsonschema.FormatChecker())
        for error in validator.iter_errors(descriptor):
            if isinstance(error, jsonschema.exceptions.ValidationError):
                message = str(error.message)
                if six.PY2:
                    message = message.replace('u\'', '\'')
                descriptor_path = '/'.join(map(str, error.path))
                profile_path = '/'.join(map(str, error.schema_path))
                error = exceptions.ValidationError(
                    'Descriptor validation error: %s '
                    'at "%s" in descriptor and '
                    'at "%s" in profile'
                    % (message, descriptor_path, profile_path))
            errors.append(error)

        # Railse error
        if errors:
            message = 'There are %s validation errors (see exception.errors)' % len(errors)
            raise exceptions.ValidationError(message, errors=errors)

        return True
Esempio n. 19
0
 def validate(self):
     format_check = jsonschema.FormatChecker()
     try:
         jsonschema.validate(
             self._snapcraft, self._schema, format_checker=format_check)
     except jsonschema.ValidationError as e:
         raise errors.SnapcraftSchemaError.from_validation_error(e)
Esempio n. 20
0
    def validate_json(self, data, filename, jsonType):
        logger.debug("Validating %s JSON." % (jsonType))

        schema_file_name = None
        if jsonType == 'BGI':
            schema_file_name = 'schemas/gene/geneMetaData.json'

        with open(schema_file_name) as schema_file:
            schema = json.load(schema_file)

        # Defining a resolver for relative paths and schema issues, see https://github.com/Julian/jsonschema/issues/313
        # and https://github.com/Julian/jsonschema/issues/274
        sSchemaDir = os.path.dirname(os.path.abspath(schema_file_name))
        oResolver = js.RefResolver(base_uri = 'file://' + sSchemaDir + '/', referrer = schema)

        try:
            js.validate(data, schema, format_checker=js.FormatChecker(), resolver=oResolver)
            logger.debug("'%s' successfully validated against '%s'" % (filename, schema_file_name))
        except js.ValidationError as e:
            logger.info(e.message)
            logger.info(e)
            raise SystemExit("FATAL ERROR in JSON validation.")
        except js.SchemaError as e:
            logger.info(e.message)
            logger.info(e)
            raise SystemExit("FATAL ERROR in JSON validation.")
Esempio n. 21
0
    def validate_config_schema(self):
        """Config schema validation

        Exception:
            If schema validation fails
        """

        schema = SchemaDefinition.get_schema(ordered=True)
        try:
            validate(self.config,
                     schema,
                     format_checker=jsonschema.FormatChecker())
        except jsonschema.exceptions.ValidationError as error:
            if error.cause is None:
                path = None
                for index, element in enumerate(error.path):
                    if isinstance(element, int):
                        path += '[{}]'.format(element)
                    else:
                        if index == 0:
                            path = '{}'.format(element)
                        else:
                            path += '.{}'.format(element)
                exc = 'Schema validation failed - {} - {}'.format(
                    path, error.message)
            else:
                exc = 'Schema validation failed - {} - {}'.format(
                    error.cause, error.message)
            if 'Additional properties are not allowed' in error.message:
                raise UserException(exc)
            else:
                raise UserCriticalException(exc)
Esempio n. 22
0
 def validate_json(cls, js):
     """
     Do validation of a dictionary that has been loaded from (or will be written to) a JSON
     """
     jsonschema.validate(js,
                         cls.schema(),
                         format_checker=jsonschema.FormatChecker())
Esempio n. 23
0
def validate(spec):
    try:
        import jsonschema
        import strict_rfc3339  # validate date-time format in jsonschema
    except ImportError:
        print("to validate install jsonschema and strict_rfc3339")
        return

    from jsonstat.schema import JsonStatSchema

    if not isinstance(spec, dict):
        json_data = json.loads(spec, object_pairs_hook=OrderedDict)
    else:
        json_data = spec
    if "version" not in json_data:
        raise JsonStatException("cannot validate jsonstat version < 2.0")
    # schema = JsonStatSchema()
    jsonstat_schema_url = "https://json-stat.org/format/schema/2.0/"

    # TODO: caching jsonschema (now it is downloaded everytime)
    global __json_schema__
    if __json_schema__ is None:
        downloader = Downloader(cache_dir=None)
        __json_schema__ = downloader.download(jsonstat_schema_url)

    schema = json.loads(__json_schema__)
    validator = jsonschema.Draft7Validator(
        schema, format_checker=jsonschema.FormatChecker())
    # validator.validate(json_data)
    errors = sorted(validator.iter_errors(json_data), key=lambda e: e.path)
    return len(errors) == 0
Esempio n. 24
0
    def test_validate_lsd(self):
        """
        Validate a License Status Document
        """

        with open(self.config.status_schema_path) as schema_file:
            lsd_json_schema = json.loads(schema_file.read())
            try:
                jsonschema.validate(self.lsd,
                                    lsd_json_schema,
                                    format_checker=jsonschema.FormatChecker())
            except jsonschema.ValidationError as err:
                raise TestSuiteRunningError(err)

        LOGGER.debug("The License Status Document is valid")

        # these are required properties -> if absent, has been spotted by the schema validation
        LOGGER.info("The status of the license is: %s", self.lsd['status'])
        LOGGER.info("The user message is: %s", self.lsd['message'])

        if 'potential_rights' in self.lsd:
            LOGGER.info("The potential rights datetime is: %s",
                        self.lsd['potential_rights']['end'])
        else:
            LOGGER.info("No potential rights datetime in the status document")
        if 'events' in self.lsd:
            for event in self.lsd['events']:
                LOGGER.info(
                    "Event: type {}, timestamp {}, id {}, name {}".format(
                        event['type'], event['timestamp'], event['id'],
                        event['name']))
        else:
            LOGGER.info("No events in the status document")
Esempio n. 25
0
 def validate_schema(self, payload, schema):
     """
     Validate the payload under the given schema.
     Raises an exception if the payload (or schema itself) is invalid
     """
     checker = jsonschema.FormatChecker(["ipv4", "ipv6", "uri"])
     jsonschema.validate(payload, schema, format_checker=checker)
Esempio n. 26
0
def list_traits(req):
    context = req.environ['placement.context']
    filters = {}

    try:
        jsonschema.validate(dict(req.GET), LIST_TRAIT_SCHEMA,
                            format_checker=jsonschema.FormatChecker())
    except jsonschema.ValidationError as exc:
        raise webob.exc.HTTPBadRequest(
            _('Invalid query string parameters: %(exc)s') %
            {'exc': exc})

    if 'name' in req.GET:
        filters = _normalize_traits_qs_param(req.GET['name'])
    if 'associated' in req.GET:
        if req.GET['associated'].lower() not in ['true', 'false']:
            raise webob.exc.HTTPBadRequest(
                explanation=_('The query parameter "associated" only accepts '
                              '"true" or "false"'))
        filters['associated'] = (
            True if req.GET['associated'].lower() == 'true' else False)

    traits = rp_obj.TraitList.get_all(context, filters)
    req.response.status = 200
    req.response.body = encodeutils.to_utf8(
        jsonutils.dumps(_serialize_traits(traits)))
    req.response.content_type = 'application/json'
    return req.response
Esempio n. 27
0
def validator_for_interface(name):
    if name not in INTERFACE_SCHEMAS:
        return None
    return jsonschema.Draft4Validator(
        INTERFACE_SCHEMAS[name],
        types={'array': (list, tuple)},
        format_checker=jsonschema.FormatChecker())
Esempio n. 28
0
def validate(value, schema, set_defaults=True):
    orig = jsonschema.Draft4Validator.VALIDATORS['properties']

    def validate_and_default(validator, properties, instance, schema):
        for property, subschema in six.iteritems(properties):
            if 'default' in subschema:
                if callable(subschema['default']):
                    instance.setdefault(property, subschema['default']())
                else:
                    instance.setdefault(property,
                                        copy.deepcopy(subschema['default']))

        for error in orig(validator, properties, instance, schema):
            yield error

    validator_cls = jsonschema.validators.extend(
        jsonschema.Draft4Validator,
        {'properties': validate_and_default
         }) if set_defaults else jsonschema.Draft4Validator

    validator_cls(schema,
                  types={
                      'array': (list, tuple)
                  },
                  format_checker=jsonschema.FormatChecker()).validate(
                      value, schema)
Esempio n. 29
0
 def validate(self, json, schema_name):
     """
     校验方法
     :param json: type(str) 请求json
     :param schema_name: type(str) schema名称
     :return: (boolean, dict)
             如果校验通过返回 (True, );如果校验不通过 (False, err)
             err = {
                 "key_name":"",
                 "key_path":"",
                 "msg": ""
             }
     :type json: dict
     :type schema_name: str
     :rtype (bool, str)
     """
     err = {"key_name": "", "key_path": "", "msg": ""}
     try:
         jsonschema.validate(json,
                             self.schema[schema_name],
                             format_checker=jsonschema.FormatChecker())
         return True, err
     except ValidationError, e:
         key_name = ""
         if e.path:
             key_name = e.path.pop().replace(".", "")
         key_path = str(list(e.path)).replace("[", "").replace(
             "]", "").replace(", ", ".").replace("'", "")
         msg = e.message
         err['key_name'] = key_name
         err['key_path'] = key_path
         err['msg'] = msg
         return False, err
Esempio n. 30
0
def validate_against_schema(j, js, longerr):
    isValid = True
    es = []
    #-- lazy validation to catch as many as possible
    myvalidator = jsonschema.Draft7Validator(js, format_checker=jsonschema.FormatChecker())
    for err in sorted(myvalidator.iter_errors(j), key=str):
        isValid = False
        if (longerr == False) and (len(err.relative_path) > 0) and (err.relative_path[0] == 'CityObjects'):
            a = "CityObject is not schema-valid: " + str(err.relative_path[1])
            es.append(a)
        else:
            es.append(err.message)
    return (isValid, es)

    # try:
    #     jsonschema.Draft4Validator(js, format_checker=jsonschema.FormatChecker()).validate(j)
    # except jsonschema.ValidationError as e:
    #     raise Exception(e.message)
    #     return False
    # except jsonschema.SchemaError as e:
    #     raise Exception(e.message)
    #     return False
    
    # try:
    #     jsonschema.validate(j, js, format_checker=jsonschema.FormatChecker())
    # except jsonschema.ValidationError as e:
    #     raise Exception(e.message)
    #     return False
    # except jsonschema.SchemaError as e:
    #     raise Exception(e.message)
    #     return False