Python jsonschema.validate() Examples

The following are code examples for showing how to use jsonschema.validate(). 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: signals   Author: Amsterdam   File: mixin.py    Mozilla Public License 2.0 7 votes vote down vote up
def get_interval_type(value):
    """
    Determine intended interval parametrization using JSON Schema.
    """
    # Note: we raise a Django validation error if the data does not match one
    # of interval JSON Schemas.
    for interval_type, schema in SCHEMAS.items():
        try:
            validate(instance=value, schema=schema)
        except JSONSchemaValidationError:
            continue
        else:
            return interval_type
    else:
        msg = 'Export parameters do not match schema.'
        raise DjangoValidationError(msg) 
Example 2
Project: drydock   Author: airshipit   File: test_schema_validation.py    Apache License 2.0 6 votes vote down vote up
def _test_validate(self, schema, expect_failure, input_files, input):
        """validates input yaml against schema.

        :param schema: schema yaml file
        :param expect_failure: should the validation pass or fail.
        :param input_files: pytest fixture used to access the test input files
        :param input: test input yaml doc filename"""
        schema_dir = pkg_resources.resource_filename('drydock_provisioner',
                                                     'schemas')
        schema_filename = os.path.join(schema_dir, schema)
        schema_file = open(schema_filename, 'r')
        schema = yaml.safe_load(schema_file)

        input_file = input_files.join(input)
        instance_file = open(str(input_file), 'r')
        instance = yaml.safe_load(instance_file)

        if expect_failure:
            with pytest.raises(ValidationError):
                jsonschema.validate(instance['spec'], schema['data'])
        else:
            jsonschema.validate(instance['spec'], schema['data']) 
Example 3
Project: AshsSDK   Author: thehappydinoa   File: test_basic.py    MIT License 6 votes vote down vote up
def test_pydist():
    """Make sure pydist.json exists and validates against our schema."""
    # XXX this test may need manual cleanup of older wheels

    import jsonschema

    def open_json(filename):
        return json.loads(open(filename, 'rb').read().decode('utf-8'))

    pymeta_schema = open_json(resource_filename('wheel.test',
                                                'pydist-schema.json'))
    valid = 0
    for dist in ("simple.dist", "complex-dist"):
        basedir = pkg_resources.resource_filename('wheel.test', dist)
        for (dirname, subdirs, filenames) in os.walk(basedir):
            for filename in filenames:
                if filename.endswith('.whl'):
                    whl = ZipFile(os.path.join(dirname, filename))
                    for entry in whl.infolist():
                        if entry.filename.endswith('/metadata.json'):
                            pymeta = json.loads(whl.read(entry).decode('utf-8'))
                            jsonschema.validate(pymeta, pymeta_schema)
                            valid += 1
    assert valid > 0, "No metadata.json found" 
Example 4
Project: signals   Author: Amsterdam   File: mixin.py    Mozilla Public License 2.0 6 votes vote down vote up
def get_parameters(value):
    """
    Derive export parameters. Raises Django ValidationError on invalid inputs.

    Note: is used to validate export parameters, hence the use of Django
    ValidationErrors.
    """
    # Make sure that schemas and derivation functions match:
    assert not set(SCHEMAS.keys()) - set(INTERVAL_DERIVATIONS.keys())

    # Validate JSONSchema and determine which interval type we have.
    # Note get_interval_type may raise a Django ValidationError.
    interval_type = get_interval_type(value)

    # Validate precise inputs per type of interval, derive interval.
    # Note get_interval_type may raise a Django ValidationError.
    t_begin, t_end = INTERVAL_DERIVATIONS[interval_type](value)

    # Note: support for categories and areas is not yet implemented. Hence the
    # empty queryset for Category and empty list for the, as yet un-implemented,
    # areas. Excpectation is that the latter will be a Django model as well.
    categories = get_categories(value)
    areas = []
    return t_begin, t_end, categories, areas 
Example 5
Project: Repobot   Author: Desgard   File: test_basic.py    MIT License 6 votes vote down vote up
def test_pydist():
    """Make sure pydist.json exists and validates against our schema."""
    # XXX this test may need manual cleanup of older wheels

    import jsonschema

    def open_json(filename):
        return json.loads(open(filename, 'rb').read().decode('utf-8'))

    pymeta_schema = open_json(resource_filename('wheel.test',
                                                'pydist-schema.json'))
    valid = 0
    for dist in ("simple.dist", "complex-dist"):
        basedir = pkg_resources.resource_filename('wheel.test', dist)
        for (dirname, subdirs, filenames) in os.walk(basedir):
            for filename in filenames:
                if filename.endswith('.whl'):
                    whl = ZipFile(os.path.join(dirname, filename))
                    for entry in whl.infolist():
                        if entry.filename.endswith('/metadata.json'):
                            pymeta = json.loads(whl.read(entry).decode('utf-8'))
                            jsonschema.validate(pymeta, pymeta_schema)
                            valid += 1
    assert valid > 0, "No metadata.json found" 
Example 6
Project: YODA-File   Author: ODM2   File: display_file.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()

    default_parser = argparse.ArgumentParser(add_help=False)

    subparsers = parser.add_subparsers(help='sub-command help')

    parser_json = subparsers.add_parser('format', parents=[default_parser])
    parser_json.add_argument('format',choices=['json', 'yaml', 'hydroprofile'], help='json  file output path')
    parser_json.add_argument('source', type=str, help='json or yaml file')
    parser_json.add_argument('output', type=str, help='json or yaml file')
    parser_json.set_defaults(func=convert)

    parser_validate = subparsers.add_parser('validate', parents=[default_parser])
    parser_validate.add_argument('source', type=str, help='json or yaml file')
    parser_validate.add_argument('schema',default='schema/df2_schema.json',nargs='?', help='schema to use to validate. default=')
    parser_validate.add_argument('level',choices=[0, 1],default=1,nargs='?', help='level of validation')
    parser_validate.set_defaults(func=df_validate)

    # parser_validate = subparsers.add_parser('validate', parents=[default_parser])
    # parser_validate.add_argument('json_schema_file_path', type=str, help='json schema file path')
    # parser_validate.set_defaults(func=validate)

    args = parser.parse_args()
    args.func(args) 
Example 7
Project: ramlwrap   Author: jmons   File: validation.py    MIT License 6 votes vote down vote up
def _validate_post_api(request, action, dynamic_values=None):
    """
    Validate POST APIs.
    :param request: incoming http request.
    :param action: action object containing data used to validate
        and serve the post request.
    :param dynamic_values: dict of dynamic id names against actual values to substitute into url
     e.g. {'dynamic_id': 'aBc'}
    :returns: returns the HttpResponse generated by the action target.
    """

    if action.query_parameter_checks:
        # Following raises exception on fail or passes through.
        _validate_query_params(request.GET, action.query_parameter_checks)

    return _common_validation(request, action, dynamic_values) 
Example 8
Project: ramlwrap   Author: jmons   File: validation.py    MIT License 6 votes vote down vote up
def _validate_put_api(request, action, dynamic_values=None):
    """
    Validate PUT APIs.
    :param request: incoming http request.
    :param action: action object containing data used to validate
        and serve the put request.
    :param dynamic_values: dict of dynamic id names against actual values to substitute into url
     e.g. {'dynamic_id': 'aBc'}
    :returns: returns the HttpResponse generated by the action target.
    """

    if action.query_parameter_checks:
        # Following raises exception on fail or passes through.
        _validate_query_params(request.PUT, action.query_parameter_checks)

    return _common_validation(request, action, dynamic_values) 
Example 9
Project: threatdetectionservice   Author: flyballlabs   File: test_basic.py    Apache License 2.0 6 votes vote down vote up
def test_pydist():
    """Make sure pydist.json exists and validates against our schema."""
    # XXX this test may need manual cleanup of older wheels

    import jsonschema

    def open_json(filename):
        with open(filename, 'rb') as json_file:
            return json.loads(json_file.read().decode('utf-8'))

    pymeta_schema = open_json(resource_filename('wheel.test',
                                                'pydist-schema.json'))
    valid = 0
    for dist in ("simple.dist", "complex-dist"):
        basedir = pkg_resources.resource_filename('wheel.test', dist)
        for (dirname, subdirs, filenames) in os.walk(basedir):
            for filename in filenames:
                if filename.endswith('.whl'):
                    whl = ZipFile(os.path.join(dirname, filename))
                    for entry in whl.infolist():
                        if entry.filename.endswith('/metadata.json'):
                            pymeta = json.loads(whl.read(entry).decode('utf-8'))
                            jsonschema.validate(pymeta, pymeta_schema)
                            valid += 1
    assert valid > 0, "No metadata.json found" 
Example 10
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 11
Project: telemetry   Author: jupyter   File: eventlog.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def record_event(self, schema_name, version, event):
        """
        Record given event with schema has occurred.
        """
        if not (self.handlers and schema_name in self.allowed_schemas):
            # if handler isn't set up or schema is not explicitly whitelisted,
            # don't do anything
            return

        if (schema_name, version) not in self.schemas:
            raise ValueError('Schema {schema_name} version {version} not registered'.format(
                schema_name=schema_name, version=version
            ))
        schema = self.schemas[(schema_name, version)]
        jsonschema.validate(event, schema)

        capsule = {
            '__timestamp__': datetime.utcnow().isoformat() + 'Z',
            '__schema__': schema_name,
            '__schema_version__': version,
            '__metadata_version__': TELEMETRY_METADATA_VERSION
        }
        capsule.update(event)
        self.log.info(capsule) 
Example 12
Project: aws-cfn-plex   Author: lordmuffin   File: test_basic.py    MIT License 6 votes vote down vote up
def test_pydist():
    """Make sure pydist.json exists and validates against our schema."""
    # XXX this test may need manual cleanup of older wheels

    import jsonschema

    def open_json(filename):
        return json.loads(open(filename, 'rb').read().decode('utf-8'))

    pymeta_schema = open_json(resource_filename('wheel.test',
                                                'pydist-schema.json'))
    valid = 0
    for dist in ("simple.dist", "complex-dist"):
        basedir = pkg_resources.resource_filename('wheel.test', dist)
        for (dirname, subdirs, filenames) in os.walk(basedir):
            for filename in filenames:
                if filename.endswith('.whl'):
                    whl = ZipFile(os.path.join(dirname, filename))
                    for entry in whl.infolist():
                        if entry.filename.endswith('/metadata.json'):
                            pymeta = json.loads(whl.read(entry).decode('utf-8'))
                            jsonschema.validate(pymeta, pymeta_schema)
                            valid += 1
    assert valid > 0, "No metadata.json found" 
Example 13
Project: jbox   Author: jpush   File: test_basic.py    MIT License 6 votes vote down vote up
def test_pydist():
    """Make sure pydist.json exists and validates against our schema."""
    # XXX this test may need manual cleanup of older wheels

    import jsonschema

    def open_json(filename):
        return json.loads(open(filename, 'rb').read().decode('utf-8'))

    pymeta_schema = open_json(resource_filename('wheel.test',
                                                'pydist-schema.json'))
    valid = 0
    for dist in ("simple.dist", "complex-dist"):
        basedir = pkg_resources.resource_filename('wheel.test', dist)
        for (dirname, subdirs, filenames) in os.walk(basedir):
            for filename in filenames:
                if filename.endswith('.whl'):
                    whl = ZipFile(os.path.join(dirname, filename))
                    for entry in whl.infolist():
                        if entry.filename.endswith('/metadata.json'):
                            pymeta = json.loads(whl.read(entry).decode('utf-8'))
                            jsonschema.validate(pymeta, pymeta_schema)
                            valid += 1
    assert valid > 0, "No metadata.json found" 
Example 14
Project: resolwe   Author: genialis   File: register.py    Apache License 2.0 6 votes vote down vote up
def valid(self, instance, schema):
        """Validate schema."""
        try:
            jsonschema.validate(instance, schema)
        except jsonschema.exceptions.ValidationError as ex:
            self.stderr.write("    VALIDATION ERROR: {}".format(instance['name'] if 'name' in instance else ''))
            self.stderr.write("        path:       {}".format(ex.path))
            self.stderr.write("        message:    {}".format(ex.message))
            self.stderr.write("        validator:  {}".format(ex.validator))
            self.stderr.write("        val. value: {}".format(ex.validator_value))
            return False

        try:
            # Check that default values fit field schema.
            for field in ['input', 'output', 'schema']:
                for schema, _, path in iterate_schema({}, instance.get(field, {})):
                    if 'default' in schema:
                        validate_schema({schema['name']: schema['default']}, [schema])
        except ValidationError:
            self.stderr.write("    VALIDATION ERROR: {}".format(instance['name']))
            self.stderr.write("        Default value of field '{}' is not valid.". format(path))
            return False

        return True 
Example 15
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 16
Project: beam   Author: axbaretto   File: test_basic.py    Apache License 2.0 6 votes vote down vote up
def test_pydist():
    """Make sure pydist.json exists and validates against our schema."""
    # XXX this test may need manual cleanup of older wheels

    import jsonschema

    def open_json(filename):
        return json.loads(open(filename, 'rb').read().decode('utf-8'))

    pymeta_schema = open_json(resource_filename('wheel.test',
                                                'pydist-schema.json'))
    valid = 0
    for dist in ("simple.dist", "complex-dist"):
        basedir = pkg_resources.resource_filename('wheel.test', dist)
        for (dirname, subdirs, filenames) in os.walk(basedir):
            for filename in filenames:
                if filename.endswith('.whl'):
                    whl = ZipFile(os.path.join(dirname, filename))
                    for entry in whl.infolist():
                        if entry.filename.endswith('/metadata.json'):
                            pymeta = json.loads(whl.read(entry).decode('utf-8'))
                            jsonschema.validate(pymeta, pymeta_schema)
                            valid += 1
    assert valid > 0, "No metadata.json found" 
Example 17
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 18
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 19
Project: nova   Author: ZhanHan   File: allocation.py    Apache License 2.0 6 votes vote down vote up
def list_for_consumer(req):
    """List allocations associated with a consumer."""
    context = req.environ['placement.context']
    consumer_id = util.wsgi_path_item(req.environ, 'consumer_uuid')

    # NOTE(cdent): There is no way for a 404 to be returned here,
    # only an empty result. We do not have a way to validate a
    # consumer id.
    allocations = objects.AllocationList.get_all_by_consumer_id(
        context, consumer_id)

    allocations_json = jsonutils.dumps(
        _serialize_allocations_for_consumer(allocations))

    req.response.status = 200
    req.response.body = allocations_json
    req.response.content_type = 'application/json'
    return req.response 
Example 20
Project: pcocc   Author: cea-hpc   File: ObjectStore.py    GNU General Public License v3.0 6 votes vote down vote up
def load_repos(self, repo_config_file, tag):
        try:
            stream = file(repo_config_file, 'r')
            repo_config = yaml.safe_load(stream)
        except (yaml.YAMLError, IOError) as err:
            raise InvalidConfigurationError(str(err))

        try:
            jsonschema.validate(repo_config,
                                yaml.safe_load(repo_definition_schema))
        except jsonschema.exceptions.ValidationError as err:
            raise InvalidConfigurationError(err.message)

        for repo in repo_config['repos']:
            if repo['name'] in self._repos.keys():
                raise InvalidConfigurationError('Duplicate repository: {0}'.format(
                    repo['name']))

            repo['tag'] = tag
            repo['store'] = ObjectStore(Config().resolve_path(repo['path']),
                                        repo['name'])
            self._repos[repo['name']] = repo 
Example 21
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: request.py    Apache License 2.0 5 votes vote down vote up
def _get_alias_from_config():
    """Parse and validate PCI aliases from the nova config."""
    jaliases = CONF.pci_alias
    aliases = {}  # map alias name to alias spec list
    try:
        for jsonspecs in jaliases:
            spec = jsonutils.loads(jsonspecs)
            jsonschema.validate(spec, _ALIAS_SCHEMA)
            name = spec.pop("name")
            dev_type = spec.pop('device_type', None)
            if dev_type:
                spec['dev_type'] = dev_type
            if name not in aliases:
                aliases[name] = [spec]
            else:
                if aliases[name][0]["dev_type"] == spec["dev_type"]:
                    aliases[name].append(spec)
                else:
                    reason = _("Device type mismatch for alias '%s'") % name
                    raise exception.PciInvalidAlias(reason=reason)

    except exception.PciInvalidAlias:
        raise
    except Exception as e:
        raise exception.PciInvalidAlias(reason=six.text_type(e))

    return aliases 
Example 22
Project: sic   Author: Yanixos   File: test_basic.py    GNU General Public License v3.0 5 votes vote down vote up
def test_pydist():
    """Make sure pydist.json exists and validates against our schema."""
    # XXX this test may need manual cleanup of older wheels

    import jsonschema

    def open_json(filename):
        return json.loads(open(filename, 'rb').read().decode('utf-8'))

    pymeta_schema = open_json(resource_filename('wheel.test',
                                                'pydist-schema.json'))
    valid = 0
    for dist in ("simple.dist", "complex-dist"):
        basedir = pkg_resources.resource_filename('wheel.test', dist)
        for (dirname, subdirs, filenames) in os.walk(basedir):
            for filename in filenames:
                if filename.endswith('.whl'):
                    whl = ZipFile(os.path.join(dirname, filename))
                    for entry in whl.infolist():
                        if entry.filename.endswith('/metadata.json'):
                            pymeta = json.loads(whl.read(entry).decode('utf-8'))
                            jsonschema.validate(pymeta, pymeta_schema)
                            valid += 1
    assert valid > 0, "No metadata.json found" 
Example 23
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 24
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 25
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 26
Project: vod-converter   Author: umautobots   File: converter.py    MIT License 5 votes vote down vote up
def validate_schema(data, schema):
    """Wraps default implementation but accepting tuples as arrays too.

    https://github.com/Julian/jsonschema/issues/148
    """
    return raw_validate(data, schema, types={"array": (list, tuple)}) 
Example 27
Project: cis   Author: mozilla-iam   File: test_profile.py    Mozilla Public License 2.0 5 votes vote down vote up
def test_profile_validation(self):
        from cis_profile import profile
        import jsonschema.exceptions

        u = profile.User()
        u.validate()

        u.user_id.value = {"invalid": "test"}
        try:
            u.validate()
            raise Exception("ValidationFailure", "Should have failed validation, did not")
        except jsonschema.exceptions.ValidationError:
            pass
        else:
            raise Exception("ValidationFailure", "Should have failed validation, did not") 
Example 28
Project: cis   Author: mozilla-iam   File: test_profile.py    Mozilla Public License 2.0 5 votes vote down vote up
def test_json_load_and_self_validate_profile(self):
        profile = json.load(open("cis_profile/data/user_profile_null.json"))
        schema = json.load(open("cis_profile/data/profile.schema"))
        jsonschema.validate(profile, schema) 
Example 29
Project: cis   Author: mozilla-iam   File: profile.py    Mozilla Public License 2.0 5 votes vote down vote up
def validate(self):
        """
        Validates against a JSON schema
        """

        return jsonschema.validate(self.as_dict(), self.__well_known.get_schema()) 
Example 30
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 31
Project: maintain   Author: kylef   File: config.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def fromfile(cls, path):
        with open(path) as fp:
            content = fp.read()
            content = yaml.safe_load(content)
            cls.validate(content)

        return cls(**content) 
Example 32
Project: maintain   Author: kylef   File: config.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def validate(cls, config):
        jsonschema.validate(config, SCHEMA)
        return True 
Example 33
Project: python-mwxml   Author: mediawiki-utilities   File: validate.py    MIT License 5 votes vote down vote up
def validate(docs, schema, verbose=False):
    for doc in docs:
        jsonschema.validate(doc, schema)
        yield doc 
Example 34
Project: eqllib   Author: endgameinc   File: schemas.py    MIT License 5 votes vote down vote up
def validate(cls, payload):
        if cls not in Document.__schemas:
            Document.__schemas[cls] = cls.get_schema(ordered=True)
        schema = cls.get_schema(ordered=True)
        jsonschema.validate(payload, schema)
        return payload 
Example 35
Project: tornado-restful-template   Author: smileboywtu   File: handler.py    Apache License 2.0 5 votes vote down vote up
def validate_arguments(self, method, data):
        return validate(data, itemgetter(method.lower())(schemas)) 
Example 36
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 37
Project: normandy   Author: mozilla   File: test_api.py    Mozilla Public License 2.0 5 votes vote down vote up
def check_action_schema_format(action):
    """Given an action, make sure its argument schema is a valid json schema"""
    # This avoids downloading the schema by asserting that it is using json schema draft v4, and
    # checking against a local copy of it.
    assert action["arguments_schema"]["$schema"] == "http://json-schema.org/draft-04/schema#"
    with open("contract-tests/data/json-schema-draft-4-schema.json") as f:
        schema = json.load(f)
        assert jsonschema.validate(action["arguments_schema"], schema) is None 
Example 38
Project: normandy   Author: mozilla   File: assertions.py    Mozilla Public License 2.0 5 votes vote down vote up
def assert_valid_schema(data):
    schema = _load_json_schema()
    return validate(data, schema) 
Example 39
Project: ramlwrap   Author: jmons   File: validation.py    MIT License 5 votes vote down vote up
def _validate_query_params(params, checks):
    """
    Function to validate get request params. If there are checks to be
    performed then they will be; these will be items such as length and type
    checks defined in the definition file.
    :param params: incoming request parameters.
    :param checks: dict of param to rule to validate with.
    :raises ValidationError: raised when any query parameter fails any
        of its checks defined in the checks param.
    :returns: true if validated, otherwise raises an exception when fails.
    """

    # If validation checks, check the params. If not, pass.
    if checks:
        for param in checks:
            # If the expected param is in the query.
            if param in params:
                for check, rule in checks[param].items():
                    if rule is not None:
                        error_message = 'QueryParam [%s] failed validation check [%s]:[%s]' % (param, check, rule)
                        if check == 'minLength':
                            if len(params.get(param)) < rule:
                                raise ValidationError(error_message)
                        elif check == 'maxLength':
                            if len(params.get(param)) > rule:
                                raise ValidationError(error_message)
                        elif check == 'type':
                            if rule == 'number':
                                try:
                                    float(params.get(param))
                                except ValueError:
                                    raise ValidationError(error_message)

            # If the require param isn't in the query.
            elif checks[param]['required'] is True:
                raise ValidationError('QueryParam [%s] failed validation check [Required]:[True]' % param)

    return True 
Example 40
Project: ramlwrap   Author: jmons   File: validation.py    MIT License 5 votes vote down vote up
def _validate_get_api(request, action, dynamic_values=None):
    """
    Validate Get APIs.
    :param request: incoming http request.
    :param action: action object containing data used to validate
        and serve the get request.
    :param dynamic_values: dict of dynamic id names against actual values to substitute into url
     e.g. {'dynamic_id': 'aBc'}
    :returns: returns the HttpResponse generated by the action target.
    """

    if action.query_parameter_checks:
        # Following raises exception on fail or passes through.
        _validate_query_params(request.GET, action.query_parameter_checks)

    if action.target:
        # If there was a dynamic value, pass it through
        if dynamic_values:
            response = action.target(request, **dynamic_values)
        else:
            response = action.target(request)
    else:
        response = _generate_example(request, action)

    if not isinstance(response, HttpResponse):
        # As we weren't given a HttpResponse, we need to create one
        # and handle the data correctly.

        # FIXME: consider changing this element here to be more inspective - if its already a string
        # how do we know its not already json encoded etc?
        if action.resp_content_type == ContentType.JSON:
            response = HttpResponse(json.dumps(response), content_type="application/json")
        else:
            raise Exception("Unsuported response content type - contact @jmons for future feature request")

    return response 
Example 41
Project: cert-schema   Author: IMSGlobal   File: schema_validator.py    MIT License 5 votes vote down vote up
def validate_json(certificate_json, schema_json):
    """
    If no exception is raised, the instance is valid. Raises BlockcertValidationError is validation fails.
    :param certificate_json:
    :param schema_json:
    :return:
    """
    try:
        jsonschema.validate(certificate_json, schema_json)
        return True
    except jsonschema.exceptions.ValidationError as ve:
        logging.error(ve, exc_info=True)
        raise BlockcertValidationError(ve) 
Example 42
Project: cert-schema   Author: IMSGlobal   File: schema_validator.py    MIT License 5 votes vote down vote up
def validate(data_file, schema_file):
    with open(data_file) as data_f, open(schema_file) as schema_f:
        data = json.load(data_f)
        schema = json.load(schema_f)
        return validate_json(data, schema) 
Example 43
Project: dataprep   Author: sfu-db   File: implicit_database.py    MIT License 5 votes vote down vote up
def __init__(self, name: str, config: Dict[str, Any]) -> None:
        jsonschema.validate(
            config, CONFIG_SCHEMA
        )  # This will throw errors if validate failed
        self.name = name
        self.config = config

        request_def = config["request"]

        self.method = request_def["method"]
        self.url = request_def["url"]

        if "authorization" in request_def:
            auth_def = request_def["authorization"]
            if isinstance(auth_def, str):
                auth_type = AuthorizationType[auth_def]
                auth_params: Dict[str, str] = {}
            elif isinstance(auth_def, dict):
                auth_type = AuthorizationType[auth_def.pop("type")]
                auth_params = {**auth_def}
            else:
                raise NotImplementedError
            self.authorization = Authorization(auth_type=auth_type, params=auth_params)

        for key in ["headers", "params", "cookies"]:
            if key in request_def:
                setattr(self, key, Fields(request_def[key]))
        if "body" in request_def:
            body_def = request_def["body"]
            self.body_ctype = body_def["ctype"]
            self.body = Fields(body_def["content"])

        response_def = config["response"]
        self.ctype = response_def["ctype"]
        self.table_path = response_def["tablePath"]
        self.schema = {
            name: SchemaField(def_["target"], def_["type"], def_.get("description"))
            for name, def_ in response_def["schema"].items()
        }
        self.orient = Orient(response_def["orient"]) 
Example 44
Project: adaptivemd   Author: markovmodel   File: resource_manager.py    GNU Lesser General Public License v2.1 5 votes vote down vote up
def _validate_resource_desc(self, resource_desc):

        """
        **Purpose**: Validate the resource description that was provided to ResourceManager

        :arguments: dictionary consisting of details of the resource request
        :return: boolean (valid/invalid)
        """

        try:


            if not isinstance(resource_desc, dict):
                raise TypeError(expected_type=dict, actual_type=type(resource_desc))

            # load the json schema
            with file(os.path.join(self._directory, "schemas/resource_description.schema")) as fp:
                schema = json.load(fp)

            # this will throw a Validation Error...
            jsonschema.validate(resource_desc, schema)

            return True

        except Exception, ex:
            raise Error(msg='Failed to validate resource description, error: %s'%ex) 
Example 45
Project: lightweight-rest-tester   Author: ridi   File: __init__.py    MIT License 5 votes vote down vote up
def _run_tests(self, tests, response):
        if tests.status_code:
            self.assertIn(
                    response.status_code,
                    convert_to_list(tests.status_code),
                    'Unexpected status code.'
            )

        if tests.json_schema:
            jsonschema.validate(
                    response.json(),
                    tests.json_schema
            ) 
Example 46
Project: deb-python-proliantutils   Author: openstack   File: manager.py    Apache License 2.0 5 votes vote down vote up
def validate(raid_config):
    """Validates the RAID configuration provided.

    This method validates the RAID configuration provided against
    a JSON schema.

    :param raid_config: The RAID configuration to be validated.
    :raises: InvalidInputError, if validation of the input fails.
    """
    raid_schema_fobj = open(RAID_CONFIG_SCHEMA, 'r')
    raid_config_schema = json.load(raid_schema_fobj)
    try:
        jsonschema.validate(raid_config, raid_config_schema)
    except json_schema_exc.ValidationError as e:
        raise exception.InvalidInputError(e.message)

    for logical_disk in raid_config['logical_disks']:

        # If user has provided 'number_of_physical_disks' or
        # 'physical_disks', validate that they have mentioned at least
        # minimum number of physical disks required for that RAID level.
        raid_level = logical_disk['raid_level']
        min_disks_reqd = constants.RAID_LEVEL_MIN_DISKS[raid_level]

        no_of_disks_specified = None
        if 'number_of_physical_disks' in logical_disk:
            no_of_disks_specified = logical_disk['number_of_physical_disks']
        elif 'physical_disks' in logical_disk:
            no_of_disks_specified = len(logical_disk['physical_disks'])

        if (no_of_disks_specified and
                no_of_disks_specified < min_disks_reqd):
            msg = ("RAID level %(raid_level)s requires at least %(number)s "
                   "disks." % {'raid_level': raid_level,
                               'number': min_disks_reqd})
            raise exception.InvalidInputError(msg) 
Example 47
Project: salicapi   Author: Lafaiet   File: InputValidation.py    GNU General Public License v3.0 5 votes vote down vote up
def validate(self):
        Draft3Validator(schema).validate(self.fields) 
Example 48
Project: holepunch   Author: CypherpunkArmory   File: json.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def validate(self, data: Union[str, dict], schema_name):
        if not self.schema_loaded(schema_name):
            self.load_json_schema(schema_name)

        json_obj = data

        if not isinstance(data, dict):
            json_obj = json.loads(data)

        return validate(
            json_obj,
            self.schema_data[schema_name],
            resolver=self.relative_resolvers[schema_name],
        ) 
Example 49
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 50
Project: file-config   Author: stephen-bunn   File: _file_config.py    ISC License 5 votes vote down vote up
def _handle_loads(cls, handler, content, validate=False, **kwargs):
    """ Loads caller, used by partial method for dynamic handler assignments.

    :param object handler: The loads handler
    :param str content: The content to load from
    :param bool validate: Performs content validation before loading,
        defaults to False, optional
    :return: The loaded instance
    :rtype: object
    """

    return from_dict(cls, handler.loads(cls, content, **kwargs), validate=validate) 
Example 51
Project: file-config   Author: stephen-bunn   File: _file_config.py    ISC License 5 votes vote down vote up
def _handle_load(cls, handler, file_object, validate=False, **kwargs):
    """ Loads caller, used by partial method for dynamic handler assignments.

    :param object handler: The loads handler
    :param file file_object: The file object to load from
    :param bool validate: Performs content validation before loading,
        defaults to False, optional
    :return: The loaded instance
    :rtype: object
    """

    return from_dict(cls, handler.load(cls, file_object, **kwargs), validate=validate) 
Example 52
Project: file-config   Author: stephen-bunn   File: _file_config.py    ISC License 5 votes vote down vote up
def validate(instance):
    """ Validates a given ``instance``.

    :param object instance: The instance to validate
    :raises jsonschema.exceptions.ValidationError: On failed validation
    """

    jsonschema.validate(
        to_dict(instance, dict_type=dict), build_schema(instance.__class__)
    ) 
Example 53
Project: file-config   Author: stephen-bunn   File: _file_config.py    ISC License 5 votes vote down vote up
def from_dict(config_cls, dictionary, validate=False):
    """ Loads an instance of ``config_cls`` from a dictionary.

    :param type config_cls: The class to build an instance of
    :param dict dictionary: The dictionary to load from
    :param bool validate: Preforms validation before building ``config_cls``,
        defaults to False, optional
    :return: An instance of ``config_cls``
    :rtype: object
    """

    return _build(config_cls, dictionary, validate=validate) 
Example 54
Project: QCSchema   Author: MolSSI   File: validate.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def validate(data, schema_type, version="dev"):
    """
    Validates a given input for a schema input and output type.
    """
    schema = versions.get_schema(schema_type, version)

    jsonschema.validate(data, schema) 
Example 55
Project: netmet   Author: godaddy   File: main.py    Apache License 2.0 5 votes vote down vote up
def metrics_add():
    """Stores metrics to elastic."""

    # Check just basic schema, let elastic check everything else
    schema = {
        "type": "array",
        "items": {"type": "object"}
    }

    try:
        req_data = flask.request.get_json(silent=False, force=True)
        jsonschema.validate(req_data, schema)
    except (ValueError, jsonschema.exceptions.ValidationError) as e:
        return flask.jsonify({"error": "Bad request: %s" % e}), 400
    else:
        data = {"north-south": [], "east-west": []}
        for d in req_data:
            for key in data:
                if key in d:
                    data[key].append(d[key])
                    break
            else:
                LOG.warning("Ignoring wrong object %s" % json.dumps(d))

        # TODO(boris-42): Use pusher here, to reduce amount of quires
        # from netmet server to elastic, join data from different netmet
        # clients requests before pushing them to elastic
        for k, v in data.iteritems():
            if v:
                db.get().metrics_add(k, v)

    return flask.jsonify({"message": "successfully stored metrics"}), 201 
Example 56
Project: netmet   Author: godaddy   File: main.py    Apache License 2.0 5 votes vote down vote up
def event_create(event_id):
    """If event already exists it recreates it."""
    schema = {
        "type": "object",

        "definitions": {
            "traffic": {
                "type": "object",
                "properties": {
                    "type": {"enum": ["host", "az", "dc"]},
                    "value": {"type": "string"}
                },
                "required": ["type", "value"]
            }
        },
        "properties": {
            "name": {"type": "string"},
            "started_at": {"type": "string"},
            "finished_at": {"type": "string"},
            "traffic_from": {"$ref": "#/definitions/traffic"},
            "traffic_to": {"$ref": "#/definitions/traffic"}
        },
        "required": ["started_at", "name"],
        "additionalProperties": False
    }
    try:
        data = flask.request.get_json(silent=False, force=True)
        jsonschema.validate(data, schema)

    except (ValueError, jsonschema.exceptions.ValidationError) as e:
        return flask.jsonify({"error": "Bad request: %s" % e}), 400

    db.get().event_create(event_id, data)
    return flask.jsonify({"message": "Event created %s" % event_id}), 201 
Example 57
Project: dcatd   Author: Amsterdam   File: dcat.py    Mozilla Public License 2.0 5 votes vote down vote up
def validate(self, data: dict, method: str):
        # language=rst
        """Validate the data.

        :returns: the Type for which validation succeeded. See also
            :meth:`OneOf.validate`
        :rtype: Type

        """
        jsonschema.validate(data, self.schema(method))
        return self 
Example 58
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 59
Project: dcatd   Author: Amsterdam   File: config.py    Mozilla Public License 2.0 5 votes vote down vote up
def validate(self, schema: T.Mapping):
        # language=rst
        """
        Validate this config dict using the JSON schema given in ``schema``.

        Raises:
            ConfigError: if schema validation failed

        """
        try:
            jsonschema.validate(self, schema)
        except jsonschema.exceptions.SchemaError as e:
            raise ConfigError("Invalid JSON schema definition.") from e
        except jsonschema.exceptions.ValidationError as e:
            raise ConfigError("Schema validation failed.") from e 
Example 60
Project: dcatd   Author: Amsterdam   File: config.py    Mozilla Public License 2.0 5 votes vote down vote up
def load() -> ConfigDict:
    # language=rst
    """ Load and validate the configuration.
    """
    config_path = _config_path()
    config = ConfigDict(_load_yaml(config_path))
    if 'logging' not in config:
        raise ConfigError(
            "No 'logging' entry in config file {}".format(config_path)
        )
    logging.config.dictConfig(config['logging'])
    logger.info("Loaded configuration from '%s'", os.path.abspath(str(config_path)))
    config.validate(_config_schema())
    return config 
Example 61
Project: stac-validator   Author: sparkgeo   File: stac_validator.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, stac_file, stac_spec_dirs=None, version="master", log_level="CRITICAL", follow=False):
        """
        Validate a STAC file.
        :param stac_file: File to validate
        :param stac_spec_dirs: List of local specification directories to check for JSON schema files.
        :param version: STAC version to validate against. Uses github tags from the stac-spec repo. ex: v0.6.2
        :param log_level: Level of logging to report
        :param follow: Follow links in STAC
        """
        """

        :param stac_file: file to validate
        :param version: github tag - defaults to master
        """

        numeric_log_level = getattr(logging, log_level.upper(), None)
        if not isinstance(numeric_log_level, int):
            raise ValueError("Invalid log level: %s" % log_level)

        logging.basicConfig(
            format="%(asctime)s : %(levelname)s : %(thread)d : %(message)s",
            datefmt="%m/%d/%Y %H:%M:%S",
            level=numeric_log_level,
        )
        logging.info("STAC Validator Started.")
        self.stac_version = version
        self.stac_file = stac_file.strip()
        self.dirpath = tempfile.mkdtemp()
        self.stac_spec_dirs = self.check_none(stac_spec_dirs)

        self.follow = follow

        self.message = []
        self.status = {
            "catalogs": {"valid": 0, "invalid": 0},
            "collections": {"valid": 0, "invalid": 0},
            "items": {"valid": 0, "invalid": 0},
            "unknown": 0,
        } 
Example 62
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 63
Project: resolwe   Author: genialis   File: migration_ops.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, process, field, schema, default=None):
        """Construct a process field add operation.

        :param process: Process slug
        :param field: Dot-separated field path, starting with the top-level
            database field name (must be either ``input`` or ``output``)
        :param schema: Field schema definition
        :param default: An operation that generates default values, which
            must be an instance of :class:`DataDefaultOperation` or its
            subclass
        """
        self._raw_field = field
        self.field = field.split('.')
        if self.field[0] not in ('input', 'output'):
            raise ValueError("Field path must start with either input or output")
        if len(self.field) < 2:
            raise ValueError("Field path must contain at least two levels")
        if len(self.field) != 2:
            # TODO: Support nested fields.
            raise NotImplementedError("Field path must contain exactly two levels")
        schema_type = self.field.pop(0)

        self.schema = schema
        jsonschema.validate([schema], FIELD_SCHEMA)

        if schema['name'] != self.field[-1]:
            raise ValueError("Field name in schema differs from field path")

        if not default and schema.get('required', True):
            raise ValueError("A required field must have a default")
        if default and not isinstance(default, DataDefaultOperation):
            raise TypeError("Default must be an instance of DataDefaultOperation")
        self.default = default

        super().__init__(
            process=process,
            schema_type=schema_type,
            field=field,
            schema=schema,
            default=default
        ) 
Example 64
Project: resolwe   Author: genialis   File: utils.py    Apache License 2.0 5 votes vote down vote up
def validate_process_types(queryset=None):
    """Perform process type validation.

    :param queryset: Optional process queryset to validate
    :return: A list of validation error strings
    """
    if not queryset:
        from .process import Process
        queryset = Process.objects.all()

    processes = {}
    for process in queryset:
        dict_dot(
            processes,
            process.type.replace(':', '.') + '__schema__',
            process.output_schema
        )

    errors = []
    for path, key, value in iterate_dict(processes, exclude=lambda key, value: key == '__schema__'):
        if '__schema__' not in value:
            continue

        # Validate with any parent types.
        for length in range(len(path), 0, -1):
            parent_type = '.'.join(path[:length] + ['__schema__'])
            try:
                parent_schema = dict_dot(processes, parent_type)
            except KeyError:
                continue

            errors += validate_process_subtype(
                supertype_name=':'.join(path[:length]),
                supertype=parent_schema,
                subtype_name=':'.join(path + [key]),
                subtype=value['__schema__']
            )

    return errors 
Example 65
Project: resolwe   Author: genialis   File: register.py    Apache License 2.0 5 votes vote down vote up
def handle(self, *args, **options):
        """Register processes."""
        force = options.get('force')
        retire = options.get('retire')
        verbosity = int(options.get('verbosity'))

        users = get_user_model().objects.filter(is_superuser=True).order_by('date_joined')

        if not users.exists():
            self.stderr.write("Admin does not exist: create a superuser")
            exit(1)

        process_paths, descriptor_paths = [], []
        process_schemas, descriptor_schemas = [], []

        for finder in get_finders():
            process_paths.extend(finder.find_processes())
            descriptor_paths.extend(finder.find_descriptors())

        for proc_path in process_paths:
            process_schemas.extend(
                self.find_schemas(proc_path, schema_type=SCHEMA_TYPE_PROCESS, verbosity=verbosity))

        for desc_path in descriptor_paths:
            descriptor_schemas.extend(
                self.find_schemas(desc_path, schema_type=SCHEMA_TYPE_DESCRIPTOR, verbosity=verbosity))

        user_admin = users.first()
        self.register_descriptors(descriptor_schemas, user_admin, force, verbosity=verbosity)
        # NOTE: Descriptor schemas must be registered first, so
        #       processes can validate 'entity_descriptor_schema' field.
        self.register_processes(process_schemas, user_admin, force, verbosity=verbosity)

        if retire:
            self.retire(process_schemas)

        if verbosity > 0:
            self.stdout.write("Running executor post-registration hook...")
        manager.get_executor().post_register_hook(verbosity=verbosity) 
Example 66
Project: probe-scraper   Author: mozilla   File: repositories.py    Mozilla Public License 2.0 5 votes vote down vote up
def validate(self, filename=None):
        repos = self._get_repos(filename)

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

        jsonschema.validate(repos, schema) 
Example 67
Project: probe-scraper   Author: mozilla   File: repositories.py    Mozilla Public License 2.0 5 votes vote down vote up
def parse(self, filename=None, glean_repo=None):
        self.validate(filename)
        repos = self._get_repos(filename)

        return [
            Repository(name, definition)
            for name, definition
            in list(repos.items())
            if (glean_repo is None
                or glean_repo == name)
        ] 
Example 68
Project: nova   Author: ZhanHan   File: request.py    Apache License 2.0 5 votes vote down vote up
def _get_alias_from_config():
    """Parse and validate PCI aliases from the nova config."""
    jaliases = CONF.pci.alias
    aliases = {}  # map alias name to alias spec list
    try:
        for jsonspecs in jaliases:
            spec = jsonutils.loads(jsonspecs)
            jsonschema.validate(spec, _ALIAS_SCHEMA)
            # It should keep consistent behaviour in configuration
            # and extra specs to call strip() function.
            name = spec.pop("name").strip()
            dev_type = spec.pop('device_type', None)
            if dev_type:
                spec['dev_type'] = dev_type
            if name not in aliases:
                aliases[name] = [spec]
            else:
                if aliases[name][0]["dev_type"] == spec["dev_type"]:
                    aliases[name].append(spec)
                else:
                    reason = _("Device type mismatch for alias '%s'") % name
                    raise exception.PciInvalidAlias(reason=reason)

    except exception.PciInvalidAlias:
        raise
    except Exception as e:
        raise exception.PciInvalidAlias(reason=six.text_type(e))

    return aliases 
Example 69
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 70
Project: pcocc   Author: cea-hpc   File: Batch.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, error):
        super(KeyCredentialError, self).__init__(
            'Keystore authentication error: ' + error)

# Schema to validate batch.yaml config file 
Example 71
Project: pcocc   Author: cea-hpc   File: Batch.py    GNU General Public License v3.0 5 votes vote down vote up
def load(batch_config_file, batchid, batchname, default_batchname,
             proc_type, batchuser):
        """Factory function to initialize a batch manager"""
        try:
            stream = file(batch_config_file, 'r')
            batch_config = yaml.safe_load(stream)
        except yaml.YAMLError as err:
            raise InvalidConfigurationError(str(err))
        except IOError as err:
            raise InvalidConfigurationError(str(err))

        try:
            jsonschema.validate(batch_config,
                                yaml.safe_load(batch_config_schema))
        except jsonschema.exceptions.ValidationError as err:
            raise InvalidConfigurationError(str(err))

        settings = batch_config['settings']

        if batch_config['type'] == 'slurm':
            return SlurmManager(
                batchid, batchname, default_batchname,
                settings, proc_type, batchuser)
        elif batch_config['type'] == 'local':
            return LocalManager(
                batchid, batchname, default_batchname,
                settings, proc_type, batchuser)
        else:
            raise InvalidConfigurationError("Invalid batch manager type") 
Example 72
Project: pcocc   Author: cea-hpc   File: Batch.py    GNU General Public License v3.0 5 votes vote down vote up
def client_cert(self):
        return self.ca_cert

# Schema to validate the global pkey state in the key/value store 
Example 73
Project: pcocc   Author: cea-hpc   File: Batch.py    GNU General Public License v3.0 5 votes vote down vote up
def _validate_job_state(self, state):
        if state is None:
            job_alloc_state = {'jobs': {}, 'next_batchid': 1}
        elif isinstance(state, dict):
            job_alloc_state = state
        else:
            job_alloc_state = yaml.safe_load(state)

        schema = yaml.safe_load(local_job_allocation_schema)
        jsonschema.validate(job_alloc_state, schema)

        return job_alloc_state 
Example 74
Project: pcocc   Author: cea-hpc   File: Misc.py    GNU General Public License v3.0 5 votes vote down vote up
def datetime_to_epoch(dt):
    return int((dt - epoch).total_seconds())

#Schema to validate the global key state in the key/value store 
Example 75
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 76
Project: cis   Author: mozilla-iam   File: test_change_endpoint.py    Mozilla Public License 2.0 4 votes vote down vote up
def test_publishing_a_profile_it_should_be_accepted(self):
        os.environ["CIS_DISCOVERY_URL"] = "https://auth.allizom.org/.well-known/mozilla-iam"
        base_url = self.helper_configuration.get_url_dict().get("change")
        wk = WellKnown(discovery_url="https://auth.allizom.org/.well-known/mozilla-iam")
        jsonschema.validate(json.loads(self.durable_profile), wk.get_schema())
        os.environ["CIS_WELL_KNOWN_MODE"] = "https"
        os.environ["CIS_PUBLIC_KEY_NAME"] = "publisher"
        user = profile.User(
            user_structure_json=json.loads(self.durable_profile),
            discovery_url="https://auth.allizom.org/.well-known/mozilla-iam",
        )
        user.verify_all_signatures()
        access_token = self.exchange_for_access_token()
        conn = http.client.HTTPSConnection(base_url)
        logger.info("Attempting connection for: {}".format(base_url))
        headers = {"authorization": "Bearer {}".format(access_token), "Content-type": "application/json"}
        conn.request("POST", "/v2/user", self.durable_profile, headers=headers)
        res = conn.getresponse()
        data = json.loads(res.read().decode())
        logger.info(data)
        status = data.get("status", data.get("status_code"))
        assert status == 200
        assert data.get("sequence_number") is not None

        #    def test_publishing_a_profile_using_a_partial_update(self):
        os.environ["CIS_DISCOVERY_URL"] = "https://auth.allizom.org/.well-known/mozilla-iam"
        base_url = self.helper_configuration.get_url_dict().get("change")
        wk = WellKnown(discovery_url="https://auth.allizom.org/.well-known/mozilla-iam")
        jsonschema.validate(json.loads(self.durable_profile), wk.get_schema())
        os.environ["CIS_WELL_KNOWN_MODE"] = "https"
        os.environ["CIS_PUBLIC_KEY_NAME"] = "publisher"
        user = profile.User(
            user_structure_json=json.loads(self.durable_profile),
            discovery_url="https://auth.allizom.org/.well-known/mozilla-iam",
        )
        partial_update = profile.User(user_structure_json=None)
        partial_update.user_id = user.user_id
        partial_update.primary_email = user.primary_email
        partial_update.primary_username = user.primary_username
        partial_update.alternative_name.value = "anewfirstname"
        partial_update.sign_attribute("alternative_name", "mozilliansorg")
        access_token = self.exchange_for_access_token()
        conn = http.client.HTTPSConnection(base_url)
        logger.info("Attempting connection for: {}".format(base_url))
        logger.info("Partial update of user: {}".format(partial_update.user_id.value))
        headers = {"authorization": "Bearer {}".format(access_token), "Content-type": "application/json"}
        conn.request("POST", "/v2/user", partial_update.as_json(), headers=headers)
        res = conn.getresponse()
        data = json.loads(res.read().decode())
        status = data.get("status", data.get("status_code"))
        assert status == 200
        assert data.get("sequence_number") is not None 
Example 77
Project: king-phisher-plugins   Author: securestate   File: __init__.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def signal_menu_item_import(self, _):
		dialog = extras.FileChooserDialog('Import Entries', self.window)
		dialog.quick_add_filter('Data Files', '*.json')
		dialog.quick_add_filter('All Files', '*')
		response = dialog.run_quick_open()
		dialog.destroy()
		if not response:
			return
		try:
			with open(response['target_path'], 'r') as file_h:
				data = serializers.JSON.load(file_h)
		except Exception:
			gui_utilities.show_dialog_error(
				'Import Failed',
				self.window,
				'Could not load the specified file.'
			)
			return
		with open(json_schema_file, 'r') as file_h:
			schema = serializers.JSON.load(file_h)
		try:
			jsonschema.validate(data, schema)
		except jsonschema.exceptions.ValidationError:
			gui_utilities.show_dialog_error(
				'Import Failed',
				self.window,
				'Could not load the specified file, the data is malformed.'
			)
			return
		cursor = len(self._tv_model)
		for entry in data['entries']:
			if 'rule' in entry:
				entry_type = 'Rule'
				text = entry['rule']
			elif 'source' in entry:
				entry_type = 'Source'
				text = entry['source']
			new_named_row = _ModelNamedRow(cursor, entry['target'], entry['permanent'], entry_type, text)
			self.application.rpc.async_call(
				'plugins/request_redirect/entries/insert',
				(cursor, named_row_to_entry(new_named_row))
			)
			self._tv_model.append(new_named_row)
			cursor += 1 
Example 78
Project: misp42splunk   Author: remg427   File: loader.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def _load_proxy(self, candidate, variables):
        """
        Render and validate proxy setting with given variables.
        :param candidate: raw proxy setting as `dict`
        :param variables: variables to render template in proxy setting.
        :return: A `dict` contains rendered proxy setting.
        """
        if not candidate:
            return {}

        proxy = self._render_from_dict(candidate, variables)

        enabled = proxy.get('enabled', '0')
        if not is_valid_bool(enabled):
            raise ValueError(
                'Proxy "enabled" expect to be bool type: {}'.format(enabled)
            )

        proxy['enabled'] = is_true(enabled)

        host, port = proxy.get('host'), proxy.get('port')

        if host or port:
            if not host:
                raise ValueError('Proxy "host" must not be empty')

            if not is_valid_port(port):
                raise ValueError(
                    'Proxy "port" expect to be in range [1,65535]: %s' % port
                )

        # proxy type default to 'http'
        proxy_type = proxy.get('type')
        proxy_type = proxy_type.lower() if proxy_type else 'http'
        if proxy_type not in _PROXY_TYPES:
            raise ValueError(
                'Proxy "type" expect to be one of [{}]: {}'.format(
                    ','.join(_PROXY_TYPES), proxy_type)
            )
        else:
            proxy['type'] = proxy_type

        # proxy rdns default to '0'
        proxy_rdns = proxy.get('rdns', '0')
        if not is_valid_bool(proxy_rdns):
            raise ValueError(
                'Proxy "rdns" expect to be bool type: {}'.format(proxy_rdns)
            )
        else:
            proxy['rdns'] = is_true(proxy_rdns)

        return proxy 
Example 79
Project: ramlwrap   Author: jmons   File: validation.py    MIT License 4 votes vote down vote up
def _common_validation(request, action, dynamic_values=None):
    """
    Function for common logic between post and put validation
    :param request: incoming http request.
    :param action: action object containing data used to validate
        and serve the request.
    :param dynamic_values: dict of dynamic id names against actual values to substitute into url
     e.g. {'dynamic_id': 'aBc'}
    """

    error_response = None

    if action.requ_content_type == ContentType.JSON:
        # If the expected request body is JSON, we need to load it.
        if action.schema:
            # If there is any schema, we'll validate it.
            try:
                data = json.loads(request.body.decode('utf-8'))
                validate(data, action.schema)
            except Exception as e:
                # Check the value is in settings, and that it is not None
                if hasattr(settings, 'RAMLWRAP_VALIDATION_ERROR_HANDLER') and settings.RAMLWRAP_VALIDATION_ERROR_HANDLER:
                    error_response = _call_custom_handler(e)
                else:
                    error_response = _validation_error_handler(e)
        else:
            # Otherwise just load it (no validation as no schema).
            data = json.loads(request.body.decode('utf-8'))
    else:
        # The content isn't JSON to just decode it as is.
        data = request.body.decode('utf-8')

    if error_response:
        response = error_response
    else:
        request.validated_data = data

        if action.target:
            if dynamic_values:
                # If there was a dynamic value, pass it through
                response = action.target(request, **dynamic_values)
            else:
                response = action.target(request)
        else:
            response = _generate_example(request, action)

    if not isinstance(response, HttpResponse):
        # As we weren't given a HttpResponse, we need to create one
        # and handle the data correctly.
        if action.resp_content_type == ContentType.JSON:
            if dynamic_values:
                response = HttpResponse(json.dumps(response), **dynamic_values)
            else:
                response = HttpResponse(json.dumps(response))

    return response 
Example 80
Project: pcocc   Author: cea-hpc   File: Misc.py    GNU General Public License v3.0 4 votes vote down vote up
def _do_alloc_ids(self, count, id_alloc_state):
        """Helper to allocate unique ids using the key/value store"""
        batch = Config().batch

        if not id_alloc_state:
            id_alloc_state = []
        else:
            id_alloc_state = yaml.safe_load(id_alloc_state)

        jsonschema.validate(id_alloc_state,
                            yaml.safe_load(id_allocation_schema))

        num_ids_preclean = len(id_alloc_state)
        # Cleanup completed jobs
        try:
            joblist = batch.list_all_jobs()
            id_alloc_state = [ pk for pk in id_alloc_state
                                 if int(pk['batchid']) in joblist ]
        except PcoccError:
            pass

        num_ids = len(id_alloc_state)
        stray_ids = num_ids_preclean - num_ids
        if stray_ids > 0:
            logging.warning(
                'Found %s leftover Ids, will try to cleanup',
                    stray_ids)

        if num_ids + count > self.num_ids:
            raise PcoccError('Not enough free ids in %s' %
                             self.key_path)

        id_indexes = []
        i = 0
        for allocated_id in sorted(id_alloc_state,
                                   key=lambda x: x['pkey_index']):

            while i < allocated_id['pkey_index'] and count > 0:
                id_indexes.append(i)
                i+=1
                count -= 1

            if count == 0:
                break

            i+=1
        else:
            id_indexes += [ i for i in xrange(i, i +  count) ]

        for i in id_indexes:
            id_alloc_state.append({'pkey_index': i,
                                   'batchid': batch.batchid})

        return yaml.dump(id_alloc_state), id_indexes