How to use the pykwalify.core.Core function in pykwalify

To help you get started, we’ve selected a few pykwalify examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Grokzen / pykwalify / tests / test_unicode.py View on Github external
# Test unicode data inside seq but wrong type
            (u"3f.yaml", SchemaError),
        ]

        for failing_test, exception_type in _fail_tests:
            f = self.f(failing_test)

            with open(f, "r") as stream:
                yaml_data = yaml.safe_load(stream)
                data = yaml_data["data"]
                schema = yaml_data["schema"]
                errors = yaml_data["errors"]

            try:
                print(u"Running test files: {0}".format(f))
                c = Core(source_data=data, schema_data=schema)
                c.validate()
            except exception_type:
                pass  # OK
            else:
                raise AssertionError(u"Exception {0} not raised as expected... FILES: {1} : {2}".format(exception_type, exception_type))

            compare(sorted(c.validation_errors), sorted(errors), prefix=u"Wrong validation errors when parsing files : {0}".format(f))
github Grokzen / pykwalify / tests / test_core.py View on Github external
def test_load_yaml_files(self, tmpdir):
        """
        Load source & schema files that has yaml file ending.
        """
        source_f = tmpdir.join("foo.yaml")
        source_f.write("3.14159")

        schema_f = tmpdir.join("bar.yaml")
        schema_f.write("type: float")

        Core(source_file=str(source_f), schema_files=[str(schema_f)])
github Grokzen / pykwalify / tests / test_core.py View on Github external
for passing_test in pass_tests:
            try:
                c = Core(source_file=passing_test[1], schema_files=passing_test[0])
                c.validate()
                compare(c.validation_errors, [], prefix="No validation errors should exist...")
            except Exception as e:
                print("ERROR RUNNING FILE: {0} : {1}".format(passing_test[0], passing_test[1]))
                raise e

            # This serve as an extra schema validation that tests more complex structures then testrule.py do
            compare(c.root_rule.schema_str, passing_test[2], prefix="Parsed rules is not correct, something have changed...")

        for failing_test in failing_tests:
            with pytest.raises(failing_test[2], msg="Test files: {0} : {1}".format(", ".join(failing_test[0]), failing_test[1])):
                c = Core(schema_files=failing_test[0], source_file=failing_test[1])
                c.validate()

            if not c.validation_errors:
                raise AssertionError("No validation_errors was raised...")

            compare(
                sorted(c.validation_errors),
                sorted(failing_test[3]),
                prefix="Wrong validation errors when parsing files : {0} : {1}".format(
                    failing_test[0],
                    failing_test[1],
                ),
github RasaHQ / rasa_core / rasa_core / domain.py View on Github external
def validate_domain_yaml(cls, yaml):
        """Validate domain yaml."""
        from pykwalify.core import Core

        log = logging.getLogger('pykwalify')
        log.setLevel(logging.WARN)

        schema_file = pkg_resources.resource_filename(__name__,
                                                      "schemas/domain.yml")
        source_data = utils.read_yaml_string(yaml)
        c = Core(source_data=source_data,
                 schema_files=[schema_file])
        try:
            c.validate(raise_exception=True)
        except SchemaError:
            raise ValueError("Failed to validate your domain yaml '{}'. "
                             "Make sure the file is correct, to do so"
                             "take a look at the errors logged during "
                             "validation previous to this exception. "
                             "".format(os.path.abspath(input)))
github rakhimov / cppdep / cppdep / cppdep.py View on Github external
def __parse_config(self, config_file_path):
        """Parses the configuration file.

        Args:
            config_file_path: The path to the configuration file.

        Raises:
            YAMLError: Errors loading yaml files.
            SchemaError: The configuration file is malformed or invalid.
            InvalidArgumentError: The configuration has invalid values.
        """
        # Load before validation to check for well-formed YAML.
        with open(config_file_path) as config_file:
            self.config = safe_load(config_file)
        Validator(config_file_path, [_SCHEMA_FILE]).validate()

        for pkg_group_config in self.config['internal']:
            DependencyAnalysis.__add_package_group(pkg_group_config,
                                                   self.internal_groups)
        for pkg_group_config in yaml_optional_list(self.config, 'external'):
            DependencyAnalysis.__add_package_group(pkg_group_config,
                                                   self.external_groups)
github gijzelaerr / kliko / kliko / validate.py View on Github external
"""
    validate a kliko yaml string

    args:
        kliko: a parsed kliko object

    returns:
        dict: a (nested) kliko structure

    raises:
        an exception if the string can't be parsed or is not in the following the Kliko schema
    """
    # first try to parse it, to make sure it is parsable

    schema_file = os.path.join(here, "schemas/%s.yml" % version)
    c = Core(source_data=kliko, schema_files=[schema_file])
    c.validate(raise_exception=True)
    return kliko
github camptocamp / tilecloud-chain / tilecloud_chain / __init__.py View on Github external
self.config['cost']['cloudfront'] = {}
            if 'sqs' not in self.config['cost']:
                self.config['cost']['sqs'] = {}
        if 'generation' not in self.config:
            self.config['generation'] = {}
        for gname, grid in sorted(self.config.get('grids', {}).items()):
            if grid is not None:
                grid["name"] = gname
        for cname, cache in sorted(self.config.get('caches', {}).items()):
            if cache is not None:
                cache["name"] = cname
        for lname, layer in sorted(self.config.get('layers', {}).items()):
            if layer is not None:
                layer["name"] = lname

        c = Core(
            source_data=self.config,
            schema_data=yaml.safe_load(pkgutil.get_data("tilecloud_chain", "schema.yaml")),
        )
        path_ = ''
        try:
            self.config = c.validate()

            for name, cache in self.config['caches'].items():
                if cache['type'] == 's3':
                    c = Core(
                        source_data=cache,
                        schema_data=yaml.safe_load(
                            pkgutil.get_data("tilecloud_chain", "schema-cache-s3.yaml")),
                    )
                    path_ = 'caches/{}'.format(name)
                    self.config['caches'][name] = c.validate()
github BROADSoftware / HADeploy / lib / hadeploy / core / schema.py View on Github external
def validate(model, schema):
    k = kwalify(source_data = model, schema_data=schema)
    try:
        k.validate(raise_exception=False)
        if len(k.errors) != 0:
            misc.ERROR("Problem {0}".format(k.errors))
    except Exception as e:          # Need to catch, as, despite raise_exeception=False, some cases still generate exception (ie map/list mismatch)
        misc.ERROR("Problem {0}".format(e))
github chuyskywalker / aws-lambda-github-webhook / src / checks / hooks_schema.py View on Github external
logger.info("Fetched .hooks.yml from repo {}".format(repo))
        except github.GithubException:
            logger.error("Missig .hooks.yml on repo {}".format(repo))
            send_status(event, context, gh_hook, self.configname, 'success', ".hooks.yml not present in branch")
            return

        try:
            hook_config = yaml.safe_load(hooks_yml.decoded_content)
            logger.info("Basic yml validation passed")
        except Exception as e:
            logger.error("Failed to decode hook yaml: " + e.message)
            send_status(event, context, gh_hook, self.configname, 'failure', "Could not decode branch .hooks.yml")
            return

        logger.info("Advanced schema validation")
        c = Core(source_data=hook_config,
                 schema_files=[os.path.join(os.path.dirname(__file__), "..", "hooks.schema.yml")])
        c.validate(raise_exception=False)
        vc = len(c.validation_errors)
        if vc > 0:
            for err in c.validation_errors:
                logger.error(" - {}".format(err))
            send_status(event, context, gh_hook, self.configname, 'failure', ".hooks.yml has {} validation errors; see log".format(vc))
            return

        send_status(event, context, gh_hook, self.configname, 'success', ".hooks.yml present and valid")
github zephyrproject-rtos / zephyr / scripts / yaml_to_cmake.py View on Github external
def main():
    parser = argparse.ArgumentParser(description='''
    Converts YAML to a CMake list''')

    parser.add_argument('-i', '--input', required=True,
                        help='YAML file with data')
    parser.add_argument('-o', '--output', required=True,
                        help='File to write with CMake data')
    parser.add_argument('-s', '--section', required=True,
                        help='Section in YAML file to parse')
    args = parser.parse_args()

    with open(args.input, 'r') as f:
        meta = yaml.safe_load(f.read())

    pykwalify.core.Core(source_data=meta,
                        schema_data=yaml.safe_load(METADATA_SCHEMA)).validate()

    val_str = ''

    section = meta.get(args.section)
    if section is not None:
        for key in section:
            val_str += '{}={}\n'.format(key, section[key])

    with open(args.output, 'w') as f:
        f.write(val_str)