Python jsonschema.ValidationError() Examples

The following are code examples for showing how to use jsonschema.ValidationError(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: validators.py    Apache License 2.0 6 votes vote down vote up
def validate(self, *args, **kwargs):
        try:
            self.validator.validate(*args, **kwargs)
        except jsonschema.ValidationError as ex:
            if isinstance(ex.cause, exception.InvalidName):
                detail = ex.cause.format_message()
            elif len(ex.path) > 0:
                # NOTE: For whole OpenStack message consistency, this error
                #       message has been written as the similar format of WSME.
                detail = _("Invalid input for field/attribute %(path)s."
                           " Value: %(value)s. %(message)s") % {
                               'path': ex.path.pop(), 'value': ex.instance,
                               'message': ex.message
                           }
            else:
                detail = ex.message
            raise exception.ValidationError(detail=detail)
        except TypeError as ex:
            # NOTE: If passing non string value to patternProperties parameter,
            #       TypeError happens. Here is for catching the TypeError.
            detail = six.text_type(ex)
            raise exception.ValidationError(detail=detail) 
Example 2
Project: zun   Author: openstack   File: validators.py    Apache License 2.0 6 votes vote down vote up
def validate(self, *args, **kwargs):
        try:
            self.validator.validate(*args, **kwargs)
        except jsonschema.ValidationError as ex:
            if len(ex.path) > 0:
                if self.is_body:
                    detail = _("Invalid input for field '%(path)s'."
                               "Value: '%(value)s'. %(message)s")
                else:
                    detail = _("Invalid input for query parameters "
                               "'%(path)s'. Value: '%(value)s'. %(message)s")
                detail = detail % {
                    'path': ex.path.pop(), 'value': ex.instance,
                    'message': six.text_type(ex)
                }
            else:
                detail = ex.message
            raise exception.SchemaValidationError(detail=detail) 
Example 3
Project: tornado-restful-template   Author: smileboywtu   File: handler.py    Apache License 2.0 6 votes vote down vote up
def get(self, *args, **kwargs):
        """
        get counter number
        
        """
        if not self.request.query_arguments:
            raise ServerException("params_err", "need params")

        data = decode_to_string(self.request.query_arguments)

        try:
            self.validate_arguments(self.request.method, data)
        except ValidationError as e:
            raise ServerException("params_err", str(e))

        cache = RedisTK()
        number = await cache.get(data["name"])
        number = int(number) if number else 0
        self.json("success", data={
            "number": int(number)
        }) 
Example 4
Project: misp42splunk   Author: remg427   File: test_cli.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_unsuccessful_validation(self):
        error = ValidationError("I am an error!", instance=1)
        stdout, stderr = StringIO(), StringIO()
        exit_code = cli.run(
            {
                "validator": fake_validator([error]),
                "schema": {},
                "instances": [1],
                "error_format": "{error.instance} - {error.message}",
            },
            stdout=stdout,
            stderr=stderr,
        )
        self.assertFalse(stdout.getvalue())
        self.assertEqual(stderr.getvalue(), "1 - I am an error!")
        self.assertEqual(exit_code, 1) 
Example 5
Project: misp42splunk   Author: remg427   File: test_cli.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_unsuccessful_validation_multiple_instances(self):
        first_errors = [
            ValidationError("9", instance=1),
            ValidationError("8", instance=1),
        ]
        second_errors = [ValidationError("7", instance=2)]
        stdout, stderr = StringIO(), StringIO()
        exit_code = cli.run(
            {
                "validator": fake_validator(first_errors, second_errors),
                "schema": {},
                "instances": [1, 2],
                "error_format": "{error.instance} - {error.message}\t",
            },
            stdout=stdout,
            stderr=stderr,
        )
        self.assertFalse(stdout.getvalue())
        self.assertEqual(stderr.getvalue(), "1 - 9\t1 - 8\t2 - 7\t")
        self.assertEqual(exit_code, 1) 
Example 6
Project: misp42splunk   Author: remg427   File: test_validators.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_it_delegates_to_a_legacy_ref_resolver(self):
        """
        Legacy RefResolvers support only the context manager form of
        resolution.

        """

        class LegacyRefResolver(object):
            @contextmanager
            def resolving(this, ref):
                self.assertEqual(ref, "the ref")
                yield {"type" : "integer"}

        resolver = LegacyRefResolver()
        schema = {"$ref" : "the ref"}

        with self.assertRaises(ValidationError):
            self.validator_class(schema, resolver=resolver).validate(None) 
Example 7
Project: core   Author: getavalon   File: test_cli.py    MIT License 6 votes vote down vote up
def test_unsuccessful_validation(self):
        error = ValidationError("I am an error!", instance=1)
        stdout, stderr = StringIO(), StringIO()
        exit_code = cli.run(
            {
                "validator": fake_validator([error]),
                "schema": {},
                "instances": [1],
                "error_format": "{error.instance} - {error.message}",
            },
            stdout=stdout,
            stderr=stderr,
        )
        self.assertFalse(stdout.getvalue())
        self.assertEqual(stderr.getvalue(), "1 - I am an error!")
        self.assertEqual(exit_code, 1) 
Example 8
Project: core   Author: getavalon   File: test_cli.py    MIT License 6 votes vote down vote up
def test_unsuccessful_validation_multiple_instances(self):
        first_errors = [
            ValidationError("9", instance=1),
            ValidationError("8", instance=1),
        ]
        second_errors = [ValidationError("7", instance=2)]
        stdout, stderr = StringIO(), StringIO()
        exit_code = cli.run(
            {
                "validator": fake_validator(first_errors, second_errors),
                "schema": {},
                "instances": [1, 2],
                "error_format": "{error.instance} - {error.message}\t",
            },
            stdout=stdout,
            stderr=stderr,
        )
        self.assertFalse(stdout.getvalue())
        self.assertEqual(stderr.getvalue(), "1 - 9\t1 - 8\t2 - 7\t")
        self.assertEqual(exit_code, 1) 
Example 9
Project: social-relay   Author: jaywink   File: poll_pods.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def get_pod_relay_preferences(self, host):
        """Query remote pods on https first, fall back to http."""
        logging.info("Querying %s" % host)
        try:
            try:
                response = requests.get("https://%s/.well-known/x-social-relay" % host,
                                timeout=5,
                                headers={"User-Agent": config.USER_AGENT})
            except timeout:
                response = None
            if not response or response.status_code != 200:
                response = requests.get("http://%s/.well-known/x-social-relay" % host,
                                timeout=5,
                                headers={"User-Agent": config.USER_AGENT})
                if response.status_code != 200:
                    return None
        except (ConnectionError, Timeout, timeout):
            return None
        try:
            # Make sure we have a valid x-social-relay doc
            validate(response.json(), self.schema)
            return response.text
        except (ValueError, ValidationError):
            return None 
Example 10
Project: solar   Author: Mirantis   File: validation.py    Apache License 2.0 6 votes vote down vote up
def validate_input(value, jsonschema=None, schema=None):
    """Validate single input according to schema.

    :param value: Value to be validated
    :param schema: Dict in jsonschema format
    :param schema: Our custom, simplified schema
    :return: list with errors
    """
    if jsonschema is None:
        jsonschema = construct_jsonschema(schema)
    try:
        validate(value, jsonschema)
    except ValidationError as e:
        return [e.message]
    except Exception as e:
        log.error('jsonschema: %s', jsonschema)
        log.error('value: %s', value)
        log.exception(e)
        raise 
Example 11
Project: insights_connexion   Author: RedHatInsights   File: app.py    MIT License 6 votes vote down vote up
def error_middleware(request, handler):
    try:
        response = await handler(request)
    except(ValidationError) as e:
        log.error(e)
        response = responses.invalid_request_parameters()
    except(UniqueViolationError) as e:
        log.error(e)
        response = responses.resource_exists()
    except(NoResultFound) as e:
        log.error(e)
        response = responses.not_found()
    except(Exception) as e:
        log.error(e)
        response = responses.internal_server_error()

    return response 
Example 12
Project: schemathesis   Author: kiwicom   File: checks.py    MIT License 6 votes vote down vote up
def response_schema_conformance(response: GenericResponse, case: "Case") -> None:
    if not response.headers["Content-Type"].startswith("application/json"):
        return
    # the keys should be strings
    responses = {str(key): value for key, value in case.endpoint.definition.get("responses", {}).items()}
    status_code = str(response.status_code)
    if status_code in responses:
        definition = responses[status_code]
    elif "default" in responses:
        definition = responses["default"]
    else:
        # No response defined for the received response status code
        return
    schema = case.endpoint.schema._get_response_schema(definition)
    if not schema:
        return
    if isinstance(response, requests.Response):
        data = response.json()
    else:
        data = response.json
    try:
        jsonschema.validate(data, schema)
    except jsonschema.ValidationError as exc:
        raise AssertionError(f"The received response does not conform to the defined schema!\n\nDetails: \n\n{exc}") 
Example 13
Project: holepunch   Author: CypherpunkArmory   File: subdomains.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def subdomain_reserve():
    try:
        json_schema_manager.validate(request.json, "subdomain_create.json")
        current_user = User.query.filter_by(uuid=get_jwt_identity()).first_or_404()
        subdomain = SubdomainCreationService(
            current_user, dig(request.json, "data/attributes/name")
        ).reserve(True)

        return json_api(subdomain, SubdomainSchema), 200
    except ValidationError:
        return (
            json_api(BadRequest(detail="Request does not match schema."), ErrorSchema),
            400,
        )
    except SubdomainLimitReached:
        return json_api(SubdomainLimitReached, ErrorSchema), 403
    except SubdomainTaken:
        return json_api(SubdomainTaken, ErrorSchema), 400
    except SubdomainError:
        return json_api(SubdomainError, ErrorSchema), 500 
Example 14
Project: holepunch   Author: CypherpunkArmory   File: account.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def create_token():
    try:
        json_schema_manager.validate(request.json, "token.json")
        email = dig(request.json, "data/attributes/email", None)
        token_type = dig(request.json, "data/type", None)
        user = User.query.filter_by(email=email).first_or_404()
        uns = UserNotification(user)

        if token_type == "email_confirm":
            uns.activation_emails()
        elif token_type == "password_reset":
            uns.password_reset_email()

    except ValidationError as e:
        return json_api(BadRequest(source=e.message), ErrorSchema), 400
    except NotFound:
        return "", 200

    return "", 200 
Example 15
Project: holepunch   Author: CypherpunkArmory   File: account.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def update_user():
    """ Update an existing User Record"""

    try:
        json_schema_manager.validate(request.json, "user_update.json")

        current_user = User.query.filter_by(uuid=get_jwt_identity()).first_or_404()

        service = UserUpdate(
            current_user,
            scopes=authentication.jwt_scopes(),
            attrs=dig(request.json, "data/attributes", {}),
            rels=dig(request.json, "data/relationships", {}),
        )
        updated_user = service.update()

        return json_api(updated_user, UserSchema), 200
    except UserError as e:
        return json_api(e, ErrorSchema), 422
    except AccessDenied as e:
        return json_api(e, ErrorSchema), 403
    except ValidationError as e:
        return json_api(BadRequest(source=e.message), ErrorSchema), 400 
Example 16
Project: flask-template   Author: adrianocanofre   File: helpers.py    MIT License 6 votes vote down vote up
def __call__(self, original_func):
        def wrappee(*args, **kwargs):
            try:
                if not hasattr(request, 'json'):
                    raise ValidationError('No json provided.')
                validate(request.json, json.loads(self.json_schema))
            except ValidationError as ex:
                app.log.error("json error. The json body is invalid.")
                return {"error": {
                            "error_code": "EX020C",
                            "error_description": "The json body is invalid."
                            }
                        }, 500
            return original_func(*args, **kwargs)

        return wrappee 
Example 17
Project: pywhdfs   Author: yassineazzouz   File: config.py    MIT License 6 votes vote down vote up
def __init__(self, path=None):
      self.path = path or os.getenv('WEBHDFS_CONFIG', self.default_path)
      if osp.exists(self.path):
        try:
          self.config = json.loads(open(self.path).read())
          self.schema = json.loads(resource_string(__name__, 'resources/config_schema.json'))
          #self.schema = open("resources/schema.config").read()
          try:
            js.validate(self.config, self.schema)
          except js.ValidationError as e:
            print e.message
          except js.SchemaError as e:
            print e

        except ParsingError:
          raise HdfsError('Invalid configuration file %r.', self.path)

        _logger.info('Instantiated configuration from %r.', self.path)
      else:
        raise HdfsError('Invalid configuration file %r.', self.path) 
Example 18
Project: openeo-openshift-driver   Author: Open-EO   File: validator.py    Apache License 2.0 6 votes vote down vote up
def validate_args(self, process_id, args_payload, args_specs):
        ''' Validates the arguments of a node '''

        for arg_id, arg_spec in args_specs.items():
            # Check if not argument is another node
            if arg_id == "imagery": 
                self.validate_node(args_payload[arg_id])
                continue

            # Check if product is available at the back-end
            if arg_id == "data_id" and not any(p['data_id'] == args_payload[arg_id] for p in self.products):
                raise ValidationError("Product '{0}' is not available.".format(args_payload[arg_id]))

            # Validate argument using JSON Schema
            if arg_id in args_payload:
                validate(args_payload[arg_id], arg_spec["schema"])
                continue

            # Raise error if argument is required but missing.
            if args_specs[arg_id]["required"] == True:
                raise ValidationError("Argument '{0}' is missing for Process '{1}'.".format(arg_id, process_id)) 
Example 19
Project: nova   Author: ZhanHan   File: validators.py    Apache License 2.0 6 votes vote down vote up
def validate(self, *args, **kwargs):
        try:
            self.validator.validate(*args, **kwargs)
        except jsonschema.ValidationError as ex:
            if isinstance(ex.cause, exception.InvalidName):
                detail = ex.cause.format_message()
            elif len(ex.path) > 0:
                # NOTE: For whole OpenStack message consistency, this error
                #       message has been written as the similar format of WSME.
                detail = _("Invalid input for field/attribute %(path)s."
                           " Value: %(value)s. %(message)s") % {
                               'path': ex.path.pop(), 'value': ex.instance,
                               'message': ex.message
                           }
            else:
                detail = ex.message
            raise exception.ValidationError(detail=detail)
        except TypeError as ex:
            # NOTE: If passing non string value to patternProperties parameter,
            #       TypeError happens. Here is for catching the TypeError.
            detail = six.text_type(ex)
            raise exception.ValidationError(detail=detail) 
Example 20
Project: nova   Author: ZhanHan   File: util.py    Apache License 2.0 6 votes vote down vote up
def extract_json(body, schema):
    """Extract JSON from a body and validate with the provided schema."""
    try:
        data = jsonutils.loads(body)
    except ValueError as exc:
        raise webob.exc.HTTPBadRequest(
            _('Malformed JSON: %(error)s') % {'error': exc},
            json_formatter=json_error_formatter)
    try:
        jsonschema.validate(data, schema,
                            format_checker=jsonschema.FormatChecker())
    except jsonschema.ValidationError as exc:
        raise webob.exc.HTTPBadRequest(
            _('JSON does not validate: %(error)s') % {'error': exc},
            json_formatter=json_error_formatter)
    return data 
Example 21
Project: nova   Author: ZhanHan   File: allocation.py    Apache License 2.0 6 votes vote down vote up
def _extract_allocations(body, schema):
    """Extract allocation data from a JSON body."""
    try:
        data = jsonutils.loads(body)
    except ValueError as exc:
        raise webob.exc.HTTPBadRequest(
            _('Malformed JSON: %(error)s') % {'error': exc},
            json_formatter=util.json_error_formatter)
    try:
        jsonschema.validate(data, schema,
                            format_checker=jsonschema.FormatChecker())
    except jsonschema.ValidationError as exc:
        raise webob.exc.HTTPBadRequest(
            _('JSON does not validate: %(error)s') % {'error': exc},
            json_formatter=util.json_error_formatter)
    return data 
Example 22
Project: scrapy-cluster   Author: istresearch   File: rest_service.py    MIT License 6 votes vote down vote up
def validate_schema(schema_name):
    """Validate the JSON against a required schema_name."""
    def decorator(f):
        @wraps(f)
        def wrapper(*args, **kw):
            instance = args[0]
            try:
                instance.validator(instance.schemas[schema_name]).validate(request.get_json())
            except ValidationError, e:
                ret_dict = instance._create_ret_object(instance.FAILURE,
                                                       None, True,
                                                       instance.BAD_SCHEMA,
                                                       e.message)
                instance.logger.error("Invalid Schema", ret_dict)
                return jsonify(ret_dict), 400
            instance.logger.debug("Schema is valid")
            return f(*args, **kw)
        return wrapper 
Example 23
Project: PatchCLI   Author: brysontyrrell   File: __init__.py    MIT License 6 votes vote down vote up
def validate_patch(data, schema=None):
    """Takes a dictionary object and validates it against a JSON schema.

    :param dict data: The JSON to validate as a dictionary object.
    :param str schema: Which schema to validate against. Valid options are:
        patch or version.

    :raises: InvalidPatchDefinitionError
    """
    if schema not in ('patch', 'version'):
        raise ValueError("Argument 'schema' must be 'patch' or 'version'")

    if schema == 'patch':
        use_schema = patch_schema
    else:
        use_schema = version_schema

    try:
        validate(data, use_schema)
    except ValidationError as error:
        message = "Validation error encountered with submitted JSON: {} for " \
                  "item: /{}".format(
                      str(error.message),
                      '/'.join([str(i) for i in error.path]))
        raise InvalidPatchDefinitionError(message) 
Example 24
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: users.py    MIT License 6 votes vote down vote up
def post(self):
        """Enroll a new user"""
        req_data = request.get_json()
        try:
            validated = validate_user(req_data)
            user_data = validated['data']
            db_user = User.query.filter_by(email=user_data['email']).first()
            email = user_data['email']
            if not db_user:
                user = User(username=user_data['username'],
                            email=email, password=generate_password_hash(user_data['password']))
                db.session.add(user)
                db.session.commit()
                return make_response({'ok': True, 'users': user.to_json()}, 201)
            else:
                raise Conflict('ERROR: Existed user!')

        except ValidationError as e:
            app.logger.error('ERROR: {0}\n{1}'.format(e.message, req_data))
            raise BadRequest(e.message) 
Example 25
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: users.py    MIT License 6 votes vote down vote up
def post(self):
        """user signin"""
        req_data = request.get_json()
        try:
            signin_data = validate_user(req_data)['data']
            db_user = db.session.query(User).filter_by(email=signin_data['email']).first()
            if db_user is None:
                app.logger.error('Not existed user!')
                raise BadRequest('Not existed user!')
            else:
                if check_password_hash(db_user.password, signin_data['password']):
                    token_data = {'user_id': db_user.id, 'username': db_user.username, 'email': db_user.email}
                    access_token = create_access_token(identity=token_data)
                    refresh_token = create_refresh_token(identity=token_data)
                    res = jsonify({'accessToken': access_token, 'refreshToken': refresh_token})
                    app.logger.debug('success:user signin:{}'.format(token_data))
                    return make_response(res, 200)
                else:
                    app.logger.error('Password is mismatched or invalid user: {0}'.format(signin_data))
                    raise BadRequest('Password is mismatched or invalid user')

        except ValidationError as e:
            app.logger.error('ERROR: {0}\n{1}'.format(e.message, req_data))
            raise BadRequest(e.message) 
Example 26
Project: jsonrpcclient   Author: bcb   File: test_parse.py    MIT License 5 votes vote down vote up
def test_parse_invalid_jsonrpc():
    with pytest.raises(ValidationError):
        parse('{"json": "2.0"}', batch=False) 
Example 27
Project: swaggerit   Author: dutradda   File: method.py    MIT License 5 votes vote down vote up
def _build_body_params(self, req):
        content_type = req.headers.get('content-type')
        if content_type is None and req.body is not None:
            body, _ = await self._cast_body(req.body)
            raise ValidationError('Request content_type is missing', instance=body)

        elif self._body_required and req.body is None:
            raise ValidationError('Request body is missing', instance=req.body)

        elif content_type is not None and 'application/json' in content_type:
            if req.body is None:
                raise ValidationError(
                    "Request body must be setted when 'content-type' header is setted",
                    instance=req.body
                )

            body, error = await self._cast_body(req.body)

            if not self._has_body_parameter:
                raise ValidationError('Request body is not acceptable', instance=body)

            if error is not None:
                raise ValidationError(str(error), instance=body)

            if self._body_validator:
                self._body_validator.validate(body)

            return body

        else:
            return req.body 
Example 28
Project: controller   Author: deis   File: serializers.py    MIT License 5 votes vote down vote up
def validate_procfile(self, data):
        for key, value in data.items():
            if value is None or value == "":
                raise serializers.ValidationError("Command can't be empty for process type")

            if not re.match(PROCTYPE_MATCH, key):
                raise serializers.ValidationError(PROCTYPE_MISMATCH_MSG)

        return data 
Example 29
Project: controller   Author: deis   File: serializers.py    MIT License 5 votes vote down vote up
def validate_memory(self, data):
        for key, value in data.items():
            if value is None:  # use NoneType to unset an item
                continue

            if not re.match(PROCTYPE_MATCH, key):
                raise serializers.ValidationError(PROCTYPE_MISMATCH_MSG)

            if not re.match(MEMLIMIT_MATCH, str(value)):
                raise serializers.ValidationError(
                    "Memory limit format: <number><unit> or <number><unit>/<number><unit>, "
                    "where unit = B, K, M or G")

        return data 
Example 30
Project: controller   Author: deis   File: serializers.py    MIT License 5 votes vote down vote up
def validate_cpu(self, data):
        for key, value in data.items():
            if value is None:  # use NoneType to unset an item
                continue

            if not re.match(PROCTYPE_MATCH, key):
                raise serializers.ValidationError(PROCTYPE_MISMATCH_MSG)

            shares = re.match(CPUSHARE_MATCH, str(value))
            if not shares:
                raise serializers.ValidationError(
                    "CPU limit format: <value> or <value>/<value>, where value must be a numeric")

        return data 
Example 31
Project: controller   Author: deis   File: serializers.py    MIT License 5 votes vote down vote up
def validate_tags(self, data):
        for key, value in data.items():
            if value is None:  # use NoneType to unset an item
                continue

            # split key into a prefix and name
            if '/' in key:
                prefix, name = key.split('/')
            else:
                prefix, name = None, key

            # validate optional prefix
            if prefix:
                if len(prefix) > 253:
                    raise serializers.ValidationError(
                        "Tag key prefixes must 253 characters or less.")

                for part in prefix.split('/'):
                    if not re.match(TAGVAL_MATCH, part):
                        raise serializers.ValidationError(
                            "Tag key prefixes must be DNS subdomains.")

            # validate required name
            if not re.match(TAGVAL_MATCH, name):
                raise serializers.ValidationError(
                    "Tag keys must be alphanumeric or \"-_.\", and 1-63 characters.")

            # validate value if it isn't empty
            if value and not re.match(TAGVAL_MATCH, str(value)):
                raise serializers.ValidationError(
                    "Tag values must be alphanumeric or \"-_.\", and 1-63 characters.")

        return data 
Example 32
Project: controller   Author: deis   File: serializers.py    MIT License 5 votes vote down vote up
def validate_healthcheck(self, data):
        for procType, healthcheck in data.items():
            if healthcheck is None:
                continue
            for key, value in healthcheck.items():
                if value is None:
                    continue
                if key not in ['livenessProbe', 'readinessProbe']:
                    raise serializers.ValidationError(
                        "Healthcheck keys must be either livenessProbe or readinessProbe")
                try:
                    jsonschema.validate(value, PROBE_SCHEMA)
                except jsonschema.ValidationError as e:
                    raise serializers.ValidationError(
                        "could not validate {}: {}".format(value, e.message))

            # http://kubernetes.io/docs/api-reference/v1/definitions/#_v1_probe
            # liveness only supports successThreshold=1, no other value
            # This is not in the schema since readiness supports other values
            threshold = jmespath.search('livenessProbe.successThreshold', healthcheck)
            if threshold is not None and threshold != 1:
                raise serializers.ValidationError(
                    'livenessProbe successThreshold can only be 1'
                )

        return data 
Example 33
Project: controller   Author: deis   File: serializers.py    MIT License 5 votes vote down vote up
def validate_whitelist(self, data):
        for address in data:
            try:
                ipaddress.ip_address(address)
            except:
                try:
                    ipaddress.ip_network(address)
                except:
                    try:
                        ipaddress.ip_interface(address)
                    except:
                        raise serializers.ValidationError(
                           "The address {} is not valid".format(address))

        return data 
Example 34
Project: controller   Author: deis   File: serializers.py    MIT License 5 votes vote down vote up
def validate_autoscale(self, data):
        schema = {
            "$schema": "http://json-schema.org/schema#",
            "type": "object",
            "properties": {
                # minimum replicas autoscale will keep resource at based on load
                "min": {"type": "integer"},
                # maximum replicas autoscale will keep resource at based on load
                "max": {"type": "integer"},
                # how much CPU load there is to trigger scaling rules
                "cpu_percent": {"type": "integer"},
            },
            "required": ["min", "max", "cpu_percent"],
        }

        for proc, autoscale in data.items():
            if autoscale is None:
                continue

            try:
                jsonschema.validate(autoscale, schema)
            except jsonschema.ValidationError as e:
                raise serializers.ValidationError(
                    "could not validate {}: {}".format(autoscale, e.message)
                )

        return data 
Example 35
Project: polls-api   Author: apiaryio   File: views.py    MIT License 5 votes vote down vote up
def post(self, request):
        if not can_create_question(self.request):
            return self.http_method_not_allowed(request)

        try:
            body = json.loads(request.body)
        except ValueError:
            return HttpResponse(status=400)

        try:
            jsonschema.validate(body, self.request_body_schema)
        except jsonschema.ValidationError:
            return HttpResponse(status=400)

        question_text = body.get('question')
        choices = body.get('choices')

        question, created = self.get_or_create(question_text, choices)
        resource = self.resource()
        resource.obj = question
        resource.request = request
        response = resource.get(request)
        if created:
            response.status_code = 201
        response['Location'] = resource.get_uri()
        return response 
Example 36
Project: signals   Author: Amsterdam   File: extra_properties.py    Mozilla Public License 2.0 5 votes vote down vote up
def __call__(self, value):
        feature_flag = settings.FEATURE_FLAGS.get('API_VALIDATE_EXTRA_PROPERTIES', False)
        if not feature_flag:
            # Feature flag not enabled, validation disabled
            return value

        try:
            validate(instance=value, schema=self.schema)
        except JSONSchemaValidationError:
            # Transform jsonschema ValidationError to DRF ValidationError
            raise ValidationError()
        else:
            return value 
Example 37
Project: tornado-restful-template   Author: smileboywtu   File: tests.py    Apache License 2.0 5 votes vote down vote up
def test_validate_get(self):
        data = {
            "name": 132
        }
        with self.assertRaises(ValidationError):
            validate(data, schemas["get"])

        data = {
            "name": "zhangsan"
        }
        validate(data, schemas["get"]) 
Example 38
Project: misp42splunk   Author: remg427   File: loader.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def load(self, definition, schema_file, context):
        """Load cloud connect configuration from a `dict` and validate
        it with schema and global settings will be rendered.
        :param schema_file: Schema file location used to validate config.
        :param definition: A dictionary contains raw configs.
        :param context: variables to render template in global setting.
        :return: A `Munch` object.
        """
        try:
            validate(definition, self._get_schema_from_file(schema_file))
        except ValidationError:
            raise ConfigException(
                'Failed to validate interface with schema: {}'.format(
                    traceback.format_exc()))

        try:
            global_settings = self._load_global_setting(
                definition.get('global_settings'), context
            )

            requests = [self._load_request(item) for item in definition['requests']]

            return munchify({
                'meta': munchify(definition['meta']),
                'tokens': definition['tokens'],
                'global_settings': global_settings,
                'requests': requests,
            })
        except Exception as ex:
            error = 'Unable to load configuration: %s' % str(ex)
            _logger.exception(error)
            raise ConfigException(error) 
Example 39
Project: misp42splunk   Author: remg427   File: test_format.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 40
Project: misp42splunk   Author: remg427   File: test_validators.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def message_for(self, instance, schema, *args, **kwargs):
        kwargs.setdefault("cls", Draft3Validator)
        with self.assertRaises(ValidationError) as e:
            validate(instance, schema, *args, **kwargs)
        return e.exception.message 
Example 41
Project: normandy   Author: mozilla   File: validators.py    Mozilla Public License 2.0 5 votes vote down vote up
def _required(validator, requirements, instance, schema):
    """Validate 'required' properties."""
    if not validator.is_type(instance, "object"):
        return

    for index, requirement in enumerate(requirements):
        if instance.get(requirement, "") == "":
            error = jsonschema.ValidationError("This field may not be blank.", path=[requirement])
            yield error


# Construct validator as extension of Json Schema Draft 4. 
Example 42
Project: normandy   Author: mozilla   File: validators.py    Mozilla Public License 2.0 5 votes vote down vote up
def validate_json(value):
    """
    Validate that a given value can be successfully parsed as JSON.
    """
    try:
        json.loads(value)
    except json.JSONDecodeError as err:
        raise ValidationError("%s is not valid JSON: %s", params=(value, err.msg)) 
Example 43
Project: core   Author: getavalon   File: test_format.py    MIT License 5 votes vote down vote up
def test_format_error_causes_become_validation_error_causes(self):
        checker = FormatChecker()
        checker.checks("foo", raises=ValueError)(self.fn)
        cause = self.fn.side_effect = ValueError()
        validator = Draft4Validator({"format" : "foo"}, format_checker=checker)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")

        self.assertIs(cm.exception.__cause__, cause) 
Example 44
Project: core   Author: getavalon   File: test_validators.py    MIT License 5 votes vote down vote up
def message_for(self, instance, schema, *args, **kwargs):
        kwargs.setdefault("cls", Draft3Validator)
        with self.assertRaises(ValidationError) as e:
            validate(instance, schema, *args, **kwargs)
        return e.exception.message 
Example 45
Project: core   Author: getavalon   File: test_validators.py    MIT License 5 votes vote down vote up
def test_it_delegates_to_a_ref_resolver(self):
        resolver = RefResolver("", {})
        schema = {"$ref" : mock.Mock()}

        @contextmanager
        def resolving():
            yield {"type": "integer"}

        with mock.patch.object(resolver, "resolving") as resolve:
            resolve.return_value = resolving()
            with self.assertRaises(ValidationError):
                self.validator_class(schema, resolver=resolver).validate(None)

        resolve.assert_called_once_with(schema["$ref"]) 
Example 46
Project: async-pluct   Author: globocom   File: resource.py    MIT License 5 votes vote down vote up
def is_valid(self):
        handlers = {'https': self.session_request_json,
                    'http': self.session_request_json}
        schema = await self.schema.raw_schema
        resolver = RefResolver.from_schema(schema,
                                           handlers=handlers)
        try:
            validate(self.data, schema, resolver=resolver)
        except (SchemaError, ValidationError):
            return False
        return True 
Example 47
Project: lightweight-rest-tester   Author: ridi   File: test_single.py    MIT License 5 votes vote down vote up
def test_get_unexpected_json_schema(self):
        json_file = '%s/resources/test_function_single_get_unexpected_json_schema.json' % self.current_dir_path
        test_function_list = self.generate_test_functions(json_file)

        with self.assertRaises(ValidationError):
            run_test_function_list(test_function_list, self) 
Example 48
Project: lightweight-rest-tester   Author: ridi   File: test_multiple.py    MIT License 5 votes vote down vote up
def test_put_and_get_unexpected_json_schema(self):
        json_file = '%s/resources/test_function_multiple_put_and_get_unexpected_json_schema.json' % \
                    self.current_dir_path
        test_function_list = self.generate_test_function(json_file)

        with self.assertRaises(ValidationError):
            run_test_function_list(test_function_list, self) 
Example 49
Project: holepunch   Author: CypherpunkArmory   File: account.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def register_user():
    """ Create a new User Record"""
    try:
        json_schema_manager.validate(request.json, "user_create.json")

        user = UserCreation(
            email=dig(request.json, "data/attributes/email"),
            password=dig(request.json, "data/attributes/password"),
        ).create()

        return json_api(user, UserSchema), 204
    except UserError as e:
        return json_api(e, ErrorSchema), 422
    except ValidationError as e:
        return json_api(BadRequest(source=e.message), ErrorSchema), 401 
Example 50
Project: holepunch   Author: CypherpunkArmory   File: tunnels.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def start_tunnel() -> Tuple[Response, int]:
    try:
        json_schema_manager.validate(request.json, "tunnel_create.json")

        subdomain_id = dig(request.json, "data/relationships/subdomain/data/id")
        port_types = dig(request.json, "data/attributes/port")
        ssh_key = dig(request.json, "data/attributes/sshKey")

        current_user = User.query.filter_by(uuid=get_jwt_identity()).first_or_404()
        try:
            tunnel_info = TunnelCreationService(
                current_user, subdomain_id, port_types, ssh_key
            ).create()
        except SubdomainLimitReached:
            return json_api(SubdomainLimitReached, ErrorSchema), 403
        except TunnelLimitReached:
            return json_api(TunnelLimitReached, ErrorSchema), 403
        except TunnelError:
            return json_api(TunnelError, ErrorSchema), 500

        return json_api(tunnel_info, TunnelSchema), 201

    except ValidationError as e:
        return json_api(BadRequest(detail=e.message), ErrorSchema), 400

    except AccessDenied:
        return json_api(AccessDenied, ErrorSchema), 403

    except SubdomainInUse:
        return json_api(SubdomainInUse, ErrorSchema), 403 
Example 51
Project: holepunch   Author: CypherpunkArmory   File: admin.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def tunnel_admin() -> Tuple[Response, int]:
    """
    Stop any currently running tunnel if you are an admin
    """
    current_user = User.query.filter_by(uuid=get_jwt_identity()).first_or_404()
    if current_user.tier != "admin":
        return json_api(NotFoundError, ErrorSchema), 404

    try:
        json_schema_manager.validate(request.json, "admin_tunnel.json")

        subdomain_name = dig(request.json, "data/attributes/subdomainName")
        reason = dig(request.json, "data/attributes/reason")
    except ValidationError as e:
        return json_api(BadRequest(source=e.message), ErrorSchema), 400

    subdomain = Subdomain.query.filter_by(name=subdomain_name).first_or_404()
    tunnel = Tunnel.query.filter_by(subdomain_id=subdomain.id).first_or_404()
    try:
        TunnelDeletionService(current_user, tunnel).delete()
        logger.info(
            "%s deleted %s.holepunch.io for reason %s",
            current_user.email,
            subdomain_name,
            reason,
        )
        return make_response(""), 204
    except TunnelError:
        return json_api(TunnelError, ErrorSchema), 500 
Example 52
Project: holepunch   Author: CypherpunkArmory   File: json.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def is_valid(self, data: Union[str, dict], schema_name):
        try:
            validate(self, data, schema_name)
        except ValidationError:
            return False 
Example 53
Project: telemetry   Author: jupyter   File: test_register_schema.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_record_event_badschema():
    """
    Fail fast when an event doesn't conform to its schema
    """
    schema = {
        '$id': 'test/test',
        'version': 1,
        'properties': {
            'something': {
                'type': 'string'
            },
            'status': {
                'enum': ['success', 'failure']
            }
        }
    }

    el = EventLog(handlers=[logging.NullHandler()])
    el.register_schema(schema)
    el.allowed_schemas = ['test/test']

    with pytest.raises(jsonschema.ValidationError):
        el.record_event('test/test', 1, {
            'something': 'blah',
            'status': 'not-in-enum'
        }) 
Example 54
Project: dcatd   Author: Amsterdam   File: dcat.py    Mozilla Public License 2.0 5 votes vote down vote up
def full_text_search_representation(self, data: T.Iterable, prop_filter:set):
        """We must check whether the given data is really a list, jsonld may
        flatten lists."""
        if type(data) is list:
            ftsr = (
                self.item_type.full_text_search_representation(v, prop_filter)
                for v in data if v is not None
            )
            retval = '\n\n'.join(v for v in ftsr if v is not None)
            return retval if len(retval) > 0 else None
        return self.item_type.full_text_search_representation(data, prop_filter)


# class OneOf(Type):
#     def __init__(self, *types, **kwargs):
#         super().__init__(**kwargs)
#         self.types = list(types)
#
#     def schema(self, method: str) -> dict:
#         retval = dict(super().schema(method))  # Important: makes a shallow copy
#         retval['oneOf'] = [v.schema(method) for v in self.types]
#         return retval
#
#     def validate(self, data, method) -> Type:
#         for type in self.types:
#             try:
#                 jsonschema.validate(data, self.schema(method))
#                 return type
#             except jsonschema.ValidationError:
#                 pass
#         raise jsonschema.ValidationError("Not valid for any type")
#
#     def full_text_search_representation(self, data: T.Any, prop_filter: set):
#         raise NotImplementedError()
#
#     def canonicalize(self, data: T.Any, **kwargs):
#         return self.validate(data).canonicalize(data, **kwargs) 
Example 55
Project: stac-validator   Author: sparkgeo   File: stac_validator.py    Apache License 2.0 5 votes vote down vote up
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}" 
Example 56
Project: rucio   Author: rucio   File: icecube.py    Apache License 2.0 5 votes vote down vote up
def validate_schema(name, obj):
    """
    Validate object against json schema

    :param name: The json schema name.
    :param obj: The object to validate.
    """
    try:
        if obj:
            validate(obj, SCHEMAS.get(name, {}))
    except ValidationError as error:  # NOQA, pylint: disable=W0612
        raise InvalidObject("Problem validating %(name)s : %(error)s" % locals()) 
Example 57
Project: rucio   Author: rucio   File: domatpc.py    Apache License 2.0 5 votes vote down vote up
def validate_schema(name, obj):
    """
    Validate object against json schema

    :param name: The json schema name.
    :param obj: The object to validate.
    """
    try:
        if obj:
            validate(obj, SCHEMAS.get(name, {}))
    except ValidationError as error:  # NOQA, pylint: disable=W0612
        raise InvalidObject("Problem validating %(name)s : %(error)s" % locals()) 
Example 58
Project: rucio   Author: rucio   File: generic.py    Apache License 2.0 5 votes vote down vote up
def validate_schema(name, obj):
    """
    Validate object against json schema

    :param name: The json schema name.
    :param obj: The object to validate.
    """
    try:
        if obj:
            validate(obj, SCHEMAS.get(name, {}))
    except ValidationError as error:  # NOQA, pylint: disable=W0612
        raise InvalidObject("Problem validating %(name)s : %(error)s" % locals()) 
Example 59
Project: rucio   Author: rucio   File: belleii.py    Apache License 2.0 5 votes vote down vote up
def validate_schema(name, obj):
    """
    Validate object against json schema

    :param name: The json schema name.
    :param obj: The object to validate.
    """
    try:
        if obj:
            validate(obj, SCHEMAS.get(name, {}))
    except ValidationError as error:  # NOQA, pylint: disable=W0612
        raise InvalidObject("Problem validating %(name)s : %(error)s" % locals()) 
Example 60
Project: rucio   Author: rucio   File: atlas.py    Apache License 2.0 5 votes vote down vote up
def validate_schema(name, obj):
    """
    Validate object against json schema

    :param name: The json schema name.
    :param obj: The object to validate.
    """
    try:
        if obj:
            validate(obj, SCHEMAS.get(name, {}))
    except ValidationError as error:  # NOQA, pylint: disable=W0612
        raise InvalidObject("Problem validating %(name)s : %(error)s" % locals()) 
Example 61
Project: openeo-openshift-driver   Author: Open-EO   File: validator.py    Apache License 2.0 5 votes vote down vote up
def validate_node(self, node_payload):
        ''' Validates a single node '''

        if not "process_id" in node_payload:
            raise ValidationError("Node is missing a ProcessID or ProductID.")

        process_id = node_payload["process_id"]
        process_spec = [p for p in self.processes if p['name'] == process_id]

        if len(process_spec) != 1:
            raise ValidationError("Process '{0}' is not available.".format(node_payload["process_id"]))

        self.validate_args(process_id, node_payload, process_spec[0]["parameters"]) 
Example 62
Project: nova   Author: ZhanHan   File: server_tags.py    Apache License 2.0 5 votes vote down vote up
def update(self, req, server_id, id, body):
        context = req.environ["nova.context"]
        context.can(st_policies.POLICY_ROOT % 'update')
        self._check_instance_in_valid_state(context, server_id, 'update tag')

        try:
            jsonschema.validate(id, parameter_types.tag)
        except jsonschema.ValidationError as e:
            msg = (_("Tag '%(tag)s' is invalid. It must be a string without "
                     "characters '/' and ','. Validation error message: "
                     "%(err)s") % {'tag': id, 'err': e.message})
            raise webob.exc.HTTPBadRequest(explanation=msg)

        try:
            tags = objects.TagList.get_by_resource_id(context, server_id)
        except exception.InstanceNotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.format_message())

        if len(tags) >= objects.instance.MAX_TAG_COUNT:
            msg = (_("The number of tags exceeded the per-server limit %d")
                   % objects.instance.MAX_TAG_COUNT)
            raise webob.exc.HTTPBadRequest(explanation=msg)

        if id in _get_tags_names(tags):
            # NOTE(snikitin): server already has specified tag
            return webob.Response(status_int=204)

        tag = objects.Tag(context=context, resource_id=server_id, tag=id)

        try:
            tag.create()
        except exception.InstanceNotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.format_message())

        response = webob.Response(status_int=201)
        response.headers['Location'] = self._view_builder.get_location(
            req, server_id, id)
        return response 
Example 63
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: users.py    MIT License 5 votes vote down vote up
def post(self):
        """Enroll a new user"""
        req_data = request.get_json()
        try:
            validated = validate_user(req_data)
            user_data = validated['data']
            user = cognito_signup(user_data)
            app.logger.debug('success: enroll user into Cognito user pool:{}'.format(user))
            return make_response({'ok': True, 'users': user}, 201)

        except ValidationError as e:
            app.logger.error('ERROR:invalid signup data format:{0}'.format(req_data))
            app.logger.error(e)
            raise BadRequest(e.message) 
Example 64
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: users.py    MIT License 5 votes vote down vote up
def post(self):
        """user signin"""
        req_data = request.get_json()
        client = boto3.client('cognito-idp')
        try:
            signin_data = validate_user(req_data)['data']
            access_token, refresh_token = cognito_signin(client, signin_data)
            res = jsonify({'accessToken': access_token, 'refreshToken': refresh_token})
            app.logger.debug('success:user signin:access_token:{}, refresh_token:{}'.format(access_token, refresh_token))
            return make_response(res, 200)

        except client.exceptions.UserNotFoundException as e:
            app.logger.error('User does not exist: {0}'.format(signin_data))
            app.logger.error(e)
            raise BadRequest('User does not exist')
        except client.exceptions.NotAuthorizedException as e:
            app.logger.error('Password is mismatched or invalid user: {0}'.format(signin_data))
            app.logger.error(e)
            raise BadRequest('Password is mismatched or invalid user')
        except ValidationError as e:
            app.logger.error('Invalid data format: {0}'.format(req_data))
            app.logger.error(e)
            raise BadRequest(e.message)
        except Exception as e:
            app.logger.error('Unexpected error: {0}'.format(req_data))
            app.logger.error(e)
            raise InternalServerError('Unexpected error: {0}'.format(req_data)) 
