Skip to main content

Load and validate configurations using jsonschema

Project description

Schema config

PyPI GitLab PyPI - Implementation PyPI - Python Version PyPI - Wheel PyPI - Status


Requires CPython/PyPy 3.6 minimum

pip3 install schema-config~=[version]

Warning: In this early versions (0.X.Y) minor versions may introduce major breaking changes!


Loading a configuration

from pkg_resources import resource_string
from os import environ

from schema_config import Configurator

schema: bytes = resource_string(__name__, "config.schema.json")
factory: Configurator = Configurator.from_string(schema)

# The order is important, the first file will be loaded
if "CONFIG" in environ:
    # loads the configuration from a yaml file (typings come from YAML)
    # Search for your programm's configurations

# Get your configuration object
cfg = factory.load_config()

Specifying a configuration schema

schema-config is configured using jsonschema.

A possible schema could be:

    "$schema": "",
    "properties": {
        "database": {
            "type": "object",
            "properties": {
                "host": {
                    "type": "string"
                "port": {
                    "type": "number"
                "password": {
                    "type": "string"
            "required": ["host"]
    "required": ["database"]

Configuring your program

With the examples above a user could configure the program like this: /etc/program/config.yaml:

    host: ""
    port: $json{DB_PORT}
    password: $string{DB_PASSWORD}

With an environment variable (currently quotes are needed):


Both would be merged in a Configuration object similar to:

    "database": {
        "host": "",
        "port": 9999,
        "password": "test"

The Configurator class


From a file

factory = Configurator.from_file("<file name>")

From a string

factory = Configurator.from_string("<json schema as a string>")

From a dict

factory = Configurator(json_schema_as_a_dict)

Adding files

factory.add_file("<file path>")

Warning: The order is important! The first one will be loaded

Getting the configuration

cfg = factory.load_config()  # type: Configuration

The Configuration class

The Configuration class is something like an easier dict.

Getting items

With KeyError if not available

database = cfg["database"]
host = cfg[""]

With default:

host = cfg.get("", "localhost")
password = cfg.get("database.password")  # Default: None

Checking for items

"" in cfg

Setting items

cfg["database"] = {"host": ""}
cfg[""] = ""

Other useful abilities

Creating a new Configuration

cfg = Configuration(some_dict)

Getting a dict out of it

dict_cfg = cfg.raw

Issues & Suggestions

If you have any issues, suggestions, licensing problems, etc. just open an issue in the gitlab repo

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for schema-config, version 0.3.0
Filename, size File type Python version Upload date Hashes
Filename, size schema_config-0.3.0-py3-none-any.whl (5.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size schema-config-0.3.0.tar.gz (5.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page