Commit f93841f3 authored by Christophe Benz's avatar Christophe Benz
Browse files

Add conf validation script

parent 495042ae
# Validation du fichier de configuration
Un script Python pour valider le fichier de configuration du badge Validata.
## Pré-requis
Pour installer les dépendances :
```bash
pip install -r requirements.txt
```
## Utilisation
```bash
python3 validate_conf.py ../badge_conf.md
```
En cas d'erreur, celle-ci est affichée. Le code de retour du script vaut `0` si aucune erreur n'est détectée, et `1` sinon.
\ No newline at end of file
jsonschema==3.0.1
toml==0.10.0
{
"$id": "https://git.opendatafrance.net/validata/validata-badge/raw/master/validation/schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ValidataBadgeConfiguration",
"type": "object",
"properties": {
"body": {
"type": "object",
"properties": {
"acceptability-threshold": {
"type": "number",
"description": "Seuil d'acceptabilité",
"minimum": 0,
"maximum": 1
},
"errors-weight": {
"type": "object",
"properties": {
"pattern-constraint": {
"type": "number",
"minimum": 0,
"maximum": 1
},
"maximum-length-constraint": {
"type": "number",
"minimum": 0,
"maximum": 1
}
},
"required": ["pattern-constraint", "maximum-length-constraint"]
}
},
"required": ["acceptability-threshold", "errors-weight"]
}
},
"required": ["body"]
}
#!/usr/bin/env python3
import argparse
import json
import sys
from pathlib import Path
import toml
from jsonschema import Draft7Validator
from jsonschema.exceptions import best_match
def main():
"""Validate configuration file"""
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('conf_file', type=Path, help='path of badge config file')
args = parser.parse_args()
conf_file = args.conf_file
if not conf_file.exists():
parser.error("Configuration file {!r} not found".format(str(conf_file)))
try:
conf = toml.load(str(conf_file))
except toml.decoder.TomlDecodeError as exc:
print(exc)
return 1
with (Path(__file__).parent / "schema.json").open() as fp:
schema = json.load(fp)
error = best_match(Draft7Validator(schema).iter_errors(conf))
if error is not None:
print(error)
return 1
return 0
if __name__ == '__main__':
sys.exit(main())
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment