confuse icon indicating copy to clipboard operation
confuse copied to clipboard

Would you consider supporting json schema validation?

Open chmduquesne opened this issue 1 year ago • 1 comments

Hi,

An increasingly popular way to verify that a json document conforms to a specification is to provide a json schema to validate it. The benefit of json schemas is that there is already a lot of 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:

# 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:

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:

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:

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

chmduquesne avatar Sep 30 '24 10:09 chmduquesne

To further clarify my question: I would offer my contribution to implement such a function, but only if it this is of interest to the maintainers.

chmduquesne avatar Oct 01 '24 07:10 chmduquesne