Example 65
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: users.py    MIT License 5 votes vote down vote up
def post(self):
        """Enroll a new user"""
        req_data = request.get_json()
        try:
            validated = validate_user(req_data)
            user_data = validated['data']
            exist_user = [item for item in User.email_index.query(user_data['email'])]
            if not exist_user:
                new_user_id = uuid.uuid4().hex
                solution_put_new_user(new_user_id, user_data)

                user = {
                    'id': new_user_id,
                    'username': user_data['username'],
                    'email': user_data['email']
                }

                app.logger.debug('success:user_signup: {0}'.format(user))
                return make_response({'ok': True, 'users': user}, 201)
            else:
                app.logger.error('ERROR: Existed user: {0}'.format(user_data))
                raise Conflict('ERROR: Existed user!')
        except ValidationError as e:
            app.logger.error('ERROR: {0}\n{1}'.format(e.message, req_data))
            raise BadRequest(e.message)
        except PynamoDBException as e:
            app.logger.error('ERROR: {0}\n{1}'.format(e.msg, req_data))
            raise InternalServerError(e.msg) 
Example 66
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: users.py    MIT License 5 votes vote down vote up
def post(self):
        """Enroll a new user"""
        req_data = request.get_json()
        try:
            validated = validate_user(req_data)
            user_data = validated['data']
            user = cognito_signup(user_data)
            app.logger.debug('success: enroll user into Cognito user pool:{}'.format(user))
            return make_response({'ok': True, 'users': user}, 201)

        except ValidationError as e:
            app.logger.error('ERROR:invalid signup data format:{0}'.format(req_data))
            app.logger.error(e)
            raise BadRequest(e.message) 
Example 67
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: users.py    MIT License 5 votes vote down vote up
def post(self):
        """user signin"""
        req_data = request.get_json()
        client = boto3.client('cognito-idp')
        try:
            signin_data = validate_user(req_data)['data']
            access_token, refresh_token = cognito_signin(client, signin_data)
            res = jsonify({'accessToken': access_token, 'refreshToken': refresh_token})
            app.logger.debug('success:user signin:access_token:{}, refresh_token:{}'.format(access_token, refresh_token))
            return make_response(res, 200)

        except client.exceptions.UserNotFoundException as e:
            app.logger.error('User does not exist: {0}'.format(signin_data))
            app.logger.error(e)
            raise BadRequest('User does not exist')
        except client.exceptions.NotAuthorizedException as e:
            app.logger.error('Password is mismatched or invalid user: {0}'.format(signin_data))
            app.logger.error(e)
            raise BadRequest('Password is mismatched or invalid user')
        except ValidationError as e:
            app.logger.error('Invalid data format: {0}'.format(req_data))
            app.logger.error(e)
            raise BadRequest(e.message)
        except Exception as e:
            app.logger.error('Unexpected error: {0}'.format(req_data))
            app.logger.error(e)
            raise InternalServerError('Unexpected error: {0}'.format(req_data)) 
Example 68
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: users.py    MIT License 5 votes vote down vote up
def post(self):
        """Enroll a new user"""
        req_data = request.get_json()
        try:
            validated = validate_user(req_data)
            user_data = validated['data']
            exist_user = [item for item in User.email_index.query(user_data['email'])]
            if not exist_user:
                new_user_id = uuid.uuid4().hex

                # TODO 1 : Implement following solution code to save user information into DynamoDB
                solution_put_new_user(new_user_id, user_data)

                user = {
                    'id': new_user_id,
                    'username': user_data['username'],
                    'email': user_data['email']
                }

                app.logger.debug('success:user_signup: {0}'.format(user))
                return make_response({'ok': True, 'users': user}, 201)
            else:
                app.logger.error('ERROR: Existed user: {0}'.format(user_data))
                raise Conflict('ERROR: Existed user!')
        except ValidationError as e:
            app.logger.error('ERROR: {0}\n{1}'.format(e.message, req_data))
            raise BadRequest(e.message)
        except PynamoDBException as e:
            app.logger.error('ERROR: {0}\n{1}'.format(e.msg, req_data))
            raise InternalServerError(e.msg) 
Example 69
Project: moving-to-serverless-renew   Author: aws-kr-tnc   File: users.py    MIT License 5 votes vote down vote up
def post(self):
        """user signin"""
        req_data = request.get_json()
        try:
            signin_data = validate_user(req_data)['data']

            # TODO 2: Implement following solution code to get user profile with GSI
            db_user = solution_get_user_data_with_idx(signin_data)
            if db_user is None:
                raise BadRequest('Not existed user!')
            else:
                if check_password_hash(db_user.password, signin_data['password']):
                    token_data = {'user_id': db_user.id, 'username': db_user.username, 'email': db_user.email}
                    access_token = create_access_token(identity=token_data)
                    refresh_token = create_refresh_token(identity=token_data)
                    res = jsonify({'accessToken': access_token, 'refreshToken': refresh_token})
                    app.logger.debug('success:user signin:{0}'.format(token_data))
                    return make_response(res, 200)
                else:
                    app.logger.error('Password is mismatched or invalid user: {0}'.format(signin_data))
                    raise BadRequest('Password is mismatched or invalid user')

        except ValidationError as e:
            app.logger.error('ERROR: {0}\n{1}'.format(e.message, req_data))
            raise BadRequest(e.message)
        except PynamoDBException as e:
            app.logger.error('ERROR: {0}\n{1}'.format(e.msg, req_data))
            raise InternalServerError(e.msg) 
Example 70
Project: babbage   Author: openspending   File: test_validation.py    MIT License 5 votes vote down vote up
def test_invalid_fact_table(self, simple_model_data):
        with pytest.raises(ValidationError):
            model = simple_model_data
            model['fact_table'] = 'b....'
            validate_model(model) 
Example 71
Project: babbage   Author: openspending   File: test_validation.py    MIT License 5 votes vote down vote up
def test_no_fact_table(self, simple_model_data):
        with pytest.raises(ValidationError):
            model = simple_model_data
            del model['fact_table']
            validate_model(model) 
Example 72
Project: swaggerit   Author: dutradda   File: method.py    MIT License 4 votes vote down vote up
def __init__(self, operation, schema, definitions, schema_dir, *, authorizer=None):
        set_logger(self)
        self._operation = operation
        self._body_validator = None
        self._path_validator = None
        self._query_validator = None
        self._headers_validator = None
        self._schema_dir = schema_dir
        self._body_required = False
        self._has_body_parameter = False
        self.auth_required = False
        self.authorizer = authorizer

        query_schema = self._build_default_schema()
        path_schema = self._build_default_schema()
        headers_schema = self._build_default_schema()

        for parameter in schema.get('parameters', []):
            if parameter['in'] == 'body':
                if definitions:
                    body_schema = deepcopy(parameter['schema'])
                    body_schema.update({'definitions': definitions})
                else:
                    body_schema = parameter['schema']

                self._body_validator = build_validator(body_schema, self._schema_dir)
                self._body_required = parameter.get('required', False)
                self._has_body_parameter = True

            elif parameter['in'] == 'path':
                self._set_parameter_on_schema(parameter, path_schema)

            elif parameter['in'] == 'query':
                self._set_parameter_on_schema(parameter, query_schema)

            elif parameter['in'] == 'header':
                if parameter['name'].lower() != 'authorization':
                    self._set_parameter_on_schema(parameter, headers_schema)

                elif self.authorizer == None:
                        raise ValidationError("'authorizer' is a required attribute when "
                                              "'Authorization' header is setted.")

                else:
                    self.auth_required = self._requires_auth(parameter)

        if path_schema['properties']:
            self._path_validator = build_validator(path_schema, self._schema_dir)

        if query_schema['properties']:
            self._query_validator = build_validator(query_schema, self._schema_dir)

        if headers_schema['properties']:
            self._headers_validator = build_validator(headers_schema, self._schema_dir) 
Example 73
Project: swaggerit   Author: dutradda   File: method.py    MIT License 4 votes vote down vote up
def __call__(self, req, session):
        denied = await self._authorize(req, session)
        if denied is not None:
            return denied

        response_headers = {'content-type': 'application/json'}

        try:
            body_params = await self._build_body_params(req)
            query_params = self._build_non_body_params(self._query_validator, req.query)
            path_params = self._build_non_body_params(self._path_validator,
                                                               req.path_params)
            headers_params = self._build_non_body_params(self._headers_validator, dict(req.headers))

        except (ValidationError, SchemaError) as error:
            return self._valdation_error_to_response(error, response_headers)

        req = SwaggerRequest(
            req.path,
            req.method,
            scheme=req.scheme,
            host=req.host,
            path_params=path_params,
            query=query_params,
            headers=headers_params,
            body=body_params,
            body_schema=self._body_validator.schema if self._body_validator else None,
            context=req.context
        )

        try:
            if session is None:
                resp = await self._operation(req)
            else:
                resp = await self._operation(req, session)

        except (ValidationError, SchemaError) as error:
            return self._valdation_error_to_response(error, response_headers)

        except Exception as error:
            body = ujson.dumps({'message': 'Something unexpected happened'})
            self._logger.exception('Unexpected')
            return SwaggerResponse(500, body=body, headers=response_headers)

        else:
            if resp.headers.get('content-type') is None:
                resp.headers.update(response_headers)
            return resp 
Example 74
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: validators.py    Apache License 2.0 4 votes vote down vote up
def _soft_validate_additional_properties(validator,
                                         additional_properties_value,
                                         instance,
                                         schema):
    """This validator function is used for legacy v2 compatible mode in v2.1.
    This will skip all the additional properties checking but keep check the
    'patternProperties'. 'patternProperties' is used for metadata API.

    If there are not any properties on the instance that are not specified in
    the schema, this will return without any effect. If there are any such
    extra properties, they will be handled as follows:

    - if the validator passed to the method is not of type "object", this
      method will return without any effect.
    - if the 'additional_properties_value' parameter is True, this method will
      return without any effect.
    - if the schema has an additionalProperties value of True, the extra
      properties on the instance will not be touched.
    - if the schema has an additionalProperties value of False and there
      aren't patternProperties specified, the extra properties will be stripped
      from the instance.
    - if the schema has an additionalProperties value of False and there
      are patternProperties specified, the extra properties will not be
      touched and raise validation error if pattern doesn't match.
    """
    if (not validator.is_type(instance, "object") or
            additional_properties_value):
        return

    properties = schema.get("properties", {})
    patterns = "|".join(schema.get("patternProperties", {}))
    extra_properties = set()
    for prop in instance:
        if prop not in properties:
            if patterns:
                if not re.search(patterns, prop):
                    extra_properties.add(prop)
            else:
                extra_properties.add(prop)

    if not extra_properties:
        return

    if patterns:
        error = "Additional properties are not allowed (%s %s unexpected)"
        if len(extra_properties) == 1:
            verb = "was"
        else:
            verb = "were"
        yield jsonschema_exc.ValidationError(
            error % (", ".join(repr(extra) for extra in extra_properties),
                     verb))
    else:
        for prop in extra_properties:
            del instance[prop] 
Example 75
Project: controller   Author: deis   File: serializers.py    MIT License 4 votes vote down vote up
def validate_values(self, data):
        for key, value in data.items():
            if value is None:  # use NoneType to unset an item
                continue

            if not re.match(CONFIGKEY_MATCH, key):
                raise serializers.ValidationError(
                    "Config keys must start with a letter or underscore and "
                    "only contain [A-z0-9_]")

            # Validate PORT
            if key == 'PORT':
                if not str(value).isnumeric():
                    raise serializers.ValidationError('PORT can only be a numeric value')
                elif int(value) not in range(1, 65536):
                    # check if hte port is between 1 and 65535. One extra added for range()
                    # http://kubernetes.io/docs/api-reference/v1/definitions/#_v1_serviceport
                    raise serializers.ValidationError('PORT needs to be between 1 and 65535')

            # Validate HEALTHCHECK_*
            if key == 'HEALTHCHECK_URL':
                # Only Path information is supported, not query / anchor or anything else
                # Path is the only thing Kubernetes supports right now
                # See https://github.com/deis/controller/issues/774
                uri = urlparse(value)

                if not uri.path:
                    raise serializers.ValidationError(
                        '{} is missing a URI path (such as /healthz). '
                        'Without it no health check can be done'.format(key)
                    )

                # Disallow everything but path
                # https://docs.python.org/3/library/urllib.parse.html
                if uri.query or uri.fragment or uri.netloc:
                    raise serializers.ValidationError(
                        '{} can only be a URI path (such as /healthz) that does not contain '
                        'other things such as query params'.format(key)
                    )
            elif key.startswith('HEALTHCHECK_') and not str(value).isnumeric():
                # all other healthchecks are integers
                raise serializers.ValidationError('{} can only be a numeric value'.format(key))

        return data 
