Skip to content

Option to not create missing parents if sub-properties have defaults #34

@larsmaxfield

Description

@larsmaxfield

... Let's say we have a database configuration described by a JSON schema.

schema = {
    "properties": {
        "pool": {
            "properties": {
                "max_connections": {"type": "int", "default": 8},
                "min_connections": {"type": "int", "default": 0}
            }
        }
    }
}
instance = {}

fill_default(instance, schema)
assert instance == {}, instance

The "pool" is optional and a user did not provide a config, so defaults are not expected.
But if a user provided an empty pool config:

instance = {"pool": {}}

Then defaults should be filled (because the user provided the "pool" config, it is just empty).

It is also possible to have a root default:

schema = {
    "properties": {
        "pool": {
            "properties": {
                "max_connections": {"type": "int", "default": 8},
                "min_connections": {"type": "int", "default": 0}
            }
        }
    },
    "default": {
        "pool": {
            "max_connections": 8,
            "min_connections": 0
        }
    }
}

In this case, if a user provides an empty config, the root's default will be used.

Originally posted by @litteratum in #30

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions