Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Would you consider supporting json schema validation? #166

Open
chmduquesne opened this issue Sep 30, 2024 · 1 comment
Open

Would you consider supporting json schema validation? #166

chmduquesne opened this issue Sep 30, 2024 · 1 comment

Comments

@chmduquesne
Copy link

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant