beetbox / beetbox/confuse

Would you consider supporting json schema validation?

Open
#166 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
424
Forks
57
PR merge metrics
No merged PRs in 30d

Description

Hi,

An increasingly popular way to verify that a json document conforms to a specification is to provide a [json schema](https://json-schema.org/) to validate it. The benefit of json schemas is that there is already a lot of [tools](https://json-schema.org/tools) to generate web UIs, lint, validate, etc.

Since yaml and json are compatible, it is possible to create a json schema to validate a yaml file. For example, let us consider this example from the [documentation](https://confuse.readthedocs.io/en/latest/examples.html):

```yaml
# servers_example.json
servers:
- host: one.example.com
- host: two.example.com
port: 8000
- host: three.example.com
port: 8080
```

Currently, confuse offers a way to validate this configuration based on a template:
```python
import confuse

source = confuse.YamlSource('servers_example.yaml')
config = confuse.RootView([source])
template = {
'servers': confuse.Sequence({
'host': str,
'port': 80,
}),
}

valid_config = config.get(template)
```

With a json schema, the same configuration can be validated in a similar fashion:

```python
import jsonschema
import yaml

yaml_source = 'servers_example.yaml'

with open(yaml_source) as f:
instance = yaml.safe_load(f)

schema = {
"type": "object",
"properties": {
"server": {
"type": "array",
"items": {
"type": "object",
"properties": {
"host": {
"type": "string"
},
"port": {
"type": "integer",
"default": 80
}
}
}
}
}
}

jsonschema.validate(instance=instance, schema=schema)
```

So it looks like confuse templates and json schemas can be used to achieve similar things. Now, what a json schema cannot do is resolve the configuration values from various places.

Given the amount of standardization that json schemas benefit from, I think it would be interesting to try to bring them in confuse. Therefore, I am wondering: would it be possible to generate a confuse template from a json schema? Looking at the spec, it looks very much possible.

As a user, I think it would be neat if confuse would offer a way to build a template from a json schema. Reusing the previous example, here is how I imagine it could work:

```python
import confuse

source = confuse.YamlSource('servers_example.yaml')
config = confuse.RootView([source])
schema = {
"type": "object",
"properties": {
"server": {
"type": "array",
"items": {
"type": "object",
"properties": {
"host": {
"type": "string"
},
"port": {
"type": "integer",
"default": 80
}
}
}
}
}
}
# new function creating a template from a json schema
template = confuse.from_jsonschema(schema)
valid_config = config.get(template)

```

What do you think? Would you be interested in supporting json schemas?

Cheers,
Christophe-Marie

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.