pykwalify is a Python library and CLI tool for validating JSON and YAML data against a schema, based on the Kwalify schema specification. The current version is 1.8.0, with releases occurring periodically to add features, fix bugs, and update dependencies, though major development seems to have slowed since late 2020.
pip install pykwalifyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a schema and validate YAML data using pykwalify. It uses `io.StringIO` to simulate file input, making the example self-contained. It also shows how to catch `ValidationError` exceptions for invalid data.
Upgrade your Python environment to 3.6 or later.
Ensure `ruamel.yaml` (minimum 0.16.0) is installed and used. No code change is typically needed as `pykwalify` defaults to `ruamel.yaml` since v1.8.0.
Update schemas to use `type: bool` for boolean values or ensure data correctly uses `type: int` for numbers.
Install the required versions: `pip install 'ruamel.yaml>=0.16.0' 'python-dateutil>=2.8.0'`.
Install the library using pip: `pip install pykwalify`
Adjust the data to conform to the schema's type definition; for 'NotMappingError', ensure a dictionary is provided where `type: map` is specified in the schema, and for 'NotSequenceError', provide a list where `type: seq` is specified.
Use `yaml.safe_load()` to load a single YAML document into a dictionary or list, which can then be passed to `pykwalify.core.Core`.
Ensure that data values conform to the expected string type when using string-specific validation rules like `regex` or `pattern` in the schema. Convert non-string data to strings if appropriate for the schema definition.