Example 76
Project: controller   Author: deis   File: serializers.py    MIT License 4 votes vote down vote up
def validate_domain(self, value):
        """
        Check that the hostname is valid
        """
        if value[-1:] == ".":
            value = value[:-1]  # strip exactly one dot from the right, if present

        if value == "*":
            raise serializers.ValidationError("Hostname can't only be a wildcard")

        labels = value.split('.')

        # Let wildcards through by not trying to validate it
        wildcard = True if labels[0] == '*' else False
        if wildcard:
            labels.pop(0)

        try:
            # IDN domain labels to ACE (IDNA2008)
            def ToACE(x): return idna.alabel(x).decode("utf-8", "strict")
            labels = list(map(ToACE, labels))
        except idna.IDNAError as e:
            raise serializers.ValidationError(
               "Hostname does not look valid, could not convert to ACE {}: {}"
               .format(value, e))

        # TLD must not only contain digits according to RFC 3696
        if labels[-1].isdigit():
            raise serializers.ValidationError('Hostname does not look valid.')

        # prepend wildcard 'label' again if removed before
        if wildcard:
            labels.insert(0, '*')

        # recreate value using ACE'd labels
        aceValue = '.'.join(labels)

        if len(aceValue) > 253:
            raise serializers.ValidationError('Hostname must be 253 characters or less.')

        if models.Domain.objects.filter(domain=aceValue).exists():
            raise serializers.ValidationError(
               "The domain {} is already in use by another app".format(value))

        return aceValue 
Example 77
Project: YODA-File   Author: ODM2   File: display_file.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def df_validate(args ):

    level = args.level

    validation_log =["validation starting"]
    stream = file(args.source)

    ymal_data = yaml.load (stream)

    validation_log.append("level 0: File valid JSON or YAML")
    if (level > 0 ):
        schema = file(args.schema)
        json_schema = json.load(schema)
        validation_log.append("Starting Level 2")
        try:
            v = Draft4Validator(json_schema)
            errors = v.iter_errors(ymal_data)
            # building an ErrorTree fails.
            # Means we can't use ErrorTree to get a count or provide more details.
            #   possibly to do  Space in KeyName?.
            #tree = ErrorTree(errors)
            #tree = ErrorTree(v.iter_errors(ymal_data))
            errorList =  list(sorted(errors, key=lambda e: e.path))

           #if (tree.total_errors==0 ) :
            if (len(errorList) == 0):
                logger.info('Level 2 validation; File valid to Display File Structural Specification')
                return

            #for error in tree.errors:
            for error in errorList:
                logger.info ('in iterated errors')
                logger.warn (  error.message + ' in ' +
                "/".join( [ str(element) for element in error.path ] )
                )
        except ValidationError as e:
            logger.info ('in validation error')
            logger.warn(e.message)
        except SchemaError as e:
            logger.error(e)


    pass 
Example 78
Project: openeo-openshift-driver   Author: Open-EO   File: service.py    Apache License 2.0 4 votes vote down vote up
def validate(self, user_id: str, process_graph: dict):
        """The request will ask the back-end to create a new process using the description send in the request body.

        Keyword Arguments:
            user_id {str} -- The identifier of the user (default: {None})
        """
        # TODO: RESPONSE HEADERS -> OpenEO-Costs

        try:
            # Get all processes
            process_response = self.process_service.get_all()
            if process_response["status"] == "error":
                return process_response
            processes = process_response["data"]["processes"]

            # # Get all products
            # product_response = self.data_service.get_all_products()
            # if product_response["status"] == "error":
            #     return product_response
            # products = product_response["data"]
            
            valid = validate_graph(process_graph, processes_list=processes)
            if valid:
                output_errors = []
            else:
                # TODO clarify errors -> use again json schemas for validation
                output_errors = [
                    {
                        "message": "error."
                    }
                ]

            # self.validator.update_datasets(processes, products)
            # self.validator.validate_node(process_graph)

            return {
                "status": "success",
                "code": 200,
                "data": output_errors
            }
        except ValidationError as exp:
            return ServiceException(ProcessesService.name, 400, user_id, exp.message, internal=False,
                                    links=["#tag/EO-Data-Discovery/paths/~1process_graph/post"]).to_dict()
        except Exception as exp:
            return ServiceException(ProcessesService.name, 500, user_id, str(exp)).to_dict() 
Example 79
Project: nova   Author: ZhanHan   File: validators.py    Apache License 2.0 4 votes vote down vote up
def _soft_validate_additional_properties(validator,
                                         additional_properties_value,
                                         instance,
                                         schema):
    """This validator function is used for legacy v2 compatible mode in v2.1.
    This will skip all the additional properties checking but keep check the
    'patternProperties'. 'patternProperties' is used for metadata API.

    If there are not any properties on the instance that are not specified in
    the schema, this will return without any effect. If there are any such
    extra properties, they will be handled as follows:

    - if the validator passed to the method is not of type "object", this
      method will return without any effect.
    - if the 'additional_properties_value' parameter is True, this method will
      return without any effect.
    - if the schema has an additionalProperties value of True, the extra
      properties on the instance will not be touched.
    - if the schema has an additionalProperties value of False and there
      aren't patternProperties specified, the extra properties will be stripped
      from the instance.
    - if the schema has an additionalProperties value of False and there
      are patternProperties specified, the extra properties will not be
      touched and raise validation error if pattern doesn't match.
    """
    if (not validator.is_type(instance, "object") or
            additional_properties_value):
        return

    properties = schema.get("properties", {})
    patterns = "|".join(schema.get("patternProperties", {}))
    extra_properties = set()
    for prop in instance:
        if prop not in properties:
            if patterns:
                if not re.search(patterns, prop):
                    extra_properties.add(prop)
            else:
                extra_properties.add(prop)

    if not extra_properties:
        return

    if patterns:
        error = "Additional properties are not allowed (%s %s unexpected)"
        if len(extra_properties) == 1:
            verb = "was"
        else:
            verb = "were"
        yield jsonschema_exc.ValidationError(
            error % (", ".join(repr(extra) for extra in extra_properties),
                     verb))
    else:
        for prop in extra_properties:
            del instance[prop] 
Example 80
Project: scrapy-cluster   Author: istresearch   File: kafka_monitor.py    MIT License 4 votes vote down vote up
def _process_messages(self):
        try:
            for message in self.consumer:
                if message is None:
                    self.logger.debug("no message")
                    break
                try:
                    self._increment_total_stat(message.value)
                    loaded_dict = json.loads(message.value)
                    found_plugin = False
                    for key in self.plugins_dict:
                        # to prevent reference modification
                        the_dict = copy.deepcopy(loaded_dict)
                        obj = self.plugins_dict[key]
                        instance = obj['instance']
                        schema = obj['schema']
                        try:
                            self.validator(schema).validate(the_dict)
                            found_plugin = True
                            self._increment_plugin_stat(
                                    instance.__class__.__name__,
                                    the_dict)
                            ret = instance.handle(the_dict)
                            # break if nothing is returned
                            if ret is None:
                                break
                        except ValidationError:
                            pass
                    if not found_plugin:
                        extras = {}
                        extras['parsed'] = True
                        extras['valid'] = False
                        extras['data'] = the_dict
                        self.logger.warn("Did not find schema to validate "
                                         "request", extra=extras)
                        self._increment_fail_stat(the_dict)

                except ValueError:
                    extras = {}
                    extras['parsed'] = False
                    extras['valid'] = False
                    extras['data'] = message.value
                    self.logger.warning('Unparseable JSON Received',
                                        extra=extras)
                    self._increment_fail_stat(message.value)
        except OffsetOutOfRangeError:
            # consumer has no idea where they are
            self.consumer.seek_to_end()
            self.logger.error("Kafka offset out of range error")